- View Animation补间动画
ImageView Image = (ImageView)findViewById(R.id.action_bar);
Animation hyAtion=AnimationUtils.loadAnimation(this,R.anim.abc_fade_in);
Image.startAnimation(hyAtion);
2 . Drawable Animation 帧动画
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.drawable_anim);
AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
anim.stop();//在start()之前必须先stop()不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次
anim.start();
//最后一点是SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
anim.start();
}
3.Property Animation 属性动画
3.1 ValueAnimator 包含Property Animation动画的所有核心功能
ValueAnimator animator = ValueAnimator.ofFloat(0f,1f);
animator.setDuration(1000);
animator.addUpdateListener(new AnimatorUpdateListener() {//动画改变的回调
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
//animation 时间插值和持续时间计算得出来的值 (随时间的的设置和 差值器的原因)
Log.i("info", ((Float)animation.getAnimatedValue()).toString());
}
});
animator.setInterpolator(new CycleInterpolator(3));
animator.start();
3.2ValueAnimator.AnimatorUpdateListener
ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);
oa.setDuration(3000);
oa.addListener(new AnimatorListenerAdapter(){// 为动画添加各种回调
public void on AnimationEnd(Animator animation){//动画结束是调用 还有其他的方法
Log.i("Animation","end");
}
});
oa.start();
3.3 AnimationSet
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4);
bouncer.play(anim5).after(amin2);
animatorSet.start();
3.4当Layout改变时应用动画
transition.setAnimator(LayoutTransition.DISAPPEARING, customDisappearingAnim);
通过setAnimator应用动画,第一个参数表示应用的情境,可以以下4种类型:
• APPEARING 当一个元素在其父元素中变为Visible时对这个元素应用动画
• CHANGE_APPEARING 当一个元素在其父元素中变为Visible时,因系统要重新布局有一些元素需要移动,对这些要移动的元素应用动画
• DISAPPEARING 当一个元素在其父元素中变为GONE时对其应用动画
• CHANGE_DISAPPEARING 当一个元素在其父元素中变为GONE时,因系统要重新布局有一些元素需要移动,这些要移动的元素应用动画.
第二个参数为一Animator。
mTransitioner.setStagger(LayoutTransition.CHANGE_APPEARING, 30);// 此函数设置动画延迟时间,参数分别为类型与时间。
3.5 Keyframes
keyFrame是一个 时间/值 对,通过它可以定义一个在特定时间的特定状态,即关键帧,而且在两个keyFrame之间可以定义不同的Interpolator,就好像多个动画的拼接,第一个动画的结束点是第二个动画的开始点。KeyFrame是抽象类,要通过ofInt(),ofFloat(),ofObject()获得适当的KeyFrame,然后通过PropertyValuesHolder.ofKeyframe获得PropertyValuesHolder对象,如以下例子:
Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("width", kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn2, pvhRotation);
rotationAnim.setDuration(2000);
上述代码的意思为:设置btn对象的width属性值使其:
• 开始时 Width=400
• 动画开始1/4时 Width=200
• 动画开始1/2时 Width=400
• 动画开始3/4时 Width=100
• 动画结束时 Width=500
第一个参数为时间百分比,第二个参数是在第一个参数的时间时的属性值。
定义了一些Keyframe后,通过PropertyValuesHolder类的方法ofKeyframe一个PropertyValuesHolder对象,然后通过ObjectAnimator.ofPropertyValuesHolder获得一个Animator对象。
用下面的代码可以实现同样的效果(上述代码时间值是线性,变化均匀):
ObjectAnimator oa=ObjectAnimator.ofInt(btn2, "width", 400,200,400,100,500);
oa.setDuration(2000);
oa.start();
3.6 ViewPropertyAnimator
如果需要对一个View的多个属性进行动画可以用ViewPropertyAnimator类,该类对多属性动画进行了优化,会合并一些invalidate()来减少刷新视图,该类在3.1中引入。
以下两段代码实现同样的效果:
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
myView.animate().x(50f).y(100f);
3.7 getLeft 和 getx 和 getTranslationx的关系
//应用动画之前
btn2.getLeft(); //40
btn2.getX(); //40
btn2.getTranslationX(); //0
//应用translationX动画
ObjectAnimator oa=ObjectAnimator.ofFloat(btn2,"translationX", 200);
oa.setDuration(2000);
oa.start();
/*应用translationX动画后
btn2.getLeft(); //40
btn2.getX(); //240
btn2.getTranslationX(); //200
*/
//应用X动画,假设没有应用之前的translationX动画
ObjectAnimator oa=ObjectAnimator.ofFloat(btn2, "x", 200);
oa.setDuration(2000);
oa.start();
/*应用X动画后
btn2.getLeft(); //40
btn2.getX(); //200
btn2.getTranslationX(); //160
*/
无论怎样应用动画,原来的布局时的位置通过getLeft()获得,保持不变;
X是View最终的位置;
translationX为最终位置与布局时初始位置这差。
所以若就用translationX即为在原来基础上移动多少,X为最终多少
getX()的值为getLeft()与getTranslationX()的和