Android Animation Tween动画效果的使用

转帖:http://aina-hk55hk.iteye.com/blog/693311

 

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

1.通过java代码实现:

Java代码 复制代码  收藏代码
  1. package com.Aina.Android;   
  2.   
  3. import android.content.Context;   
  4. import android.graphics.Bitmap;   
  5. import android.graphics.Canvas;   
  6. import android.graphics.Paint;   
  7. import android.graphics.drawable.BitmapDrawable;   
  8. import android.view.KeyEvent;   
  9. import android.view.View;   
  10. import android.view.animation.AlphaAnimation;   
  11. import android.view.animation.Animation;   
  12. import android.view.animation.RotateAnimation;   
  13. import android.view.animation.ScaleAnimation;   
  14. import android.view.animation.TranslateAnimation;   
  15.   
  16. /**  
  17.  * com.Aina.Android Pro_AnimationTween  
  18.  *   
  19.  * @author Aina.huang E-mail: 674023920@qq.com  
  20.  * @version 创建时间:2010 Jun 17, 2010 5:15:36 PM 类说明  
  21.  */  
  22. public class GameView extends View {   
  23.   
  24.     private Paint mPaint = null;   
  25.     private Animation mAlphaAnimation = null;   
  26.     private Animation mScaleAnimation = null;   
  27.     private Animation mTranslateAnimation = null;   
  28.     private Animation mRotateAnimation = null;   
  29.     private Bitmap mBitmap = null;   
  30.   
  31.     public GameView(Context context) {   
  32.         super(context);   
  33.         mBitmap = ((BitmapDrawable) this.getResources().getDrawable(   
  34.                 R.drawable.img)).getBitmap();   
  35.     }   
  36.   
  37.     @Override  
  38.     protected void onDraw(Canvas canvas) {   
  39.         super.onDraw(canvas);   
  40.         mPaint = new Paint();   
  41.         mPaint.setAntiAlias(true);   
  42.         canvas.drawBitmap(mBitmap, 00, mPaint);   
  43.     }   
  44.   
  45.     public boolean onKeyDown(int keyCode, KeyEvent event) {   
  46.         switch (keyCode) {   
  47.         case KeyEvent.KEYCODE_DPAD_UP:   
  48.             mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);// 透明度   
  49.             mAlphaAnimation.setDuration(3000);   
  50.             this.startAnimation(mAlphaAnimation);   
  51.             break;   
  52.         case KeyEvent.KEYCODE_DPAD_DOWN:   
  53.             mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,   
  54.                     1.0f,// 整个屏幕就0.0到1.0的大小//缩放   
  55.                     Animation.RELATIVE_TO_SELF, 0.5f,   
  56.                     Animation.RELATIVE_TO_SELF, 0.0f);   
  57.             mScaleAnimation.setDuration(3000);   
  58.             this.startAnimation(mScaleAnimation);   
  59.             break;   
  60.         case KeyEvent.KEYCODE_DPAD_LEFT:   
  61.             mTranslateAnimation = new TranslateAnimation(01000100);// 移动   
  62.             mTranslateAnimation.setDuration(2000);   
  63.             this.startAnimation(mTranslateAnimation);   
  64.             break;   
  65.         case KeyEvent.KEYCODE_DPAD_RIGHT:   
  66.             mRotateAnimation = new RotateAnimation(0.0f, 360.0f,//旋转   
  67.                     Animation.RELATIVE_TO_SELF, 0.5f,   
  68.                     Animation.RELATIVE_TO_SELF, 0.5f);   
  69.             mRotateAnimation.setDuration(3000);   
  70.             this.startAnimation(mRotateAnimation);   
  71.             break;   
  72.         default:   
  73.             break;   
  74.         }   
  75.         return super.onKeyDown(keyCode, event);   
  76.     }   
  77. }  
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: 674023920@qq.com
 * @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);
	}
}


Java代码 复制代码  收藏代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.KeyEvent;   
  6.   
  7. public class Test_AnimationTween extends Activity {   
  8.     /** Called when the activity is first created. */  
  9.     private GameView gv = null;   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         gv = new GameView(this);   
  14.         this.setContentView(gv);   
  15.     }   
  16.     @Override  
  17.     public boolean onKeyDown(int keyCode, KeyEvent event) {   
  18.         return gv.onKeyDown(keyCode, event);   
  19.     }   
  20.        
  21. }  
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布局实现:

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <alpha android:fromAlpha="0.1" android:toAlpha="1.0"  
  4.         android:duration="3000">   
  5.     </alpha>   
  6. </set>  
