安卓之Animator讲解

Animator

Animator 动画的实现机制说起来其实更加简单一点,因为他其实只是计算动画开启之后,结束之前,到某个时间点得时候,某个属性应该有的值,然后通过回调接口去设置具体值,其实 Animator 内部并没有针对某个 view 进行刷新,来实现动画的行为,动画的实现是在设置具体值的时候,方法内部自行调取的类似 invalidate 之类的方法实现的.也就是说,使用 Animator ,内部的属性发生了变化。(引用大佬的讲解链接: https://blog.csdn.net/y874961524/article/details/53980165,您可以点击观看详细的对Animator的讲解)

ndroid动画分为两类:
1.View Animation(视图动画,在api1引入)View Animation又分为两类:Frame animation(幁动画) 和Tween animation (补间动画)。
2.Property Animator(属性动画,在api11引入)。
今天我们学的是Property Animator。
话不多说直接上案例。
当然我们首先了解动画,rotation(旋转),scale(缩放),tranlation(移动)。
在安卓中创建一个安卓项目,在这里我就不介绍如何创建一个安卓项目了。然后在res下创建一个animator。
在这里插入图片描述
在animator.xml中编写如下代码:

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:ordering="sequentially">

    <set android:ordering="together">

        <objectAnimator

            android:duration="2000"

            android:propertyName="rotation"

            android:valueFrom="0"

            android:valueTo="360"

            android:valueType="floatType" />

        <objectAnimator

            android:duration="2000"

            android:propertyName="scaleX"

            android:valueFrom="0"

            android:valueTo="2" />

        <objectAnimator

            android:duration="2000"

            android:propertyName="scaleY"

            android:valueFrom="0"

            android:valueTo="2" />

        <objectAnimator

            android:duration="2000"

            android:propertyName="translationY"

            android:valueFrom="0"

            android:valueTo="400" />
    </set>
    <set android:ordering="together">

        <objectAnimator

            android:duration="2000"

            android:propertyName="translationY"

            android:valueFrom="400"

            android:valueTo="0" />

        <objectAnimator

            android:duration="2000"

            android:propertyName="scaleX"

            android:valueFrom="2"

            android:valueTo="1" />


    </set>

</set>

接下来讲解上面的代码:
android:duration:动画持续时间;

android:propertyName:动画类型;

android:repeatCount:重复次数,-1为一直重复;

android:repeatMode:重复模式:reverse(从结束的位置继续), restart(从新开始);

android:valueFrom:起始值;

android:valueTo:结束值。

android:valueType:值类型
移动分为沿x、y轴移动,沿x轴时使用translationX,沿y轴移动使用translationY。
接下来在你的项目的xml中编写四个按钮,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="rotation"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button1"
        android:layout_marginLeft="0dp"
        android:text="scale"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button2"
        android:layout_marginLeft="0dp"
        android:text="translate"
        />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button3"
        android:layout_marginLeft="0dp"
        android:text="set"
        />
</RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
     <ImageView
         android:id="@+id/imageView"
         android:layout_width="100sp"
         android:layout_height="100sp"
         android:src="@drawable/one"
         />
    </RelativeLayout>
</LinearLayout>

然后在java中编写的代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1=(Button)findViewById(R.id.button1);
        Button button2=(Button)findViewById(R.id.button2);
        Button button3=(Button)findViewById(R.id.button3);
        Button button4=(Button)findViewById(R.id.button4);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);

        imageView=(ImageView)findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                ObjectAnimator rotation= ObjectAnimator.ofFloat(imageView,"rotationY",0f,360f);//设置图片从0度旋转到360
                rotation.setDuration(4000);//设置动画时长
                //如果你想让它一直重复的话,可以使用ObjectAnimator提供的setRepeatCount(int count)。count为重复次数,-1表示一直重复。
                rotation.setRepeatCount(-1);
                rotation.start();//开始动画
                break;
            case R.id.button2:
                ObjectAnimator scale=ObjectAnimator.ofFloat(imageView,"scaleX",0,2,1);
                scale.setDuration(4000);//设置动画时长
                scale.setInterpolator(new AccelerateDecelerateInterpolator());//加速度插值器:表示越到后面变化越快
                scale.start();//开始动画
                break;
            case R.id.button3:
                 ObjectAnimator translation=ObjectAnimator.ofFloat(imageView,"TranslationY",0,500,0);
                 translation.setDuration(4000);
                 translation.start();
                break;
            case R.id.button4:

                AnimatorSet objectAnimator = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.animator);//将xml中的动画加载
                //这是设置颜色的
                ObjectAnimator translation1 = ObjectAnimator.ofInt(imageView,"backgroundColor",0xffff0000,0xff00ff00,0xff0000ff);

                translation1.setEvaluator(new ArgbEvaluator());

                translation1.setDuration(4000);

                translation1.setInterpolator(new LinearInterpolator());

                objectAnimator.setTarget(imageView);

                objectAnimator.play(translation1);

                objectAnimator.start();
                break;
                default:
                    break;
        }
    }
}

Interpolator:
Interpolator定义了动画变化的速率,在Animation框架当中定义了以下几种Interpolator
AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator:在动画开始的地方速率该百年比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率该百年比较慢
LinearInterpolator:在动画的以均匀的速率该改变
我们的实例就做完了.

发布了11 篇原创文章 · 获赞 14 · 访问量 2618

猜你喜欢

转载自blog.csdn.net/jzdcuccess/article/details/105745003