属性动画ValueAnimtor和ObjectAnimtor

1,设置布局

在这里插入图片在这里插入图片描述描述

ic class MainActivity extends AppCompatActivity {

private ImageView img;
private Button btn;
private Button btn1;

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

    img = findViewById(R.id.img);
    btn = findViewById(R.id.btn);
    btn1 = findViewById(R.id.btn1);

    //ObjectAnimtor
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //平移
            ObjectAnimator transition=ObjectAnimator.ofFloat(img,"TranslationX",0,100,200);
            //旋转
            ObjectAnimator rotate=ObjectAnimator.ofFloat(img,"Rotation",0,360);
            //透明度
            ObjectAnimator alpha=ObjectAnimator.ofFloat(img,"Alpha",0,100);
            //缩放
            ObjectAnimator scaleX=ObjectAnimator.ofFloat(img,"ScaleX",0,1);
            AnimatorSet animatorSet=new AnimatorSet();
            //执行顺序
            animatorSet.play(transition).with(rotate).with(scaleX).before(alpha);
            animatorSet.setDuration(5000);
            animatorSet.start();
        }
    });

    //ValueAnimtor
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ValueAnimator valueAnimator=ValueAnimator.ofInt(0,1000);
            valueAnimator.setDuration(6000);
            valueAnimator.setStartDelay(5000);
           valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
               @Override
               public void onAnimationUpdate(ValueAnimator animation) {
                   int value = (int) animation.getAnimatedValue();
                   //重新给他赋值
                   img.getLayoutParams().width=value;
                   //刷新视图
                   img.requestLayout();
               }
           });
           //开始动画
            valueAnimator.start();
        }
    });

}

}

猜你喜欢

转载自blog.csdn.net/qq_43589991/article/details/84993769