这两天一直看在Sundy的动画教程,看完总结如下。
动画分类
1.Peoperty Animation
这个动画是Android3.0之后推出的目前用处不大。
2.View Animation
这类动画也叫tween animation 主要分为 渐变动画(AlphaAnimation)旋转动画(RotateAnimation)
缩放动画(ScaleAnimation)位移动画(TranslateAnimation)
3.Drawable Animation
这类动画也叫帧动画 FrameAnimation
Interpolator
An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated
直接上代码
public class TestAnimaionActivity extends Activity {
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView)findViewById(R.id.imageView1);
Button btnAlpha=(Button)findViewById(R.id.Alpha);
Button btnRotate=(Button)findViewById(R.id.Rotate);
Button btnScale=(Button)findViewById(R.id.Scale);
Button btnTranslate=(Button)findViewById(R.id.Translate);
Button btnAnimationSet=(Button)findViewById(R.id.animationSet);
btnAlpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));
btnRotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));
btnScale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));
btnTranslate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));
btnAnimationSet.setOnClickListener(new AnimationClickListener(AnimationType.Complex));
}
enum AnimationType{
Alpha,
Rotate,
Scale,
Translate,
Complex;
}
private class AnimationClickListener implements View.OnClickListener{
private AnimationType animationType;
public AnimationClickListener(AnimationType animationType) {
super();
this.animationType = animationType;
}
@Override
public void onClick(View v) {
switch (animationType) {
case Alpha:
AlphaAnimation alphaAnimation=(AlphaAnimation) AnimationUtils.loadAnimation(TestAnimaionActivity.this, R.anim.alphaanim);
alphaAnimation.setRepeatCount(5);
alphaAnimation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(alphaAnimation);
break;
case Rotate:
RotateAnimation rotateAnimation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatCount(3);
imageView.startAnimation(rotateAnimation);
break;
case Scale:
ScaleAnimation scaleAnimation=new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setZAdjustment(Animation.ZORDER_NORMAL);
imageView.startAnimation(scaleAnimation);
break;
case Translate:
TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);
translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
translateAnimation.setDuration(3000);
imageView.startAnimation(translateAnimation);
break;
case Complex:
AnimationSet animationSet=new AnimationSet(false);
AlphaAnimation alphaAnimation2=new AlphaAnimation(1.0f, 0.1f);
alphaAnimation2.setDuration(3000);
TranslateAnimation translateAnimation2=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);
translateAnimation2.setDuration(3000);
RotateAnimation rotateAnimation2=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation2.setDuration(3000);
animationSet.addAnimation(alphaAnimation2);
animationSet.addAnimation(translateAnimation2);
animationSet.addAnimation(rotateAnimation2);
imageView.startAnimation(animationSet);
break;
default:
break;
}
}
}
}
framedrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/grid_liushui" android:duration="200"/>
<item android:drawable="@drawable/grid_payout" android:duration="200"/>
<item android:drawable="@drawable/grid_report" android:duration="200"/>
</animation-list>
public class FrameAnimationAcitvity extends Activity { private ImageView imageView; private AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation_list); Button button=(Button)findViewById(R.id.button1); imageView=(ImageView)findViewById(R.id.imageView1); imageView.setBackgroundResource(R.drawable.framedrawable); ad=(AnimationDrawable)imageView.getBackground(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ad.start(); } }); } }