Android 属性动画

在这里插入图片描述
1.首先在res下边创建一个Directory文件夹,命名为animator
2.在animator里边创建objectAnimator
3.objectanimator.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"

    android:propertyName="rotationX"
    android:duration="3000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:startOffset="0"
    android:valueFrom="360.0"
    >

</objectAnimator>

4.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="alpha透明"/>

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="translationY平移"/>
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="scaleX缩放"/>
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rotationY旋转"/>
    <Button
        android:id="@+id/bt5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="animatorset两个动画"/>
    <ImageButton
        android:id="@+id/ib1"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:src="@drawable/n"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

5.主页面MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private ImageButton ib1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
       /* //静态使用动画的效果  就是把一个动画的xml文件变成一个对象 方便我们操控
        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
        //吧要做动画的控件设置给animator对象
        animator.setTarget(ib1);
        //开启动画
        animator.start();*/


    }

    private void initView() {
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);
        ib1 = (ImageButton) findViewById(R.id.ib1);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        ib1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                //透明的动画alpha
                //参数1  是你要执行动画控件  参数2  要做的动画类型  参数3动画执行每个节点的属性
                ObjectAnimator alpha = ObjectAnimator.ofFloat(ib1, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
                alpha.setDuration(2000);//执行时间
                alpha.setRepeatMode(ObjectAnimator.RESTART);//执行模式
                alpha.setRepeatCount(1);//执行重复次数
                alpha.start();//开始执行
                break;
            case R.id.bt2:
                //平移的动画translationY
                //参数1  是你要执行动画控件  参数2  要做的动画类型  参数3动画执行每个节点的属性
                ObjectAnimator translationY = ObjectAnimator.ofFloat(ib1, "translationY", new float[]{10f, 20f, 40f, 10f});
                translationY.setDuration(2000);//执行时间
                translationY.setRepeatMode(ObjectAnimator.RESTART);//执行模式
                translationY.setRepeatCount(2);//执行重复次数
                translationY.start();//开始执行
                break;
            case R.id.bt3:
                //缩放的动画scaleX
                //参数1  是你要执行动画控件  参数2  要做的动画类型  参数3动画执行每个节点的属性
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(ib1, "scaleX", new float[]{1f, 2f, 4f, 1f});
                scaleX.setDuration(2000);//执行时间
                scaleX.setRepeatMode(ObjectAnimator.RESTART);//执行模式
                scaleX.setRepeatCount(2);//执行重复次数
                scaleX.start();//开始执行
                break;
            case R.id.bt4:
                //旋转立体的动画rotationY
                //参数1  是你要执行动画控件  参数2  要做的动画类型  参数3动画执行每个节点的属性
                ObjectAnimator rotationY = ObjectAnimator.ofFloat(ib1, "rotationY", new float[]{90f, 180f, 270f, 360f});
                rotationY.setDuration(2000);//执行时间
                rotationY.setRepeatMode(ObjectAnimator.RESTART);//执行模式
                rotationY.setRepeatCount(2);//执行重复次数
                rotationY.start();//开始执行
                break;
            case R.id.bt5:
                //做动画的集合,分别创建两个动画对象,然后执行
                //可以再同一时间执行两个动画
                AnimatorSet set = new AnimatorSet();
                //透明
                ObjectAnimator alpha1 = ObjectAnimator.ofFloat(ib1, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
                alpha1.setDuration(2000);//执行时间
                //缩放
                ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(ib1, "scaleX", new float[]{1f, 2f, 4f, 1f});
                scaleX1.setDuration(2000);//执行时间
                //set把这两个对象放入咱们的动画集合中
                //playTogether  一起执行
                //set.playTogether(alpha1,scaleX1);
                //分别执行playSequentially
                set.playSequentially(alpha1,scaleX1);
                //开始执行
                set.start();
                break;
            case R.id.ib1:

                break;
        }
    }
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值