Android 动画复习

Android常用动画有frame(帧动画)动画,tween(补间动画)动画,属性动画。
1.frame动画
作为帧动画,frame动画根据快速切换图片而达到动画效果。通常用xml定义动画会更好。在res/drawable目录下新建xml文件。

<?xml version = "1.0" encoding = "utf - 8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">    //表示动画执行一次
      <item
       android:duration="500"
       android:drawable="@drawable/"
      <item
       android:duration="500"
       android:drawable="@drawable/"
      <item
       android:duration="500"
       android:drawable="@drawable/`
</animationt-list>                 
       
启动动画效果
ImageView myView = (ImageView) findViewbyId(R.)
((AnimationDrawable)myView.getBackgroud()).start()

2.tween动画 ,在res/anim目录下建xml文件

<?xml version = "1.0" encoding = "utf-8"?>
<set xmlns:android="http://schema.android.com/apk/res/android"
 //可以自定义interpolator(接口)
 android:interpolator="@android:anim/accelerate_interpolator"
 android:shareInterpolator="true">
<alpha
  android:fromAlpha='float'
  android:toAlpha="float"/>
<translate
  android:fromX="float"
  android:toX="float"/>
.......
</set>

set是动画容器,interpolator为插值器,set里面可以嵌套roate translate scale alpha。也可以不共享插值器,每个item单独设置。不间动画只用于View.并且效果有限。用法为  Animation anim = AnimationUtils.loadAnimation(this,R.);View.startAnimation(anim);

3.属性动画
ValueAnimator是属性动画的核心类,用于实现值得过渡。ObjectAnimator是其子类。可以对任意属性进行操作。

ValueAnimator anim = ValueAnimator.ofFloat(0.0f,1.0f)
anim.setDuration(1000)
anim.addUpdateListener(listener)
anim.start()


ValueAnimation.AnimatorUpdateListener listener = new ValueAnimation.AnimatorUpdateListener (){
@Override
 public void  onAnimationUpdate(ValueAnimator animation){
    float newvalue = animation.getAnimatedValue()
    View.setAlpha(newValue)
}
}

ObjectAnimator  animation = ObjectAnimator.ofFloat(View,”alpha”,1.0f,0.3f,0.6f)

 
 animation.setDuration(500)
 animation.setInterpolator(new AccelerateInterpolator())
 animation.start()

 实际上属性动画是每个一段时间不断获得属性值,这个值如何确定呢?通过TypeEvaluator的唯一evaluate()获得。如果要控制速度,还要用到Interpolator.

 public class myEvaluator implements TypeEvaluator{
@Override
  public Integer evaluate(float fraction.Integer startValue ,Integer endValue){
     int newValue = startValue + (int) (fraction*(endValue - startValue))
     return newValue 
  }
  }
  参数float fraction为已经执行时间占总的时间比
   
 插值器Interpolator       


myInterpolator implements Timeinterpolator{
@Override
public float getInterpolator(float input){
  return input*input
}
}
     
 input 即为TypeEvaluator中的fraction,通过修改fraction。
 变占比。100毫秒0.01,200  0.2*02.=0.04,300 0.09,差值为1,为加速
 

ObjectAnimator animator = ObejectAnimator.ofObject(mView,"x",new myEvaluator(),0,200)
animator.setDuration(300)
animator.start()

  AnimatorSet 属性动画集的方法
  play()/with()/before()/after(Animator)/after(long)/playTogether(…)

猜你喜欢

转载自blog.csdn.net/sinat_33878878/article/details/70768302