为了账号安全,请及时绑定邮箱和手机立即绑定

Android动画解析(一)

标签:
Android

一、动画分类:


1、两种类型:View Animation(视图动画)、Property Animator(属性动画)

( 或者分为View动画(Tween Animation)、逐帧动画、属性动画三种)。

  • View Animation包括Tween Animation(补间动画)和Frame Animation(逐帧动画); 

  • Property Animator包括ValueAnimator和ObjectAnimation;

2、区别:

  • 引入时间不同:View Animation是API Level 1就引入的。Property Animation是API Level 11引入的,即Android 3.0才开始有Property Animation相关的API。 

  • 所在包名不同:View Animation在包android.view.animation中。而Property Animation API在包 android.animation中。 

  • 动画类的命名不同:View Animation中动画类取名都叫XXXXAnimation,而在Property Animator中动画类的取名则叫XXXXAnimator

3、属性动画和补间动画的区别:

属性动画能够只针对控件的某一个属性来做动画,所以也就造就了他能单独改变控件的某一个属性的值!比如颜色!

补间动画虽能对控件做动画,但并没有改变控件内部的属性值。

二、逐帧动画:


将一系列图片按照一定的顺序播放(实例略)

三、View动画:

平移、缩放、旋转、透明度动画

5b780ac800015d2008880208.jpg

1、常用属性:

-TranslateAnimation -> 移动View

    |-<translate>
        |-android:fillAfter -> 为true表示动画保存最后时的状态
        |-android:duration -> 表示动画持续的时间
        |-android:fromXDelta -> 表示 x 的起始值
        |-android:toXDelta -> 表示 x 的结束值
        |-android:fromYDelta -> 表示 y 的起始值
        |-android:toYDelta -> 表示 y 的结束值
-ScaleAnimation -> 放大或者缩小View
    |-<scale>
        |-android:duration -> 表示动画持续的时间
        |-android:fromXScale -> 表示水平方向缩放的起始值
        |-android:fromYScale -> 表示竖直方向缩放的起始值
        |-android:pivotX -> 表示缩放中心点的 X 坐标
        |-android:pivotY -> 表示缩放中心点的 Y 坐标
        |-android:toXScale -> 表示水平方向缩放的结束值
        |-android:toYScale -> 表示竖直方向缩放的结束值
-RotateAnimation -> 旋转View
    |-<rotate>

        |-android:duration -> 表示动画持续的时间

        |-android:fromDegrees -> 旋转开始的角度,正数顺时针,负数逆时针

        |-android:toDegrees -> 旋转结束的角度
        |-android:pivotX -> 旋转中心点的 X 坐标
        |-android:pivotY -> 旋转中心点的 Y 坐标
-AlphaAnimation -> 改变View的透明度
    |-<alpha>
        |-android:duration -> 表示动画持续的时间
        |-android:fromAlpha -> 透明度的起始值
        |-android:toAlpha -> 透明度的结束值



2、其他属性:

  • android:interpolator -> 插值器,影响动画的速度

  •        默认值 -> @android:anim/accelerate_decelerate_interpolator

  • android:shareInterpolator -> 集合所有动画是否使用同一插值器

  • android:fillAfter -> 动画结束后View是否停留在结束的位置

  • android:startOffset -> 动画多少秒之后执行

  • android:repeatCount -> 重复的次数

  • android:repeatMode -> 重复的模式,默认为restart,即重头开始重新运行,reverse即从结束开始向前重新运行(必须和repeatCount一起使用)

3、相关代码:

  • ScaleAnimation(Context context, AttributeSet attrs)  

  • 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)

  • AlphaAnimation(Context context, AttributeSet attrs)  

  • AlphaAnimation(float fromAlpha, float toAlpha)

  • RotateAnimation(Context context, AttributeSet attrs)  

  • 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)

  • 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)


