欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 属性动画(缩放,渐变,移动,旋转)

程序员文章站 2024-03-24 12:16:16
...

首先设置一个图片,引入布局

<ImageView
        android:id="@+id/iv_splash"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/i"
        android:layout_gravity="center"
        />

第二步:初始化initData()

getSupportActionBar().hide();//隐藏标题栏
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();//这个好像是可以获取屏幕高度
        int height = displayMetrics.heightPixels;//获取屏幕高度
        //缩放---ofFloat用4个参数的ofFloat
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv_splash, "scaleX", 2, 1);
        //渐变
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(iv_splash, "alpha", 0, 1);
        //旋转
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(iv_splash, "rotation", 0, 360);
        //移动
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(iv_splash, "translationY", 0, height / 2 - iv_splash.getHeight() / 2);

        //组合
        AnimatorSet set = new AnimatorSet();
         /**
         * 动画执行
         */
        set.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3).with(objectAnimator4);
        set.setDuration(3000);
        set.start();

第三步:给动画添加监听

/**
         * 动画执行的监听
         */
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }
            //动画执行完后跳转到主activity
            @Override
            public void onAnimationEnd(Animator animation) {
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });