android_动画总结

来源:互联网 发布:淘宝走最红的项目 编辑:程序博客网 时间:2024/05/21 10:48
1.View Animation 包括Tween Animation(补间动画)和Frame Animation(逐帧动画);
1 Tween Animation
1.1.1scale
    属性
    android:fromXScale    起始的X方向上相对自身的缩放比例,浮点值,例1.0无变化,0.5缩小一倍,2.0放大一倍    android:toXScale      结尾的X方向上相对自身的缩放比例,浮点值;    android:fromYScale    起始的Y方向上相对自身的缩放比例,浮点值,    android:toYScale      结尾的Y方向上相对自身的缩放比例,浮点值;    android:pivotX        缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式        当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点        如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点        如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标    android:pivotY        缩放起点Y轴坐标,取值及意义跟android:pivotX一样。
    构造
    ScaleAnimation(Context context, AttributeSet attrs)  从XML文件加载动画,基本用不到    ScaleAnimation(float fromX, float toX, float fromY, float toY)    ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)    ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType,             float pivotYValue)
    例
    scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);     scaleAnim.setDuration(700);     tv.startAnimation(scaleAnim);  
1.1.2.alpha
    属性
    android:fromAlpha     动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明    android:toAlpha       动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
    构造
    AlphaAnimation(Context context, AttributeSet attrs)  同样,从本地XML加载动画,基本不用    AlphaAnimation(float fromAlpha, float toAlpha)
    例
    alphaAnim = new AlphaAnimation(1.0f,0.1f);      alphaAnim.setDuration(3000);      alphaAnim.setFillBefore(true);  
1.1.3.ratate
    属性
    android:fromDegrees   开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数    android:toDegrees     结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数    android:pivotX        缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p    android:pivotY        缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p
    构造
    RotateAnimation(Context context, AttributeSet attrs)  从本地XML文档加载动画,同样,基本不用    RotateAnimation(float fromDegrees, float toDegrees)    RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)    RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float     pivotYValue)
    例
    rotateAnim = new RotateAnimation(0, -650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);      rotateAnim.setDuration(3000);      rotateAnim.setFillAfter(true);  
1.1.4.translate
    属性
    android:fromXDelta    起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p    android:fromYDelta    起始点Y轴从标,可以是数值、百分数、百分数p 三种样式;    android:toXDelta      结束点X轴坐标    android:toYDelta      结束点Y轴坐标
    构造
    TranslateAnimation(Context context, AttributeSet attrs)  同样,基本不用    TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)    TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue,         int toYType, float toYValue)
    例
    translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,               Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);      translateAnim.setDuration(2000);      translateAnim.setFillBefore(true);  
  公共属性
    android:duration      动画持续时间,以毫秒为单位    android:fillAfter     如果设置为true,控件动画结束时,将保持动画最后时的状态    android:fillBefore    如果设置为true,控件动画结束时,还原到开始动画前的状态    android:fillEnabled   与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态    android:repeatCount   重复次数    android:repeatMode    重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍    android:interpolator  设定插值器
1.1.5.set

1.1.6.使用

     xml     布局动画:Animation scaleAnimation= AnimationUtils.loadAnimation(this, R.anim.scaleanim);               tv.startAnimation(scaleAnimation);       属性动画:ObjectAnimator scaleAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this,     R.animator.scale_object_animator);
1.1.7.Interpolator
    AccelerateDecelerateInterpolator   在动画开始与介绍的地方速率改变比较慢,在中间的时候加速    AccelerateInterpolator             在动画开始的地方速率改变比较慢,然后开始加速    AnticipateInterpolator             开始的时候向后然后向前甩    AnticipateOvershootInterpolator    开始的时候向后然后向前甩一定值后返回最后的值    BounceInterpolator                 动画结束的时候弹起    CycleInterpolator                  动画循环播放特定的次数,速率改变沿着正弦曲线    DecelerateInterpolator             在动画开始的地方快然后慢    LinearInterpolator                 以常量速率改变    OvershootInterpolator              向前甩一定值后再回到原来位置