AnimationSet:

  • AnimationSet(Context context, AttributeSet attrs)  

  • AnimationSet(boolean shareInterpolator)  shareInterpolator取值true或false,取true时,指在AnimationSet中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。

  • public void addAnimation (Animation a)


4、View动画特殊使用场景

(1)LayoutAnimation

LayoutAnimation作用于ViewGroup,ViewGroup指定一个动画,子元素就具有这种动画效果

  • android:delay:动画时间延迟

  • android:animationOrder:子元素动画顺序:normal 顺序显示,reverse 逆向显示,random随机播放 

  • android:animation 指定具体入场动画    android:layoutAnimation=“@anim/..."

(2)Activity的切换

overridePendingTransition(int enterAnim,int exitAnim)    用于startActiivty或者finish的后面

Fragment可以通过FragmentTransaction中的setCustomAnimations方法来添加切换动画。


三、ValueAnimator:

1、开始动画:

ValueAnimator没有跟任何的控件相关联,那也正好说明ValueAnimator只是对值做动画运算,而不是针对控件。

  1. <span style='font-family: "Microsoft YaHei"; font-size: 18px;'>ValueAnimator animator = ValueAnimator.ofInt(0,400);    

  2. animator.setDuration(1000);    

  3. animator.start();    

  4. </span>  

2、添加监听:(监听当前进度具体数值)


  1. <span style='font-family: "Microsoft YaHei"; font-size: 18px;'>  ValueAnimator animator = ValueAnimator.ofInt(0400);  

  2.         animator.setDuration(1000);  

  3.         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  

  4.             @Override  

  5.             public void onAnimationUpdate(ValueAnimator animation) {  

  6.                 int curValue = (int) animation.getAnimatedValue();  

  7.                         

  8.             }  

  9.         });  

  10.         animator.start();</span>  

3、根据监听的进度进行view的更新

  1. ValueAnimator animator = ValueAnimator.ofInt(0, 400);  

  2.       animator.setDuration(1000);  

  3.       animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  

  4.           @Override  

  5.           public void onAnimationUpdate(ValueAnimator animation) {  

  6.               int curValue = (int) animation.getAnimatedValue();  

  7.               //更新进度条进度  

  8.               progressBar.setProgress(curValue);  

  9.           }  

  10.       });  

  11.       animator.start();  

4、常用方法:


  1. public static ValueAnimator ofInt(int... values)    

  2. public static ValueAnimator ofFloat(float... values)    

参数类型都是可变参数长参数,所以我们可以传入任何数量的值;传进去的值列表,就表示动画时的变化范围

  1. /**  

  2.   * 设置动画时长,单位是毫秒   

  3.   */  

  4.  ValueAnimator setDuration ( long duration)  

  5.  /**  

  6.   * 获取ValueAnimator在运动时,当前运动点的值   

  7.   */  

  8.  Object getAnimatedValue ();  

  9.  /**  

  10.   * 开始动画   

  11.   */  

  12.  void start ()  

  13.  /**  

  14.   * 设置循环次数,设置为INFINITE表示无限循环   

  15.   */  

  16.  void setRepeatCount ( int value)  

  17.  /**  

  18.   * 设置循环模式   

  19.   * value取值有RESTART,REVERSE,   

  20.   */  

  21.  void setRepeatMode ( int value)  

  22.  /**  

  23.   * 取消动画   

  24.   */  

  25.  void cancel ()  

(1) setRepeatCount(int value)用于设置动画循环次数,设置为0表示不循环,设置为ValueAnimation.INFINITE表示无限循环。 

(2) setRepeatMode(int value)用于设置循环模式,取值为ValueAnimation.RESTART时,表示正序重新开始,当取值为ValueAnimation.REVERSE表示倒序重新开始。 

  1. /** 

  2.  * 延时多久时间开始,单位是毫秒  

  3.  */  

  4. public void setStartDelay ( long startDelay)  

  5. /** 

  6.  * 完全克隆一个ValueAnimator实例,包括它所有的设置以及所有对监听器代码的处理  

  7.  */  

  8. public ValueAnimator clone ()  

