Android动画小结

来源:互联网 发布:淘宝店铺怎么写标题 编辑:程序博客网 时间:2024/05/17 13:42
其中属性动画是3.0开始,矢量动画是5.0可以使用,总结的还有很多不足请指正。
一,平移动画的基本实现方式 
     1.基本实现,缺点:点击事件不会随着图片的移动而移动
//第一个参数 Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.//第二个参数是设置位置
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.1f,Animation.RELATIVE_TO_SELF,2.0f,
        Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f);
translateAnimation.setDuration(5000);
translateAnimation.setFillAfter(true);
translateAnimation.setInterpolator(new BounceInterpolator());
imageView.startAnimation(translateAnimation);
     2.运用属性动画
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "TranslationX", 10, 200).setDuration(5000);
        animator.setInterpolator(new BounceInterpolator());
        animator.start();
二,旋转动画的实现,
     1.  RotateAnimation animation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
      animation.setDuration(1000);
       animation.setInterpolator(new AccelerateInterpolator());
       imageView.startAnimation(animation);
2. ObjectAnimator.ofFloat(imageView,"Rotation",0,360).setDuration(1000).start();
三,透明度动画
     1.AlphaAnimation alpha = new AlphaAnimation(1.0f,0.2f);
        alpha.setDuration(1000);
        alpha.setFillAfter(true);
        imageView.startAnimation(alpha);
     2. ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.2f).setDuration(1000).start();
四,缩放动画
     1. ScaleAnimation scale = new ScaleAnimation(0.5f, 3.0f, 0.5f, 3.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
       scale.setDuration(1000);
       scale.setFillAfter(true);
      scale.setInterpolator(new BounceInterpolator());
       imageView.startAnimation(scale);
     2.
     ObjectAnimator.ofFloat(imageView, "scaleX", 0.5F, 3.0f).setDuration(1000).start();
     ObjectAnimator.ofFloat(imageView, "scaleY", 0.5F, 3.0f).setDuration(1000).start();

五,动画集合(属性动画)
     1.    第一种方式
        ObjectAnimator.ofFloat(imageView, "scaleX", 0.5F, 3.0f).setDuration(1000).start();
       ObjectAnimator.ofFloat(imageView, "scaleY", 0.5F, 3.0f).setDuration(1000).start();
       ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.2f).setDuration(1000).start();
      第二种方式
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 3.0f);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 3.0f);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.2f);
        ObjectAnimator.ofPropertyValuesHolder(imageView, holder1, holder2, holder3).setDuration(1000).start();
第三种:
    public void setScaleAnimBig(View view) {
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view"ScaleX"1.0f1.05f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view"ScaleY"1.0f1.05f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setTarget(view);
        animatorSet.playTogether(objectAnimator1);
        animatorSet.playTogether(objectAnimator2);
//        animatorSet.playSequentially();按照次序来变化
        animatorSet.setDuration(300);
        animatorSet.start();
    }

矢量动画和属性动画的使用:
矢量动画的xml文件
Animator animator = AnimatorInflater.loadAnimator(this, R.anim.rotation1);
        animator.setTarget(imageView);
        animator.start();

0 0