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

这篇文章主要为大家详细介绍了Android如何通过自定义View实现动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

帧动画

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

  

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

补间动画

补间动画就是指定动画开始和结束的状态,中间的变化,由计算机自动补齐。补间动画有4种类型,平移动画(Translate)、透明度动画(Alpha)、旋转动画(Rotate)、缩放动画(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 { /** * This function returns the result of linearly interpolating the start and end values, with * fraction representing the proportion between the start and end values. The * calculation is a simple parametric calculation: result = x0 + t * (x1 - x0), * where x0 is startValue, x1 is endValue, * and t is fraction. * * @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 *         fraction 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抽象类的。

到此这篇关于Android自定义View实现动画效果详解的文章就介绍到这了,更多相关Android自定义View内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Android自定义View实现动画效果详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动