Android动画学习总结(三)------- property animation

一:基础详解

What is propery animation:

 

     像谷歌原话的解释: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:
    相对于父控件左上角的位置
  • rotationrotationX,
    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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值