欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tween动画

程序员文章站 2022-03-16 19:17:04
...

动画:Tween动画(Animation)、Frame动画(AnimationDrawable)、属性动画(Animator)

Tween动画

通过对一个视图不断的进行平移、旋转、放大、缩小、透明变换所形成的动画

透明度动画

//创建动画对象
AlphaAnimation anim = new AlphaAnimation(1.0f,0);
//设置时间
anim.setDuration(1000);
//重复次数(0则表示之执行一次不重复)
anim.setRepeatCount(3);
//设置监听
anim.setAnimationListener(new Animation.AnimationListener() {

   //动画开始
   @Override
   public void onAnimationStart(Animation animation) {

    }

    //动画结束
    @Override
    public void onAnimationEnd(Animation animation) {
         tv.setVisibility(View.INVISIBLE);
    }

    //动画重复
    @Override
    public void onAnimationRepeat(Animation animation) {

      }
   });
//对View启动动画
tv.startAnimation(anim);

平移动画

 //(起始的x,结束的x,起始的y,结束的y)
 TranslateAnimation anim = new TranslateAnimation(0,100,0,400);
 anim.setDuration(3000);
 //保持动画之后的状态
 anim.setFillAfter(true);
 anim.setFillBefore(true);
 tv.startAnimation(anim);

旋转动画

默认参考左上角
RotateAnimation anim = new RotateAnimation(0, 180)

//(起始角度,结束的角度,中心点x的参考方式,相对比例,中心点y的参考方式,y的相对比例)
RotateAnimation anim = new RotateAnimation(0, 180,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_PARENT, 0.5f);

缩放动画

默认参考左上角 
ScaleAnimation anim = new ScaleAnimation(0,1,0,1)

//(x方向起始倍数,x方向结束的倍数,y方向起始的倍数,y方向结束的倍数,后面4个是中心点的配置)
ScaleAnimation anim = new ScaleAnimation(0,1,0,1,
     Animation.RELATIVE_TO_SELF,0.5f,
     Animation.RELATIVE_TO_PARENT,1.0f);

动画集

AnimationSet set = new AnimationSet(true);
...
//将动画添加到set中
set.addAnimation(scale);
set.addAnimation(alpha);
set.addAnimation(anim);
//启动动画
tv.startAnimation(set);

动画速率

Interpolater描述动画的辅助效果,一般用其子类

OvershootInterplator 向前甩一下再回来
anim.setInterpolator(new OvershootInterpolator(2.0f));

AccelerateDecelerateInterplator在动画开始于结束的地方速率改变比较慢,中间加速

AccelerateInterplator 越来越快
DecelerateInterplator 越来越慢
CycleInterplator动画循环按照正弦曲线方向改变速率
LinearInterplator匀速运动

XML中定义动画

可以在res/anim文件夹中新建动画文件,如anim.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">

<alpha
    android:duration="2000"
    android:fromAlpha="0.5"
    android:toAlpha="1.0" />

<rotate
    android:duration="2000"
    android:fromDegrees="0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%p"
    android:toDegrees="360" />

</set>

java中加载

Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.move_anim);
tv.startAnimation(anim);

Frame动画

AnimationDrawable,管理帧,时间和内容的管理工具(控制某个画面停留多久)

AnimationDrawable d = new AnimationDrawable();
for(int i = 1; i < 10 ; i++){
    int id = getResources().getIdentifier
    ("pic"+i,"drawable",getApplication().getPackageName());
    Drawable frame = getResources().getDrawable(id);
    //添加帧
    d.addFrame(frame,500);
  }
  //设置是否一次性 
  d.setOneShot(false);
  //显示
  mIv.setImageDrawable(d);

XML中可以定义跟节点animation-list的xml文件文件(定义在drawable下即可)

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
    android:drawable="@drawable/pic1"
    android:duration="500" />
<item
    android:drawable="@drawable/pic2"
    android:duration="500" />
</animation-list>

布局加载
<ImageView
    android:id="@+id/m_image2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/frame_amin"/>

Animator

通过不断的修改对象的属性形成的动画(ObjectAnimator结合时间使用了对象的get/set修改属性)时间计算因子

class Util {
    View v;

    Util(View v) {
        this.v = v;
    }

    public void setWidth(int width){
        Log.e("m_tag","width:"+width);
        v.getLayoutParams().width = width;
        v.requestLayout();  //刷新布局
    }

    public int getWidth(){
        return v.getLayoutParams().width;
    }
}

针对getWidth以及setWidth方法做属性修改

 Util u = new Util(btn);
 ObjectAnimator.ofInt(u,"width",150,300).setDuration(2000).start();
ValueAnimator表示某个值在某段时间的变化
ValueAnimator anim = ValueAnimator.ofInt(1, 100);
anim.setDuration(2000);
//值变化监听
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     //整数变化的转换因子
     private IntEvaluator mEvaluator = new IntEvaluator();

     @Override
     public void onAnimationUpdate(ValueAnimator animation) {
            //获取当前值
            int currentValue = (Integer) animation.getAnimatedValue();
            float fraction = (float)currentValue/100;
            int width = mEvaluator.evaluate(fraction,150,500);
            Log.e("m_tag","width:"+width);
            btn.getLayoutParams().width = width;
            btn.requestLayout();
        }
    });
    anim.start();