简单的补间动画Animation和属性动画ObjectAnimator

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {  
  2.   
  3.     private Mycircle ci;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         ci = findViewById(R.id.ci);  
  10.         Button tran = findViewById(R.id.tran);  
  11.         Button rotate = findViewById(R.id.rotate);  
  12.         Button scale = findViewById(R.id.scale);  
  13.         Button alpha = findViewById(R.id.alpha);  
  14.         Button zh = findViewById(R.id.zh);  
  15.         Button py = findViewById(R.id.py);  
  16.         tran.setOnClickListener(this);  
  17.         rotate.setOnClickListener(this);  
  18.         scale.setOnClickListener(this);  
  19.         alpha.setOnClickListener(this);  
  20.         zh.setOnClickListener(this);  
  21.         py.setOnClickListener(this);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onClick(View view) {  
  26.         switch (view.getId()) {  
  27.             case R.id.tran:  
  28.                 //平移动画   补间动画  
  29.                 TranslateAnimation trans = new TranslateAnimation(05000500);  
  30.                 trans.setDuration(3000);  
  31.                 ci.startAnimation(trans);  
  32.                 break;  
  33.             case R.id.scale:  
  34.                 //缩放  
  35.                 ScaleAnimation scaleAnimation = new ScaleAnimation(0202, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  36.                 scaleAnimation.setDuration(3000);  
  37.                 ci.startAnimation(scaleAnimation);  
  38.                 break;  
  39.             case R.id.rotate:  
  40.                 //旋转  
  41.                 Animation rotateAnimation = new RotateAnimation(0270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  42.                 rotateAnimation.setDuration(3000);  
  43.                 ci.startAnimation(rotateAnimation);  
  44.                 break;  
  45.             case R.id.alpha:  
  46.                 //透明  
  47.                 AlphaAnimation alphaAnimation = new AlphaAnimation(10);  
  48.                 alphaAnimation.setDuration(3000);  
  49.                 ci.startAnimation(alphaAnimation);  
  50.                 break;  
  51.             case R.id.py:  
  52.                 //属性动画  
  53.                 float tr = ci.getTranslationX();  
  54.                 ObjectAnimator translationX = ObjectAnimator.ofFloat(ci, "translationX", 300f);  
  55.                 translationX.setDuration(3000);  
  56.                 translationX.start();  
  57.                 break;  
  58.             case R.id.zh:  
  59.                 //平移 属性动画  
  60.                //TranslateAnimation  
  61.                 ObjectAnimator tranX = ObjectAnimator.ofFloat(ci, "translationX"0300);  
  62.                 //创建透明度动画  
  63.                 ObjectAnimator alpha = ObjectAnimator.ofFloat(ci, "alpha"1.0f, 0f);  
  64.                  //旋转  
  65.                 ObjectAnimator rotation = ObjectAnimator.ofFloat(ci, "rotation", 0f, 360f);  
  66.                 //缩放  
  67.                 ObjectAnimator scaleY = ObjectAnimator.ofFloat(ci, "scaleY", 1f, 3f);  
  68.                 //动画集合  
  69.                 AnimatorSet set = new AnimatorSet();  
  70.                 //添加动画  
  71.                 set.play(rotation).with(alpha).after(tranX);//可选  
  72.                 //设置时间等  
  73.                 set.setDuration(5000);  
  74.                 set.start();  
  75.   
  76.                 //动画监听  
  77.                 set.addListener(new AnimatorListenerAdapter() {  
  78.                     @Override  
  79.                     public void onAnimationEnd(Animator animation) {  
  80.                         super.onAnimationEnd(animation);  
  81.                         ci.setVisibility(View.GONE);  
  82.                     }  
  83.   
  84.                     @Override  
  85.                     public void onAnimationRepeat(Animator animation) {  
  86.                         super.onAnimationRepeat(animation);  
  87.                     }  
  88.   
  89.                     @Override  
  90.                     public void onAnimationStart(Animator animation) {  
  91.                         super.onAnimationStart(animation);  
  92.                     }  
  93.                 });  
  94.                 break;  
  95.         }  
  96.     }  
  97. }  

你可能感兴趣的:(简单的补间动画Animation和属性动画ObjectAnimator)