Android动画之属性动画

本文详细介绍了Android属性动画的使用,包括透明、位移、旋转和缩放动画的实现,并通过一个Demo展示了如何使用AnimatorSet进行动画组合,实现了更复杂的动画效果。

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
为了更好的演示,写了一个Demo,xml界面如下(最后有源码)
在这里插入图片描述

要介绍的有:
1.透明动画:alpha
2.位移动画:translationX,translationY
3.旋转动画:rotation
4.缩放动画:scaleX,scaleY
5.组合显示:AnimatorSet(动画集合容器)

1.透明动画:alpha在这里插入图片描述
2.位移动画:translationX,translationY
在这里插入图片描述
3.旋转动画:rotation
在这里插入图片描述
4…缩放动画:scaleX,scaleY
在这里插入图片描述
5.组合显示:AnimatorSet(动画集合容器)在这里插入图片描述
源码如下:
activity_third.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ThirdActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="透明动画" />

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="位移动画" />

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="旋转动画" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="缩放动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="groupshow"
        android:text="组合显示" />
</RelativeLayout>

ThirdActivity.java文件:

//属性动画
public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_translate;
    private Button btn_rotate;
    private Button btn_scale;
    private ImageView iv_show;
    ObjectAnimator objectAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        initView();
    }


    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_translate = (Button) findViewById(R.id.btn_translate);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        iv_show = (ImageView) findViewById(R.id.iv_show);

        btn_alpha.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //ofFloat:三个参数 :1.受到动画影响的对象(UI控件)2. 要执行的动画类型 3. 一组动画的属性
        switch (v.getId()) {
            case R.id.btn_alpha://透明动画
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "alpha", 0.5f, 1f, 0.5f, 1f);
                break;
            case R.id.btn_translate://位移动画
                //只会执行一个
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "translationX", 0, 200);
                //objectAnimator=ObjectAnimator.ofFloat(iv_show,"translationY",0,200);
                break;
            case R.id.btn_rotate://旋转动画
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);
                break;
            case R.id.btn_scale://缩放动画
                //只会执行一个
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 3f, 4f);
                // objectAnimator=ObjectAnimator.ofFloat(iv_show,"scaleX",1f,2f,3f,4f);

                break;
        }
        //动画持续时间
        objectAnimator.setDuration(3000);
        //启动动画
        objectAnimator.start();

    }

    public void groupshow(View view) {//组合

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 1f, 2f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(iv_show, "scaleX", 1f, 2f, 1f, 2f);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);
        //创建属性动画的集合容器
        AnimatorSet animatorSet = new AnimatorSet();
        //同时播放
        animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3);
        //按照顺序播放
        // animatorSet.play(objectAnimator1).after(objectAnimator2).after(objectAnimator3);

        /*
        * 另一种方式
        List<Animator> list=new ArrayList<>();
        list.add(objectAnimator1);
        list.add(objectAnimator2);
        list.add(objectAnimator3);
        animatorSet.playSequentially(list);//按照顺序播放
        animatorSet.playTogether(list);//同时执行
        */
        //设置时长
        animatorSet.setDuration(3000);
        //启动
        animatorSet.start();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值