Android自定义View实现动画效果详解

帧动画

帧动画就是给定一个完整动画的所有关键帧,由大脑想象中间的变化过程的一种动画。

<?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/ic_loading_0" android:duration="100"/>
 <item android:drawable="@drawable/ic_loading_1" android:duration="100"/>
 <item android:drawable="@drawable/ic_loading_2" android:duration="100"/> 
 <item android:drawable="@drawable/ic_loading_3" android:duration="100"/> 
 <item android:drawable="@drawable/ic_loading_4" android:duration="100"/> 
 <item android:drawable="@drawable/ic_loading_5" android:duration="100"/> 
 <item android:drawable="@drawable/ic_loading_6" android:duration="100"/>
 <item android:drawable="@drawable/ic_loading_7" android:duration="100"/>
</animation-list>

将这个xml文件放到drawable文件夹下,就可以引用上了。

补间动画

补间动画就是指定动画开始和结束的状态,中间的变化,由计算机自动补齐。补间动画有4种类型,平移动画(Translate)、透明度动画(Alpha)、旋转动画(Rotate)、缩放动画(Scale)。

旋转动画

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
 android:fillAfter="true"
 android:fromDegrees="0"
 android:pivotX="50%" 
 android:pivotY="50%"
 android:repeatCount="1"
 android:toDegrees="180">
</rotate>

透明度动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
 android:fromAlpha="1.0"
 android:toAlpha="0.5">
</alpha>

平移动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
 android:fromXDelta="0"
 android:fromYDelta="0"
 android:toXDelta="100%"
 android:toYDelta="100%">
</translate>

缩放动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
 android:fromXScale="1.0"
 android:fromYScale="1.0"
 android:toXScale="1.2"
 android:toYScale="1.2"
 android:pivotX="50%"
 android:pivotY="50%">
</scale>

属性动画

属性动画,就是监听一个属性的变化值,来完成的动画。简言之,就是不断修改该对象的某个属性值,然后让动画框架来监听它。

ValueAnimator

ValueAnimator.ofFloat(0f, 1f)

创建一个从0逐渐变化到1点动画。

ObjectAnimator

new ObjectAnimator().addListener(new AnimatorListenerAdapter() {
 @Override
 public void onAnimationCancel(Animator animation) {
 super.onAnimationCancel(animation);
 }

 @Override
 public void onAnimationEnd(Animator animation) {
 super.onAnimationEnd(animation);
 }

 @Override
 public void onAnimationRepeat(Animator animation) {
 super.onAnimationRepeat(animation);
 }

 @Override
 public void onAnimationStart(Animator animation) {
 super.onAnimationStart(animation);
 }

 @Override
 public void onAnimationPause(Animator animation) {
 super.onAnimationPause(animation);
 }

 @Override
 public void onAnimationResume(Animator animation) {
 super.onAnimationResume(animation);
 }
});

监听动画的状态。

TypeEvaluator估值器

public interface TypeEvaluator<T> {

 /**
 * This function returns the result of linearly interpolating the start and end values, with
 * <code>fraction</code> representing the proportion between the start and end values. The
 * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
 * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
 * and <code>t</code> is <code>fraction</code>.
 *
 * @param fraction The fraction from the starting to the ending values
 * @param startValue The start value.
 * @param endValue The end value.
 * @return A linear interpolation between the start and end values, given the
 * <code>fraction</code> parameter.
 */
 public T evaluate(float fraction, T startValue, T endValue);

}

使用估值器来计算两个边缘状态的所有中间值。

ObjectAnimator.ofObject(target, propertyName, evaluator, values);

target参数为监听的对象,propertyName为监听的该对象的某个属性的名字,也可以监听该属性的setXxx()方法。evaluator传入一个估值器,values为你要估值的所有关键的点的值,然后通过这些给定的点,来估计一定时间内这个点在中间时刻的值。

Interpolator插值器

public interface Interpolator extends TimeInterpolator {
 // A new interface, TimeInterpolator, was introduced for the new android.animation
 // package. This older Interpolator interface extends TimeInterpolator so that users of
 // the new Animator-based animations can use either the old Interpolator implementations or
 // new classes that implement TimeInterpolator directly.
}

用它的实现类来决定动画的变化速率,比如先快后慢、先慢后快、匀速等。比较常用的有LinearInterpolator(匀速)、AccelerateInterpolator(加速)、DecelerateInterpolator(减速)。ValueAnimator和ObjectAnimator都可以通过调用

public abstract void setInterpolator(TimeInterpolator value);

设置插值器,因为这个方法是Animator抽象类的。

作者:dora

%s 个评论

要回复文章请先登录注册