<?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>


Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <scale android:fromXScale="0.0" android:toXScale="1.0"  
  4.         android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%"  
  5.         android:pivotY="50%" android:fillAfter="false"  
  6.         android:duration="3000">   
  7.     </scale>   
  8. </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>


Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <translate android:fromXDelta="0" android:toXDelta="100"  
  4.         android:fromYDelta="0" android:toYDelta="100" android:duration="3000">   
  5.     </translate>   
  6. </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>


Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <rotate   
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  5.         android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%"  
  6.         android:pivotY="50%" android:duration="3000">   
  7.     </rotate>   
  8. </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>


Java代码 复制代码  收藏代码
  1. package com.Aina.Android;   
  2.   
  3. import android.content.Context;   
  4. import android.graphics.Bitmap;   
  5. import android.graphics.Canvas;   
  6. import android.graphics.Paint;   
  7. import android.graphics.drawable.BitmapDrawable;   
  8. import android.view.KeyEvent;   
  9. import android.view.View;   
  10. import android.view.animation.AlphaAnimation;   
  11. import android.view.animation.Animation;   
  12. import android.view.animation.AnimationUtils;   
  13. import android.view.animation.RotateAnimation;   
  14. import android.view.animation.ScaleAnimation;   
  15. import android.view.animation.TranslateAnimation;   
  16.   
  17. /**  
  18.  * com.Aina.Android Pro_AnimationTween  
  19.  *   
  20.  * @author Aina.huang E-mail: 674023920@qq.com  
  21.  * @version 创建时间:2010 Jun 17, 2010 5:15:36 PM 类说明  
  22.  */  
  23. public class GameView extends View {   
  24.   
  25.     private Paint mPaint = null;   
  26.     private Animation mAlphaAnimation = null;   
  27.     private Animation mScaleAnimation = null;   
  28.     private Animation mTranslateAnimation = null;   
  29.     private Animation mRotateAnimation = null;   
  30.     private Bitmap mBitmap = null;   
  31.     private Context mContext = null;   
  32.     public GameView(Context context) {   
  33.         super(context);   
  34.         mContext = context;   
  35.         mBitmap = ((BitmapDrawable) this.getResources().getDrawable(   
  36.                 R.drawable.img)).getBitmap();   
  37.     }   
  38.   
  39.     @Override  
  40.     protected void onDraw(Canvas canvas) {   
  41.         super.onDraw(canvas);   
  42.         mPaint = new Paint();   
  43.         mPaint.setAntiAlias(true);   
  44.         canvas.drawBitmap(mBitmap, 00, mPaint);   
  45.     }   
  46.   
  47.     public boolean onKeyDown(int keyCode, KeyEvent event) {   
  48.         switch (keyCode) {   
  49.         case KeyEvent.KEYCODE_DPAD_UP:   
  50. //          mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f);// 透明度   
  51. //          mAlphaAnimation.setDuration(3000);   
  52.             mAlphaAnimation = AnimationUtils.loadAnimation(mContext, R.anim.alpha);   
  53.             this.startAnimation(mAlphaAnimation);   
  54.             break;   
  55.         case KeyEvent.KEYCODE_DPAD_DOWN:   
  56. //          mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,   
  57. //                  1.0f,// 整个屏幕就0.0到1.0的大小//缩放   
  58. //                  Animation.RELATIVE_TO_SELF, 0.5f,   
  59. //                  Animation.RELATIVE_TO_SELF, 0.0f);   
  60. //          mScaleAnimation.setDuration(3000);   
  61.             mScaleAnimation = AnimationUtils.loadAnimation(mContext, R.anim.scale);   
  62.             this.startAnimation(mScaleAnimation);   
  63.             break;   
  64.         case KeyEvent.KEYCODE_DPAD_LEFT:   
  65. //          mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);// 移动   
  66. //          mTranslateAnimation.setDuration(2000);   
  67.             mTranslateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.translate);   
  68.             this.startAnimation(mTranslateAnimation);   
  69.             break;   
  70.         case KeyEvent.KEYCODE_DPAD_RIGHT:   
  71. //          mRotateAnimation = new RotateAnimation(0.0f, 360.0f,//旋转   
  72. //                  Animation.RELATIVE_TO_SELF, 0.5f,   
  73. //                  Animation.RELATIVE_TO_SELF, 0.5f);             
  74. //          mRotateAnimation.setDuration(3000);   
  75.             mRotateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.rotate);   
  76.             this.startAnimation(mRotateAnimation);   
  77.             break;   
  78.         default:   
  79.             break;   
  80.         }   
  81.         return super.onKeyDown(keyCode, event);   
  82.     }   
  83. }   

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值