Android--只是简单的ObjectAnimator (单个动画)

/*
这个ObjectAnimator 继承的父类是ValueAnimator 效果是一样的 但是这个比其父类(ValueAnimator )更加简单一点
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt1,bt2,bt3,bt4,bt5;
    private ImageView img;
    private List list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt1 =  findViewById(R.id.bt_1);
        bt2 =  findViewById(R.id.bt_2);
        bt3 = findViewById(R.id.bt_3);
        bt4 = findViewById(R.id.bt_4);
        bt5 = findViewById(R.id.bt_5);
        img = findViewById(R.id.img_pg);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);


    }
    private ObjectAnimator objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.bt_1://平移

                objectAnimator1 = ObjectAnimator.ofFloat(img,"translationX",0,500);
                objectAnimator1.setDuration(3000);
                objectAnimator1.start();

                break;

                case R.id.bt_2://淡入淡出
                     objectAnimator2=ObjectAnimator.ofFloat(img,"alpha",1,0.5F,0,0.5F,1);
                    objectAnimator2.setDuration(6000);
                    objectAnimator2.setRepeatCount(10);
                    objectAnimator2.start();

                break;

                case R.id.bt_3://渐变
                     objectAnimator3=ObjectAnimator.ofArgb(img, "backgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);
                    objectAnimator3.setDuration(6000);
                    objectAnimator3.setRepeatCount(10);
                    objectAnimator3.start();
                break;

                case R.id.bt_4://旋转
                     objectAnimator4=ObjectAnimator.ofFloat(img,"rotationY",0,360);
                    objectAnimator4.setDuration(3000);
                    objectAnimator4.start();
                    break;
//                    case R.id.bt_5://组合动画
////                    ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(img,"rotationY",0,360);
////                    objectAnimator2.setDuration(3000);
////                    objectAnimator2.start();
//                        AnimatorSet animatorSet = new AnimatorSet();
//                        animatorSet.playSequentially(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4);
//                        animatorSet.setDuration(2000);
//                        animatorSet.start();
//                        break;
        }
    }

}

 

你可能感兴趣的:(Android--只是简单的ObjectAnimator (单个动画))