Android动画小结
原创
©著作权归作者所有:来自51CTO博客作者不死鸟jgc的原创作品,请联系作者获取转载授权,否则将追究法律责任
Android动画分为3种:View Animation 、Drawable Animation 、Property Animation。其中View动画又叫Tween动画,补间动画,Drawable动画又叫Frame动画,帧动画。
View动画
基类是Animation。分为渐变—AlphaAnimation,旋转—RotateAnimation,伸缩—ScaleAnimation,平移—TranslateAnimation。
可以结合插值器,AnimationSet实现复杂效果。
注意:View动画只能在View上使用,只是改变界面显示,并不是真正改变View。
Drawable动画
使用示例如下
<?xml version="1.0" encoding="utf-8"?>
<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();
Property动画
真正改变一个Object,API11(Android 3.0)引入
常用类:ObjectAnimator 动画的执行类,ValueAnimator 动画的执行类
ObjectAnimator使用示例如下:
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();
注意:只有实现了Setter和getter的属性才可以使用
ValueAnimator使用示例如下:
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0));
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
{
// fraction = t / duration
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue)
{
Log.e(TAG, fraction * 3 + "");
// x方向200px/s ,则y方向0.5 * 10 * t
PointF point = new PointF();
point.x = 200 * fraction * 3;
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
return point;
}
});
valueAnimator.start();
ValueAnimator并没有在属性上做操作,所以不需要操作的对象的属性一定要有getter和setter方法,你可以自己根据当前动画的计算值,来操作任何属性。