Android动画——属性动画

    xiaoxiao2023-03-24  4

    1、概述 前面我们已经说过,Tween Animation只能应用于View对象,而且只支持一部分属性,而想要实现更复杂的操作就无能为力了,因此Property Animation应运而生了,Property Animation的功能可以说是最强大的(从某种角度看,属性动画是补间动画的增强版),属性动画需要定义如下几方面属性: 动画持续时间:该属性的默认值是300毫秒。在属性动画资源文件中通过android:duration属性指定。 动画差值方式:该属性的作用与补间动画中差值属性的作用基本类似,用来定义动画的变化率。在属性动画资源文件中通过android:interpolator属性指定。 动画重复次数:指定动画重复播放的次数,在属性动画资源文件中通过android:repeatCount属性指定。 动画重复模式:指定动画播放结束后、下次播放时,是从开始帧再次播放到结束帧,还是从结束帧反向播放到开始帧。在属性动画资源文件中通过android:repeatMode属性指定。 动画集:将多个属性动画合并成一组,既可以让这组属性动画按次序播放,也可以让这组动画同时播放,在属性动画资源文件中通过<set .../>元素来组合。 帧刷新频率:指定每隔多长时间播放一帧,默认值是10毫秒。 2、相关API Animator:它提供了创建属性动画的基类,基本上不会直接使用该类,通常要继承它并重写相关方法。 ValueAnimator :属性动画主要的时间引擎,它负责计算各个帧的属性值。它定义了属性动画的绝大部分核心功能:计算各帧的相关属性值,负责处理更新事件,按属性值的类型控制计算规则。属性动画主要由两方面组成:1、计算各帧的相关属性值;2、为指定对象设置这些计算后的值。ValueAnimator只负责第一部分,所以程序员必须根据ValueAnimator计算并监听值更新来更新对象的相关属性值。 ObjectAnimator:它是ValueAnimator子类,运行对指定对象的属性执行动画。 AnimatorSet:它是Animator子类,用于组合多个Animator,并指定它们的播放次序。 同时,属性动画还需要利用一个Evaluator(计算器),该工具类控制属性动画如何计算属性值,共有如下几种Evaluator: IntEvaluator:用于计算int型属性值; FloatEvaluator:用于计算float型属性值; IntEvaluator:用于计算以16进制形式表示的颜色值; TypeEvaluator:计算器接口,可以通过实现该接口类来实现自定义计算器。 3、使用ValueAnimator实现动画 使用ValueAnimator动画可按如下步骤: 调用ValueAnimator的ofInt()、ofFloat()或ofObject()方法创建ValueAnimator实例。 调用ValueAnimator的setXxx()方法设置动画持续时间、差值方式、重复次数等。 调用ValueAnimator的start()方法启动动画。 为ValueAnimator注册AnimatorUpdateListener监听器,在监听器中监听ValueAnimator变化的值,并将这些值应用到指定对象。 看个例子: animator = ValueAnimator.ofFloat(0f, 300); animator.setDuration(4000); animator.start(); 这段代码仅仅是计算了动画变化过程值,并没有把值应用到任何对象上,如何要显示出效果,还要注册一个监听器——AnimatorUpdateListener,通过getAnimatedValue()方法获取当前帧的值,并将该值应用到指定对象上: animator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mImageView.setTranslationX( (Float)animation.getAnimatedValue()); } }); 效果如下: 当然,我们也可以自定义一个Evaluator计算器,例如: animator=ValueAnimator.ofObject(new CustomEvaluatour(), startVal,endVal); animator.setDuration(1000); animator.start(); 4、使用ObjectAnimator创建动画 ObjectAnimator继承ValueAnimator,因此可以它能够直接将ValueAnimator在动画过程中计算出来的值应用到指定属性上,所以使用ObjectAnimator就不需要注册AnimatorUpdateListener监听器了,下面我就以鸿洋大神的例子作为说明了 布局文件如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:onClick="changeAlpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/circle_photo" /> </RelativeLayout> Activity代码: public class ObjectAnimatorActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_object_animator); } public void changeAlpha(final View view) { ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 1.0F, 0.2F) .setDuration(500);// anim.start(); } } 效果如下: 与ValueAnimator不同的是,我们在使用ObjectAnimator还要注意以下几点: 如果调用了ObjectAnimator提供的ofInt()、ofFloat()或ofObject()方法,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。当对于属性值,只设置一个的时候(通常需要开始值和结束值),该值会被认为是结束值,当前对象该属性的值为开始值(getPropName反射获取)。该对象应该为该属性提供一个getter方法,getter的返回值作为开始值。 如果操作对象的该属性方法里面,比如上例的setAlpha(),如果内部没有调用view的重绘,则还需要在onAnimationUpdate()事件监听方法中来刷新屏幕的显示: anim.addUpdateListener(new AnimatorUpdateListener()           {               @Override               public void onAnimationUpdate(ValueAnimator animation)               {   //              view.postInvalidate();   //              view.invalidate();               }           });   上面的例子,设置的操作的属性只有一个,如果希望一个动画能够让一个View支持多个动画怎么办?可能会说使用AnimatorSet就行了,但是只使用ObjectAnimator能否实现呢?下面实现下: public void changeAlpha(final View view) { ObjectAnimator anim = ObjectAnimator.ofFloat(view, "xmr", 1.0F, 0.2F) .setDuration(5000); anim.start(); anim.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (Float) animation.getAnimatedValue(); view.setAlpha(value); view.setScaleX(value); view.setScaleY(value); } }); } 因为我们要在下面设置透明度,所以把设置属性的那个字符串“alpha"随便改一个该对象没有的属性,只需要它按照时间插值和持续时间计算的那个值,自己手动调用即可: 实际上使用propertyValuesHolder也可以实现一个动画更改多个效果: public void changeAlpha(View view) { 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(5000).start(); } 5、监听动画的事件 动画一般都是做一些辅助效果,比如我要删除个元素,我可能希望是个淡出的效果,但是最终还是要删处,并不是你透明度没有了,还占着位置,所以我们需要知道动画如何结束。 所以我们可以添加一个动画的监听: ublic void fadeOut(View view) { ObjectAnimator anim = ObjectAnimator.ofFloat(mImageView, "alpha", 0.5f) .setDuration(5000); anim.start(); anim.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { ViewGroup parent = (ViewGroup) mImageView.getParent(); if (parent != null) parent.removeView(mImageView); } @Override public void onAnimationCancel(Animator animation) { } }); } 这个监听器可以监听动画的开始、结束、被取消、重复等事件,但是有时候只要知道结束就行了,那你可以使用AnimatorListenerAdapter,AnimatorListenerAdapter继承了AnimatorListener接口,然后空实现了所有的方法: anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { ViewGroup parent = (ViewGroup) mImageView.getParent(); if (parent != null) parent.removeView(mImageView); } }); 6、AnimatorSet的使用 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="togetherRun" android:text="多动画同时执行" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="playWithAfter" android:text="多动画按次序执行" /> </LinearLayout> <ImageView android:id="@+id/iv_anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/circle_photo" /> </RelativeLayout> 代码: public class AnimatorSetActivity extends Activity { private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animator_set); mImageView = (ImageView) findViewById(R.id.iv_anim); } public void togetherRun(View view) { ObjectAnimator anim1 = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.0f, 2.0f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mImageView, "scaleY", 1.0f, 2.0f); AnimatorSet animSet = new AnimatorSet(); animSet.setDuration(5000); animSet.setInterpolator(new LinearInterpolator()); //两个动画同时执行 animSet.playTogether(anim1, anim2); // animSet.playSequentially(items) animSet.start(); } public void playWithAfter(View view) { float cx = mImageView.getX(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.0f, 2.0f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mImageView, "scaleY", 1.0f, 2.0f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(mImageView, "x", cx , 0.0f); ObjectAnimator anim4 = ObjectAnimator.ofFloat(mImageView, "x", cx); /** * anim1,anim2,anim3同时执行 * anim4接着执行 */ AnimatorSet animSet = new AnimatorSet(); animSet.play(anim1).with(anim2); animSet.play(anim2).with(anim3); animSet.play(anim4).after(anim3); animSet.setDuration(2000); animSet.start(); } } 效果如下: 有一点注意:animSet.play().with();也是支持链式编程的,但是不要想着狂点,比如 animSet.play(anim1).with(anim2).before(anim3).before(anim5); 这样是不行的,系统不会根据你写的这一长串来决定先后的顺序,所以麻烦你按照上面例子的写法,多写几行 好了,先说这么多吧 参考: http://blog.csdn.net/lmj623565791/article/details/38067475 http://www.2cto.com/kf/201401/270169.html http://blog.csdn.net/xushuaic/article/details/40424379 http://blog.csdn.net/sin90lzc/article/details/7517837 http://blog.csdn.net/xushuaic/article/details/40322345 http://blog.csdn.net/CHZiroy/article/details/45058789
    转载请注明原文地址: https://ju.6miu.com/read-1202756.html
    最新回复(0)