Animation
public class AnimationTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new AnimationView(this));
}
}
class AnimationView extends View implements AnimationListener {
private static final String TAG = "Gao";
private Paint mPaint;
private Bitmap mBitmap;
private Animation mAlphaAnimation;
private Animation mScaleAnimation;
private Animation mTranslateAnimation;
private Animation mRotateAnimation;
public AnimationView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
canvas.drawText("方向键↑ 渐变透明度动画效果", 80, this.getHeight() - 80, mPaint);
canvas.drawText("方向键↓ 渐变尺寸伸缩动画效果", 80, this.getHeight() - 60, mPaint);
canvas.drawText("方向键← 画面转换位置移动动画效果", 80, this.getHeight() - 40, mPaint);
canvas.drawText("方向键→ 画面转移旋转动画效果", 80, this.getHeight() - 20, mPaint);
canvas.drawBitmap(mBitmap,this.getWidth() / 2 - mBitmap.getWidth() / 2, this.getHeight()/ 2 - mBitmap.getHeight() / 2, mPaint);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
startAlphaAnimation();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
startScaleAnimation();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
startTranslateAnimation();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
startRotateAnimation();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
private void startRotateAnimation() {
mRotateAnimation=new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
mRotateAnimation.setAnimationListener(this);
mRotateAnimation.setDuration(3000);
this.startAnimation(mRotateAnimation);
}
private void startTranslateAnimation() {
mTranslateAnimation=new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
mTranslateAnimation.setAnimationListener(this);
mTranslateAnimation.setDuration(3000);
this.startAnimation(mTranslateAnimation);
}
private void startScaleAnimation() {
mScaleAnimation = new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);
mScaleAnimation.setAnimationListener(this);
mScaleAnimation.setDuration(3000);
this.startAnimation(mScaleAnimation);
}
private void startAlphaAnimation() {
mAlphaAnimation=new AlphaAnimation(0.1f, 1.0f);
mAlphaAnimation.setAnimationListener(this);
mAlphaAnimation.setDuration(3000);
this.startAnimation(mAlphaAnimation);
}
@Override
public void onAnimationEnd(android.view.animation.Animation animation) {
// TODO Auto-generated method stub
Log.i(TAG, "onAnimationEnd");
}
@Override
public void onAnimationRepeat(android.view.animation.Animation animation) {
// TODO Auto-generated method stub
Log.i(TAG, "onAnimationRepeat");
}
@Override
public void onAnimationStart(android.view.animation.Animation animation) {
// TODO Auto-generated method stub
Log.i(TAG, "onAnimationStart");
}
}
canvas.drawBitmap(mBitmap,this.getWidth() / 2 - mBitmap.getWidth() / 2, this.getHeight()/ 2 - mBitmap.getHeight() / 2, mPaint);这段代码可以让图片画在屏幕的正中间
每次启动一个Animation都会触发
I/Gao ( 569): onAnimationStart
I/Gao ( 569): onAnimationEnd