您的位置:首页 > 移动开发 > Objective-C

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

2018-01-10 09:33 369 查看
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;  

        }  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