星空网 > 软件开发 > 操作系统

android 动画分类

       Android 平台提供了一套完整的动画框架,在Android3.0之前有两种动画Tween Animation(补间动画)Frame Animation(帧动画),

对应SDK中的View Animation和Drawable Animation。

在Android3.0之后,新增了一种动画Property Animation(属性动画)。


 

一: 补间动画(res/anim/ )

  Tween Animation可以对view实现一系列的转换,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。可以通过 代码  和  定义,代码中定义用: AlphaAnimation…

特点:

Tween Animation只能应用于View对象,并且只支持一部分属性。它并不会改变属性的值,只是改变了View对象绘制的位置。比如,一个按钮在动画过后,不再原来的位置,但是触发点击事件的任然是原坐标。

,可以是渐变<alpha>, 伸缩<scale>, 移动<translate>, 旋转<rotate>, 或者 <set>。<set>是一个动画集,可以包含前面的一个或多个。

 1 <!-- --> 2 <set android:shareInterpolator="false"> 3   <scale 4     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 5     android:fromXScale="1.0" 6     android:toXScale="1.4" 7     android:fromYScale="1.0" 8     android:toYScale="0.6" 9     android:pivotX="50%"10     android:pivotY="50%"11     android:fillAfter="false"12     android:duration="700" />13 </set>

 代码中使用:


1 ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);2 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);3 spaceshipImage.startAnimation(hyperspaceJumpAnimation);

     定义动画的三个步骤 1. 创建动画对象 2. 设置时长 3 . 开启动画

 Interpolators 插值器

可以让动画按照一定的频率运动,实现加速、减速、重复、回弹等效果。

res/anim 下,并定义android:interpolator="@android:anim/.."来使用

二: 帧动画(res/drawable/)

  Frame Animation是一系列的图片按顺序显示,来模拟动画的效果。 代码中通过AnimationDrawable来定义。

1 <animation-list ="http://schemas.android.com/apk/res/android"2   android:oneshot="true">3   <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />4   <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />5   <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />6 </animation-list>

必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。

 1 protected void onCreate(Bundle savedInstanceState) { 2     super.onCreate(savedInstanceState); 3     setContentView(R.layout.main); 4     imageView = (ImageView) findViewById(R.id.imageView1); 5     imageView.setBackgroundResource(R.drawable.drawable_anim); 6     anim = (AnimationDrawable) imageView.getBackground(); 7   } 8  9   public boolean onTouchEvent(MotionEvent event) {10     if (event.getAction() == MotionEvent.ACTION_DOWN) {11       anim.stop();12       anim.start();13       return true;14     }15     return super.onTouchEvent(event);16   }

注意事项:

  • 要在代码中调用Imageview的setBackgroundResource方法,如果直接在
  • 在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次
  • 最后一点是SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。

三 : 属性动画(res/animator/)//3.0之后新加的特性

    Property Animation更改的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。它不止可以应用于View,还可以应用于almost任何对象,在任何时候(即使没有draws to the screen)。Property Animation只是表示一个值在一段时间内的改变,当值改变时要做什么事情完全是你自己决定的。

Property Animation允许我们定义的属性:

  • Duration :时长 ,default length is 300 ms.
  • Time interpolation :属性值的计算方式,如先快后慢
  • Repeat count and behavior:重复次数与方式
  • Animator sets :动画集合,存放几个动画,这些动画可以同时播放也可以设置不同的偏移
  • Frame refresh delay:多少时间刷新一次 ,default is 10 ms ,受系统进程调度与硬件的影响

①: ValueAnimator :包含Property Animation动画的所有核心功能,如动画时间,开始、结束属性值,相应时间属性值计算方法等。

          应用ValueAnimator有两个步聚:

  1. 计算属性值
  2. 根据属性值执行相应的动作,如改变对象的某一属性。

           第一步 ValuAnimiator 已经完成了,我们只需要实现ValueAnimator.onUpdateListener接口

 1 ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f); 2 animation.setDuration(1000); 3 animation.addUpdateListener(new AnimatorUpdateListener() { 4   @Override 5   public void onAnimationUpdate(ValueAnimator animation) { 6    TextView tv = new TextView(getApplication()); 7        tv.setTranslationX((Float) animation.getAnimatedValue()); 8   } 9 });10 animation.setInterpolator(new CycleInterpolator(3));11 animation.start();

②:ObjectAnimator :它是ValueAnimator的子类,它完成了ValueAnimator中 前两步 操作

为了让 ObjectAnimator 正确的更新属性, 我们必须做下面的操作:

  • 对象应该有一个setter函数:set<PropertyName>(驼峰命名法),例如, 如果属性是foo, 你必须有一个 setFoo() 方法,如果没有,则只能使用ValueAnimator
  • 如上面的例子中,像ofFloat之类的工场方法,第一个参数为对象名,第二个为属性名,后面的参数为可变参数,如果values…参数只设置了一个值的话,那么会假定为目的值,属性值的变化范围为当前值到目的值,为了获得当前值,该对象要有相应属性的getter方法:get<PropertyName>
  • 如果有getter方法,其应返回值类型应与相应的setter方法的参数类型一致。
     1 复制代码 2 tv=(TextView)findViewById(R.id.textview1); 3 btn=(Button)findViewById(R.id.button1); 4 btn.setOnClickListener(new OnClickListener() { 5   @Override 6   public void onClick(View v) { 7     ObjectAnimator oa 8 =ObjectAnimator.ofFloat(tv, "alpha" 9 , 0f, 1f);10     oa.setDuration(3000);11     oa.start();12   }13 });

    在有些情况下,我们可能需要在onAnimationUpdate() callback中 执行invalidate() 方法来强制屏幕重绘。

