- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Mycircle ci;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ci = findViewById(R.id.ci);
- Button tran = findViewById(R.id.tran);
- Button rotate = findViewById(R.id.rotate);
- Button scale = findViewById(R.id.scale);
- Button alpha = findViewById(R.id.alpha);
- Button zh = findViewById(R.id.zh);
- Button py = findViewById(R.id.py);
- tran.setOnClickListener(this);
- rotate.setOnClickListener(this);
- scale.setOnClickListener(this);
- alpha.setOnClickListener(this);
- zh.setOnClickListener(this);
- py.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.tran:
- //平移动画 补间动画
- TranslateAnimation trans = new TranslateAnimation(0, 500, 0, 500);
- trans.setDuration(3000);
- ci.startAnimation(trans);
- break;
- case R.id.scale:
- //缩放
- ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- scaleAnimation.setDuration(3000);
- ci.startAnimation(scaleAnimation);
- break;
- case R.id.rotate:
- //旋转
- Animation rotateAnimation = new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- rotateAnimation.setDuration(3000);
- ci.startAnimation(rotateAnimation);
- break;
- case R.id.alpha:
- //透明
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- alphaAnimation.setDuration(3000);
- ci.startAnimation(alphaAnimation);
- break;
- case R.id.py:
- //属性动画
- float tr = ci.getTranslationX();
- ObjectAnimator translationX = ObjectAnimator.ofFloat(ci, "translationX", 300f);
- translationX.setDuration(3000);
- translationX.start();
- break;
- case R.id.zh:
- //平移 属性动画
- //TranslateAnimation
- ObjectAnimator tranX = ObjectAnimator.ofFloat(ci, "translationX", 0, 300);
- //创建透明度动画
- ObjectAnimator alpha = ObjectAnimator.ofFloat(ci, "alpha", 1.0f, 0f);
- //旋转
- ObjectAnimator rotation = ObjectAnimator.ofFloat(ci, "rotation", 0f, 360f);
- //缩放
- ObjectAnimator scaleY = ObjectAnimator.ofFloat(ci, "scaleY", 1f, 3f);
- //动画集合
- AnimatorSet set = new AnimatorSet();
- //添加动画
- set.play(rotation).with(alpha).after(tranX);//可选
- //设置时间等
- set.setDuration(5000);
- set.start();
- //动画监听
- set.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
- ci.setVisibility(View.GONE);
- }
- @Override
- public void onAnimationRepeat(Animator animation) {
- super.onAnimationRepeat(animation);
- }
- @Override
- public void onAnimationStart(Animator animation) {
- super.onAnimationStart(animation);
- }
- });
- break;
- }
- }
- }