最近学习了下动画,大致分为:属性动画(继承值动画),帧动画,补间动画。下面介绍下每一种的XML文件配置
首先:普通动画:
分为translate,scale,alpha,rotate,四种动画配置大题相同;
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="0" android:interpolator="@anim/cycle_7" android:toXDelta="10" />解释下interpolator这个属性,这个可以设置动画结束时候的一些结束动画,例如来回振动次数cycle,还有overshoot等。
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />这个属性说明,来回振动7次。其他三种动画的配置也相同,如果想设置多种动画的合集,XML文件外可以包裹一层Set属性。
在代码中同样可以设置补间动画。
public void translate(View v){ // ta = new TranslateAnimation(10, 100, 20, 200); ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f); //设置播放时间 ta.setDuration(2000); //设置重复次数 ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); } public void scale(View v){ // sa = new ScaleAnimation(fromX, toX, fromY, toY, iv.getWidth() / 2, iv.getHeight() / 2); sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); //填充动画的结束位置 sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); sa.setFillAfter(true); iv.startAnimation(sa); } public void alpha(View v){ aa = new AlphaAnimation(0, 1); aa.setDuration(2000); sa.setRepeatCount(1); iv.startAnimation(aa); } public void rotate(View v){ ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); iv.startAnimation(ra); } public void fly(View v){ AnimationSet set = new AnimationSet(false); set.addAnimation(ta); set.addAnimation(sa); set.addAnimation(ra); set.addAnimation(aa); iv.startAnimation(set); }
然后是帧动画,一般这种动画出现次数比较少,相对在app的开场动画中会使用。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/g1" android:duration="200" /> <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> <item android:drawable="@drawable/g4" android:duration="200" /> <item android:drawable="@drawable/g5" android:duration="200" /> <item android:drawable="@drawable/g6" android:duration="300" /> <item android:drawable="@drawable/g7" android:duration="300" /> <item android:drawable="@drawable/g8" android:duration="300" /> <item android:drawable="@drawable/g9" android:duration="200" /> <item android:drawable="@drawable/g10" android:duration="200" /> <item android:drawable="@drawable/g11" android:duration="200" /> </animation-list>oneshot:动画是否只执行一次,duration:动画的持续时间。
//把帧动画的资源文件指定为iv的背景 iv.setBackgroundResource(R.drawable.frameanimation); //获取iv的背景 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start();在类中调用帧动画。
下面介绍下属性动画:这种动画与帧动画的区别在于,他是真实的去移动位移。
public void translate(View v){ // TranslateAnimation ta = new TranslateAnimation(0, 150, 0, 0); // ta.setDuration(2000); // ta.setFillAfter(true); // iv.startAnimation(ta); //target:动画作用于哪个组件 ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); } public void scale(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2); oa.setDuration(2000); oa.start(); } public void alpha(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.6f, 0.2f, 1); oa.setDuration(2000); oa.start(); } public void rotate(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); } public void fly(View v){ AnimatorSet set = new AnimatorSet(); ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); oa1.setDuration(2000); oa1.setRepeatCount(1); oa1.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10, 70, 20, 100); oa2.setDuration(2000); oa2.setRepeatCount(1); oa2.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2); oa3.setDuration(2000); oa3.setRepeatCount(1); oa3.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360); oa4.setDuration(2000); oa4.setRepeatCount(1); oa4.setRepeatMode(ValueAnimator.REVERSE); //设置挨个飞 // set.playSequentially(oa1, oa2, oa3, oa4); //设置一起飞 set.playTogether(oa1, oa2, oa3, oa4); set.start(); } public void xml(View v){ Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator); //设置作用于哪个组件 at.setTarget(iv); at.start(); }同样,他也可以加载XML动画。
<set xmlns:android="http://schemas.android.com/apk/res/android" > <objectAnimator android:propertyName="translationX" android:duration="200" android:repeatCount="1" android:repeatMode="reverse" android:valueFrom="-100" android:valueTo="100" > </objectAnimator> </set>