Android Animation Tween动画效果的使用

Animation Tween动画可以通过java代码实现,也可以通过xml布局来实现

1.通过java代码实现:

 

package com.Aina.Android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * com.Aina.Android Pro_AnimationTween
 * 
 * @author Aina.huang E-mail: [email protected]
 * @version 创建时间:2010 Jun 17, 2010 5:15:36 PM 类说明
 */
public class GameView extends View {

	private Paint mPaint = null;
	private Animation mAlphaAnimation = null;
	private Animation mScaleAnimation = null;
	private Animation mTranslateAnimation = null;
	private Animation mRotateAnimation = null;
	private Bitmap mBitmap = null;

	public GameView(Context context) {
		super(context);
		mBitmap = ((BitmapDrawable) this.getResources().getDrawable(
				R.drawable.img)).getBitmap();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		canvas.drawBitmap(mBitmap, 0, 0, mPaint);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);// 透明度
			mAlphaAnimation.setDuration(3000);
			this.startAnimation(mAlphaAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,
					1.0f,// 整个屏幕就0.0到1.0的大小//缩放
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.0f);
			mScaleAnimation.setDuration(3000);
			this.startAnimation(mScaleAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);// 移动
			mTranslateAnimation.setDuration(2000);
			this.startAnimation(mTranslateAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			mRotateAnimation = new RotateAnimation(0.0f, 360.0f,//旋转
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			mRotateAnimation.setDuration(3000);
			this.startAnimation(mRotateAnimation);
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}





package com.Aina.Android;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class Test_AnimationTween extends Activity {
    /** Called when the activity is first created. */
	private GameView gv = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gv = new GameView(this);
        this.setContentView(gv);
    }
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return gv.onKeyDown(keyCode, event);
	}
    
}

 

 

2.通过xml布局实现:

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
		android:duration="3000">
	</alpha>
</set>




<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<scale android:fromXScale="0.0" android:toXScale="1.0"
		android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%"
		android:pivotY="50%" android:fillAfter="false"
		android:duration="3000">
	</scale>
</set>




<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="0" android:toXDelta="100"
		android:fromYDelta="0" android:toYDelta="100" android:duration="3000">
	</translate>
</set>




<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<rotate
		android:interpolator="@android:anim/accelerate_decelerate_interpolator"
		android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%"
		android:pivotY="50%" android:duration="3000">
	</rotate>
</set>
 

package com.Aina.Android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * com.Aina.Android Pro_AnimationTween
 * 
 * @author Aina.huang E-mail: [email protected]
 * @version 创建时间:2010 Jun 17, 2010 5:15:36 PM 类说明
 */
public class GameView extends View {

	private Paint mPaint = null;
	private Animation mAlphaAnimation = null;
	private Animation mScaleAnimation = null;
	private Animation mTranslateAnimation = null;
	private Animation mRotateAnimation = null;
	private Bitmap mBitmap = null;
	private Context mContext = null;
	public GameView(Context context) {
		super(context);
		mContext = context;
		mBitmap = ((BitmapDrawable) this.getResources().getDrawable(
				R.drawable.img)).getBitmap();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		canvas.drawBitmap(mBitmap, 0, 0, mPaint);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
//			mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);// 透明度
//			mAlphaAnimation.setDuration(3000);
			mAlphaAnimation = AnimationUtils.loadAnimation(mContext, R.anim.alpha);
			this.startAnimation(mAlphaAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
//			mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,
//					1.0f,// 整个屏幕就0.0到1.0的大小//缩放
//					Animation.RELATIVE_TO_SELF, 0.5f,
//					Animation.RELATIVE_TO_SELF, 0.0f);
//			mScaleAnimation.setDuration(3000);
			mScaleAnimation = AnimationUtils.loadAnimation(mContext, R.anim.scale);
			this.startAnimation(mScaleAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
//			mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);// 移动
//			mTranslateAnimation.setDuration(2000);
			mTranslateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.translate);
			this.startAnimation(mTranslateAnimation);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
//			mRotateAnimation = new RotateAnimation(0.0f, 360.0f,//旋转
//					Animation.RELATIVE_TO_SELF, 0.5f,
//					Animation.RELATIVE_TO_SELF, 0.5f);			
//			mRotateAnimation.setDuration(3000);
			mRotateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
			this.startAnimation(mRotateAnimation);
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
 

 

 

 

 

你可能感兴趣的:(animation)