1.1.8 自定义差值器
    public class MyInterploator implements TimeInterpolator {          @Override          public float getInterpolation(float input) {                // TODO          }      }  
1.1.9 Evaluator
    自定义Evaluator        例 public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values);               public class MyEvaluator implements TypeEvaluator<Integer> {                  @Override                  public Integer evaluate(float fraction, Integer startValue, Integer endValue) {                     return null;  // return startvalue+fraction*(end - start)             }             }  
1.2 Frame Animation(逐帧动画)
   例
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"          android:oneshot="false">          <item android:drawable="@drawable/f1" android:duration="300" />          <item android:drawable="@drawable/f2" android:duration="300" />          <item android:drawable="@drawable/f3" android:duration="300" />          <item android:drawable="@drawable/f4" android:duration="300" />      </animation-list>    //使用    image.setBackgroundResource(R.anim.frame);      AnimationDrawable anim = (AnimationDrawable)    image.getBackground();      anim.start();  


2.Property Animator包括ValueAnimator和ObjectAnimation;


2.1 ObjectAnimator
2.1.1 Translation
    ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "translationX", -500f, 0f);    ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "translationY", -500f, 0f);    AnimatorSet set2 = new AnimatorSet();    set2.play(anim2).before(set3) ;    set.play(anim).before(set2);    set.start();
2.1.2 Alpha
   ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "alpha", 1f, 0f);
2.1.3 Rotate
    ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "rotationX", 180f, 0f);    ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "rotationY", 0f, 180f);    ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "rotation", 0, 180, 0);  
2.1.4 Scale
    ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "scaleX", 0.5f);      ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "scaleY", 1f); 
2.1.5 自定义属性
属性动画自定义属性
2.1.6 propertyValuesHolder
    public void propertyValuesHolder(View view)          {              PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,                      0f, 1f);              PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,                      0, 1f);              PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,                      0, 1f);              ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();          }  
2.1.7 AnimatorSet
     animSet.playTogether(anim1, anim2);      animSet.start();
2.1.8 ViewPropertyAnimator 多属性动画
  viewPropertyAnimator animate = imageview.animate();//该对象没有setRepeat的方法          //通过一些动画属性来设置 组合成类似set的效果          animate.alpha(0);          animate.rotationX(50);          animate.translationXBy(500);          animate.scaleX(1.5f);          animate.scaleY(1.5f);          animate.setInterpolator(new BounceInterpolator());          animate.setDuration(2000);          animate.start(); 
2.2 ValueAnimator
  例
    ValueAnimator animator = ValueAnimator.ofInt(0,400);      animator.setDuration(1000);       animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {          @Override          public void onAnimationUpdate(ValueAnimator animation) {              int curValue = (int)animation.getAnimatedValue();              tv.layout(curValue,curValue,curValue+tv.getWidth(),curValue+tv.getHeight());        }      });      animator.start();  
   类型
    public static ValueAnimator ofInt(int... values)      public static ValueAnimator ofFloat(float... values)  
   常用方法
   设置动画时长,单位是毫秒        ValueAnimator setDuration(long duration)      获取ValueAnimator在运动时,当前运动点的值        Object getAnimatedValue();      开始动画        void start()      设置循环次数,设置为INFINITE表示无限循环        void setRepeatCount(int value)      设置循环模式  value取值有RESTART,REVERSE,        void setRepeatMode(int value)      取消动画        void cancel()  
    监听器
    监听器一:监听动画变化时的实时值
    public static interface AnimatorUpdateListener {              void onAnimationUpdate(ValueAnimator animation);          } 

        添加方法为:

public void addUpdateListener(AnimatorUpdateListener listener)  

    监听器二:监听动画变化时四个状态  
        public static interface AnimatorListener {              void onAnimationStart(Animator animation);              void onAnimationEnd(Animator animation);              void onAnimationCancel(Animator animation);              void onAnimationRepeat(Animator animation);          }
    监听器三:AnimatorListenerAdapter
    移除监听
        移除AnimatorUpdateListener
      void removeUpdateListener(AnimatorUpdateListener listener);        void removeAllUpdateListeners();  
        移除AnimatorListener  
     void removeListener(AnimatorListener listener);       void removeAllListeners();    
2.3 xml 使用
      Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex);        anim.setTarget(mMv);        anim.start();  


3 布局动画(Layout Animations)

LayoutTransition transition = new LayoutTransition();      transition.setAnimator(LayoutTransition.CHANGE_APPEARING,              transition.getAnimator(LayoutTransition.CHANGE_APPEARING));      transition.setAnimator(LayoutTransition.APPEARING,              null);      transition.setAnimator(LayoutTransition.DISAPPEARING,              null);      transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,              null);      mGridLayout.setLayoutTransition(transition);      

就这样

1 0