属性动画模版

来源:互联网 发布:互联网数据分析师前景 编辑:程序博客网 时间:2024/05/22 05:06
public class MainActivity extends AppCompatActivity {        private static final String TAG = "MainActivity";        private Button btnStart;        private Button btnStartTween;        private Button btnStop;        private TextView txtHello;        private ObjectAnimator animatr;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            btnStart = (Button) findViewById(R.id.btn_start);            btnStartTween = (Button) findViewById(R.id.btn_start_tween);            btnStop = (Button) findViewById(R.id.btn_stop);            txtHello = (TextView) findViewById(R.id.txt_hello);            txtHello.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    Log.i(TAG, "onClick: " + v.getX());                }            });            // 补间动画的属性并没有发生变化,属性动画是真正改变了视图的值            btnStartTween.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    TranslateAnimation animation = new TranslateAnimation(0, 500, 0, 0);                    animation.setDuration(3000);                    txtHello.startAnimation(animation);                }            });            btnStart.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    // 可以对一个值进行平滑的改变                    // ValueAnimator只是对一个值做的改变//                ValueAnimator animator = ValueAnimator.ofInt(1, 9);//                ValueAnimator animator = ValueAnimator.ofFloat(0, 1);//                animator.setDuration(3000);//                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//                    @Override//                    public void onAnimationUpdate(ValueAnimator animation) {//                        Log.i(TAG, "onAnimationUpdate: " + animation.getAnimatedValue());//                    }//                });//                animator.start();//                txtHello.setTranslationX(500);//                txtHello.setTextColor();                    // 属性动画可以完成补间动画的所有效果,也可以实现其他补间动画没有的效果,                    // 属性动画本质上是改变了视图的属性,所以只要视图有公开的set方法,就可以进行变换//                ObjectAnimator animatr = ObjectAnimator.ofInt(//                        txtHello,//                        "textColor",//                        Color.RED, Color.GREEN//                );//                txtHello.setAlpha();//                ObjectAnimator animatr = ObjectAnimator.ofFloat(//                        txtHello,//                        "alpha",//                        1, 0//                );//                txtHello.setRotation();//                animatr = ObjectAnimator.ofFloat(//                        txtHello,//                        "rotation",//                        90, 360//                );//                animatr = ObjectAnimator.ofFloat(//                        txtHello,//                        "translationX",//                        0, 500//                ).ofFloat(//                        txtHello,//                        "rotation",//                        0, 360//                );                    Animator animatr01 = ObjectAnimator.ofFloat(                            txtHello,                            "translationX",                            0, 500                    );                    Animator animatr02 = ObjectAnimator.ofFloat(                            txtHello,                            "rotation",                            0, 360                    );                    Animator animatr03 = ObjectAnimator.ofFloat(                            txtHello,                            "alpha",                            0, 1                    );                    Animator animatr04 = ObjectAnimator.ofFloat(                            txtHello,                            "scaleX",                            0, 1                    );//                animatr01.setDuration(3000);//                animatr02.setDuration(3000);//                animatr03.setDuration(3000);//                animatr04.setDuration(3000);                    // 动画的集合                    AnimatorSet set = new AnimatorSet();                    set.setDuration(3000);                    set.play(animatr01) // 平移                            // 在。。。之后                            .after(animatr02) // 旋转                            // 和....一起                            .with(animatr03) // 透明度                            // 在。。。。之前                            .before(animatr04); // 缩放                    set.start();                    // 旋转-透明度/平移-缩放                    // 动画自定义差值器                    // 先加速后减速//                animatr.setInterpolator(new AccelerateDecelerateInterpolator());                    // 加速//                animatr.setInterpolator(new AccelerateInterpolator());                    // 减速//                animatr.setInterpolator(new DecelerateInterpolator());                    // 匀速//                animatr.setInterpolator(new LinearInterpolator());////                animatr.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//                    @Override//                    public void onAnimationUpdate(ValueAnimator animation) {//                        Log.i(TAG, "onAnimationUpdate:" + animation.getAnimatedValue());//                    }//                });//                ObjectAnimator animatr = ObjectAnimator.ofFloat(//                        txtHello,//                        "translationX",//                        0, 500//                );//                animatr.setDuration(3000);////                animatr.setRepeatCount(3);//                animatr.start();                }            });            btnStop.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    if(animatr.isStarted()){                        animatr.cancel();                    }                }            });        }    }