AnimatorSet动画集

       在动画集中,我们可以设置组中动画的时序关系,如同时播放,顺序播放等。

 1 AnimatorSet bouncer = new AnimatorSet(); 2 bouncer.play(bounceAnim).before(squashAnim1); 3 bouncer.play(squashAnim1).with(squashAnim2); 4 bouncer.play(squashAnim1).with(stretchAnim1); 5 bouncer.play(squashAnim1).with(stretchAnim2); 6 bouncer.play(bounceBackAnim).after(stretchAnim2); 7 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); 8 fadeAnim.setDuration(250); 9 AnimatorSet animatorSet = new AnimatorSet();10 animatorSet.play(bouncer).before(fadeAnim);11 animatorSet.start();

④: 动画监听

      Animator.AnimatorListener中定义了动画的一些公共接口

1 onAnimationStart()2 3 onAnimationEnd()4 5 onAnimationRepeat()6 7 //当动画被取消时调用,同时会调用onAnimationEnd().8 onAnimationCancel()

注意:

通过观察我们发现AnimatorListenerAdapter这个类 实现了Animator.AnimatorListener接口{每个方法的内部实现都是空} ,我们可以通过继承这个类 {只复写需要复写的方法},而不是实现接口,来达到精简代码的操作。

1 ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);2 oa.setDuration(3000);3 oa.addListener(new AnimatorListenerAdapter(){4   public void on AnimationEnd(Animator animation){5     Log.i("Animation","end");6   }7 });8 oa.start();

⑤ViewGroups过渡动画

1 <LinearLayout2   android:orientation="vertical"3   android:layout_width="wrap_content"4   android:layout_height="match_parent"5   android:id="@+id/verticalContainer"6   <!--设置为true,在添加或者移除View的时候,播放动画-->7   android:animateLayoutChanges="true" />8 复制代码

常见过度动画类型:

  • LayoutTransition.APPEARING :当一个View在ViewGroup中出现时,对此View设置的动画
  • LayoutTransition.CHANGE_APPEARING 当一个View在ViewGroup中出现时,对此View对其他View位置造成影响,对其他View设置的动画
  • LayoutTransition.DISAPPEARING  当一个View在ViewGroup中消失时,对此View设置的动画
  • LayoutTransition.CHANGE_DISAPPEARING 当一个View在ViewGroup中消失时,对此View对其他View位置造成影响,对其他View设置的动画

⑥View的animate()方法 和 ViewPropertyAnimator

 1 // need API12 2     imageView.animate() 3         .alpha(0) 4         .y(50).setDuration(1000) 5         // need API 12 6         .withStartAction(new Runnable() { 7           //动画前要执行的操作 8           @Override 9           public void run() {10             Log.e(TAG, "START");11           }12           // need API 1613         }).withEndAction(new Runnable() {14       // 动画后要执行 的操作15       @Override16       public void run() {17         Log.e(TAG, "END");18         runOnUiThread(new Runnable() {19           @Override20           public void run() {21             imageView.setY(0);22             imageView.setAlpha(1.0f);23           }24         });25       }26     }).start();

ViewPropertyAnimator类多用于对一个View的多个属性进行动画,该类对多属性动画进行了优化,会合并一些invalidate()来减少刷新视图

1 /**与上面代码的f功能相同*/2 PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 3       0f, 1f); 4   PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 50, 0); 5   ObjectAnimator.ofPropertyValuesHolder(mBlueBall, pvhX, pvhY).setDuration(1000).start();

⑦TypeEvaluator

根据属性的开始、结束值与TimeInterpolation计算出的因子计算出当前时间的属性值

  • IntEvaluator:属性的值类型为int
  • FloatEvaluator:属性的值类型为float
  • ArgbEvaluator:属性的值类型为十六进制颜色值
  • TypeEvaluator:一个接口,可以通过实现该接口自定义Evaluator
     1 public class FloatEvaluator implements TypeEvaluator { 2  3   public Object evaluate(float fraction, Object startValue, Object endValue) { 4     float startFloat = ((Number) startValue).floatValue(); 5 /** 6 fraction 是根据动画执行的时间跟应用的Interplator,计算出的一个0~1之间的因子 7 */ 8     return startFloat + fraction * (((Number) endValue).floatValue() - startFloat); 9   }10 }

     

 

   

1 <??> 2 <objectAnimator ="http://schemas.android.com/apk/res/android" 3   android:duration="1000" 4   android:propertyName="scaleX" 5   android:valueFrom="1.0" 6   android:valueTo="2.0" 7   android:valueType="floatType" > 8 </objectAnimator>

   code:

1 public void scaleX(View view) 2   { 3     // 加载动画 4     Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex); 5     anim.setTarget(mMv); 6     anim.start(); 7   }

学习来自此处,谢谢大家!




原标题:android 动画分类

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商物流配送服务:https://www.goluckyvip.com/tag/93971.html
国际跨境物流服务:https://www.goluckyvip.com/tag/93972.html
跨境网络专线服务:https://www.goluckyvip.com/tag/93973.html
跨境专线服务:https://www.goluckyvip.com/tag/93974.html
快递跨境服务点:https://www.goluckyvip.com/tag/93975.html
挑选实惠的跨境物流方案:https://www.goluckyvip.com/tag/93976.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流