Android animation学习笔记之propery animation

    以前写博客都是用百度空间,总感觉不专业,访问量少,好友少,里边可运用的资源也少,所以昨天网上查了一下,觉得iteye社区还不错,就申请了一个iteye帐号,今天开始写博,希望大家多关注多交流,共同进步.

 

    最近一直在做中国最强音app的开发,好不容易闲了下来,觉得在做中国最强音app的时候动画方面的知识还不够扎实,所以这周腾出手来主要学习了animation和graphics方面的知识,现在先将animation方面的知识总结如下:

 

    Android框架提供了两种动画:property animation(android3.0以上会有)view animation.其中建议选property animation,因为它要更强大更高效,除了上边两种,你还可以运用drawable animation,它可以帮你导入drawable中的资源并且一个一个显示他们.

 

    ,Property animation:这种动画系统帮助你实现任何对象的动画,包括那些没有被添加到界面当中的对象.

    二,View animation:这是一种比较老的方式并且只能对Views进行使用,使用和设置起来比较容易.

    三,Drawable animation:像电影一样,一个drawable接一个进行播放.

 

What is propery animation:

扫描二维码关注公众号,回复: 685661 查看本文章

 

     像谷歌原话的解释:The property animation system is a robust framework that allows you to animate almost anything.在一定的时间内,property animation可以改变一个property(a field in an object)的值,比如位置,动画持续时间,动画开始时间等,来控制一个动画.

 

     Property animation的属性包括:

 

  • Duration:动画持续的时间,系统默认为300ms.
  • Time interpolation:动画插入器,LinearInterpolator动画以均匀的速率改变
  • Repeat count and mode,动画重复次数和返回状态,restart reverse.
  • Animation sets:可以将一系列动画放入一个集合中一起进行或者一个一个进行.
  • Frame refresh delay,可以设置动画的刷新时间,系统自定义为10ms.

How property animation works:

 

     // http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view

 

How property animation differs from view animation

 

     view animation 只可以操作是view的对象,并且只能对view进行rotate,scale,translatealpha操作.view animation另一个不能实现的功能就是it only modified where the View was drawn, and not the actual View itself,意思是它动画的时候其实那个view实际是不在显示的位置上的.

 

     The property animation system can animate Views on the screen by changing the actual properties in the View objects. In addition, Views also automatically call the invalidate() method to refresh the screen whenever its properties are changed. The new properties in the view class that facilitate property animations are:

 

  • translationX and translationY: 相对于父控件左上角的位置
  • rotation, rotationX, and rotationY: 旋转的中心点的坐标
  • scaleX and scaleY: 缩放的中心点的坐标
  • pivotX and pivotY: 中心点的坐标
  • x and y: 距离坐标
  • alpha: 透明度,值在0到1之间

How to accompish an animation:

 

    1,Multiple ObjectAnimator objects

ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

 

     2,One ObjectAnimator

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
 

     3,ViewPropertyAnimator

myView.animate().x(50f).y(100f);

How to Declaring Animation in XML:

 

    android开发允许你使用xml文件代替编程来运用动画,通过xml文件,你可以在多个activity中很容易的重用或重编辑你的动画.

    为了区分新的property animation 和旧的view animation,从android3.1以上,将文件名由res/anim改成res/animator, 在xml中可以设置三种动画属性:

用法如下:

<setandroid:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="x"
            android:duration="500"
            android:valueTo="400"
            android:valueType="intType"/>
        <objectAnimator
            android:propertyName="y"
            android:duration="500"
            android:valueTo="300"
            android:valueType="intType"/>
    </set>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1f"/></set>

 

代码中:

AnimatorSetset=(AnimatorSet)AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);set.setTarget(myObject);set.start();

 

 

Animation api overview:

 

     android.animation包中可以找到很多property animationapi,android.view.animation包中可以找到很多定义好的imterpolators.下边是propery animation system 中的组件.

 

     1,what is included in animator class

 

  •  ValueAnimator继承于animator,google 解释为The main timing engine for property animation that also computes the values for the property to be animated,就是每隔10ms计算图画的位置
  • ObjectAnimator继承于ValueAnimator,google 解释为A subclass of ValueAnimator that allows you to set a target object and object property to animate,ValueAnimator多出的功能就是不仅可以计算位置,还可以将图形的新的位置上刷新出来.
  • AnimatorSet,Provides a mechanism to group animations together so that they run in relation to one another就是把很多动画组合起来进行刷新显示.

 

     2,what and how to use Evaluators

     Evaluators tell the property animation system how to calculate values for a given property.IntEvaluator/FloatEvaluator/ArgbEvaluator/TypeEvaluator.

     Using a typeEvaluator:

public class FloatEvaluator implements TypeEvaluator {
	public Object evaluate(float fraction, Object startValue, Object endValue) {
		float startFloat = ((Number) startValue).floatValue();
		return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
	}
}

 

 

     3,what and how to use interpolator

    A time interpolator defines how specific values in an animation are calculated as a function of time.

 

  • AccelerateDecelerateInterpolator先加速后减速,
  • accelerateInterpolator一直加速
  • AnticipateInterpolator:表示开始的时候向后然后向前甩
  • OvershootInterpolator:表示向前甩一定值后再回到原来置,
  • BounceInterpolator:表示动画结束的时候弹起,
  • CycleInterpolator:表示动画循环播放特定的次数,速率改变沿着正弦曲线
  • DecelerateInterpolator:表示在动画开始的地方快然后慢
  • LinearInterpolator:表示以常量速率改变
  • AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值
  • TimeInterpolator,Aninterface that allows you to implement your own interpolator.

Using interpolators:

    AccelerateDecelerateInterpolator:

public float getInterpolation(float input) {
	return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}

 

    LinearInterpolator:

public float getInterpolation(float input) {
    return input;
}

 

 

Animating with ValueAnimator/Animating with ObjectAnimator:这两种animator的运用方法见demo,demo中还包括了AnimatorSetanimatorListener的用法.

 

Animating Layout changes to ViewGroup:

 

刚才提到,perporty animation不仅可以用到view,还可以用到wiewgroup.

 

  • APPEARING:view正子出现的状态
  • CHANGE_APPEARING :view正在出现的父控件的状态
  • DISAPPEARING :view正子消失的状态
  • CHANGE_DISAPPEARING :view正在消失的父控件的状态

猜你喜欢

转载自frand-feng.iteye.com/blog/1860973