通过一个Tween动画(补间动画,学过Flash的人应该很熟悉)实例学习。
首先,关于Tween动画有两种方法可以创建,先看第一种——在Java代码里编辑:
- package com.android.tween;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- 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;
-
- public class GameView extends View
- {
-
- private Animation mAnimationAlpha = null;
-
-
- private Animation mAnimationScale = null;
-
-
- private Animation mAnimationTranslate = null;
-
-
- private Animation mAnimationRotate = null;
-
-
- Bitmap mBitmap = null;
-
- public GameView(Context context)
- {
- super(context);
-
-
- mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.my)).getBitmap();
- }
-
- public void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
-
-
- canvas.drawBitmap(mBitmap, 0, 0, null);
- }
-
- public boolean onKeyUp(int keyCode, KeyEvent event)
- {
- switch ( keyCode )
- {
- case KeyEvent.KEYCODE_DPAD_UP:
-
- mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
-
- mAnimationAlpha.setDuration(3000);
-
- this.startAnimation(mAnimationAlpha);
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
-
- mAnimationScale =new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
-
- mAnimationScale.setDuration(500);
-
- this.startAnimation(mAnimationScale);
- break;
- case KeyEvent.KEYCODE_DPAD_LEFT:
-
- mAnimationTranslate = new TranslateAnimation(10, 100,10, 100);
-
- mAnimationTranslate.setDuration(1000);
-
- this.startAnimation(mAnimationTranslate);
- break;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
-
- mAnimationRotate=new RotateAnimation(0.0f, +360.0f,
- Animation.RELATIVE_TO_SELF,0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
-
- mAnimationRotate.setDuration(1000);
-
- this.startAnimation(mAnimationRotate);
- break;
- }
- return true;
- }
- }
mAnimationScale =new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);关于这个构造方法的参数理解,可以利用Eclipse的提示功能一个一个的去理解这几个参数,我这里说一下,Animation.RELATIVE_TO_SELF这个参数是限定后面的参数的参照方式,是相对的还是绝对的,相对父界面,还是相对手机窗口,绝对的话你可以设置具体的数值了!
注意:别以为在GameView这个自定义的View里重写了onKeyUp()这个函数,就以为会自动监听键盘事件了,还必须在主Acitivity(HellloTween.java)里重写onKeyUp()方法,并且调用GameView里面的onKeyUp();这样才能做到监听键盘事件!
主Acitivity,HellloTween.java代码:
- package com.android.tween;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
-
- public class HellloTween extends Activity
- {
- private GameView mGameView;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- mGameView = new GameView(this);
-
- setContentView(mGameView);
- }
-
- public boolean onKeyUp(int keyCode, KeyEvent event)
- {
- if ( mGameView == null )
- {
- return false;
- }
- mGameView.onKeyUp(keyCode,event);
- return true;
- }
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- if ( mGameView == null )
- {
- return false;
- }
- if ( keyCode == KeyEvent.KEYCODE_BACK)
- {
- this.finish();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
再看第二种——在XML里设置:
先建res/anim文件夹,然后在里面分别建alpha_animation.xml、scale_animation.xml、translate_animation.xml、rotate_animation.xml四个文件(注意:文件的命名有规定的,must contain only [a-z0-9_.]):
alpha_animation.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="2000"
- />
- </set>
scale_animation.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.0"
- android:fromYScale="0.0"
- android:toYScale="1.0"
- android:pivotX="80%"
- android:pivotY="50"
- android:fillAfter="true"
- android:duration="500"
- />
- </set>
android:pivotX="80%" 比例,经实验,应该是相对手机屏幕的长宽的比例。
android:pivotY="50" 具体像素
当然,这两个参数的具体含义是为了确定缩放时的起点,即图片右下角的位置。以这个位置为起点,然后向左上方向放大。
translate_animation.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta = "10"
- android:toXDelta = "100"
- android:fromYDelta = "10"
- android:toYDelta = "100"
- android:duration = "1000"
- />
- </set>
rotate_animation.xm
- <?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="0"
- android:pivotY="50%"
- android:duration="1000"
- />
- </set>
android:pivotX="0"
android:pivotY="50%"
这两个属性和上面的基本上差不多,有区别的地方是,这里的作用主要是为了确定旋转的轴心。
GameView.java
- package com.android.myAnimation;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.drawable.BitmapDrawable;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
-
- public class GameView extends View
- {
- Context mContext = null;
- private Animation mAnimationAlpha = null;
- private Animation mAnimationScale = null;
- private Animation mAnimationTranslate = null;
- private Animation mAnimationRotate = null;
- private Bitmap mBitmap = null;
- public GameView(Context context) {
- super(context);
-
- mContext = context;
- mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.my)).getBitmap();
- }
-
- public void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawBitmap(mBitmap, 0, 0, null);
- }
-
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- switch(keyCode) {
- case KeyEvent.KEYCODE_DPAD_UP:
- mAnimationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.alpha_animation);
- startAnimation(mAnimationAlpha);
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- mAnimationScale = AnimationUtils.loadAnimation(mContext, R.anim.scale_animation);
- startAnimation(mAnimationScale);
- break;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- mAnimationTranslate = AnimationUtils.loadAnimation(mContext, R.anim.translate_animation);
- startAnimation(mAnimationTranslate);
- break;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- mAnimationRotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_animation);
- startAnimation(mAnimationRotate);
- break;
- }
-
- return true;
- }
- }
AnimationXML.java(主Activity):
- package com.android.myAnimation;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
-
- public class AnimationXML extends Activity {
- private GameView mGameView = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- mGameView = new GameView(this);
-
- setContentView(mGameView);
- }
-
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- if(mGameView == null) {
- return false;
- }
-
- mGameView.onKeyUp(keyCode, event);
- return true;
- }
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(mGameView == null) {
- return false;
- }
-
- if(keyCode == KeyEvent.KEYCODE_BACK) {
- this.finish();
- return true;
- }
-
- return super.onKeyDown(keyCode, event);
- }
- }
最后看看效果图:
alpha_animation
scale_animation
rotate_animation
translate_animation
关于android:XXX这样的属性含义,很多人不理解,然后就束手无策了,我经常这样去解决:1、顾名思义:用金山词霸去翻译(当然,我的英文很菜,必须借助这个……),意思应该差不多!2、查询API文档:很多人找不到地方,不知道在哪查,我现在截图给大家