监听:


  1. /** 

  2.  * 监听器一:监听动画变化时的实时值  

  3.  */  

  4. public static interface AnimatorUpdateListener {  

  5.     void onAnimationUpdate(ValueAnimator animation);  

  6. }  

  7.   

  8. /** 

  9.  * 监听器二:监听动画变化时四个状态  

  10.  */  

  11. public static interface AnimatorListener {  

  12.     void onAnimationStart(Animator animation);  

  13.     void onAnimationEnd(Animator animation);  

  14.     void onAnimationCancel(Animator animation);  

  15.     void onAnimationRepeat(Animator animation);  

  16. }  

四个状态:start、end、cancel、repeat;

当动画开始时,会调用onAnimationStart(Animator animation)方法,

当动画结束时调用onAnimationEnd(Animator animation),

当动画取消时,调用onAnimationCancel(Animator animation)函数,

当动画重复时,会调用onAnimationRepeat(Animator animation)函数。 
取消监听:

  1. /** 

  2.  * 移除AnimatorUpdateListener  

  3.  */  

  4. void removeUpdateListener (AnimatorUpdateListener listener);  

  5. void removeAllUpdateListeners ();  

  6. /** 

  7.  * 移除AnimatorListener  

  8.  */  

  9. void removeListener (AnimatorListener listener);  

  10. void removeAllListeners ();  

5、ofObject 使用:

  1. public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values);    

它有两个参数,第一个是自定义的Evaluator,第二个是可变长参数,Object类型

例:

  1. public class Point {  

  2.        private int radius;  

  3.   

  4.        public Point(int radius) {  

  5.            this.radius = radius;  

  6.        }  

  7.   

  8.        public int getRadius() {  

  9.            return radius;  

  10.        }  

  11.   

  12.        public void setRadius(int radius) {  

  13.            this.radius = radius;  

  14.        }  

  15.    }  

  1. public class MyPointView extends View {  

  2.        public MyPointView(Context context) {  

  3.            super(context);  

  4.        }  

  5.   

  6.        public MyPointView(Context context, @Nullable AttributeSet attrs) {  

  7.            super(context, attrs);  

  8.        }  

  9.   

  10.        private Point mCurPoint;  

  11.   

  12.        @Override  

  13.        protected void onDraw(Canvas canvas) {  

  14.            super.onDraw(canvas);  

  15.            if (mCurPoint != null){  

  16.                Paint paint = new Paint();  

  17.                paint.setAntiAlias(true);  

  18.                paint.setColor(Color.RED);  

  19.                paint.setStyle(Paint.Style.FILL);  

  20.                canvas.drawCircle(300,300,mCurPoint.getRadius(),paint);  

  21.            }  

  22.        }  

  23.   

  24.        public void doPointAnim(){  

  25.            ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(),new Point(20),new Point(200));  

  26.            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  

  27.                @Override  

  28.                public void onAnimationUpdate(ValueAnimator animation) {  

  29.                    mCurPoint = (Point)animation.getAnimatedValue();  

  30.                    invalidate();  

  31.                }  

  32.            });  

  33.            animator.setDuration(1000);  

  34.            animator.setRepeatMode(ValueAnimator.RESTART);  

  35.            animator.setRepeatCount(100);  

  36.            animator.setInterpolator(new BounceInterpolator());  

  37.            animator.start();  

  38.        }  

  39.   

  40.        public class PointEvaluator implements TypeEvaluator<Point> {  

  41.            @Override  

  42.            public Point evaluate(float fraction, Point startValue, Point endValue) {  

  43.                int start = startValue.getRadius();  

  44.                int end  = endValue.getRadius();  

  45.                int curValue = (int)(start + fraction * (end - start));  

  46.                return new Point(curValue);  

  47.            }  

  48.        }  

  49.    }  

原文链接:http://www.apkbus.com/blog-847095-77491.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消