android 漸變透明、伸縮、平移、旋轉動畫效果
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; 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;/**
- @author Himi
- @AlphaAnimation 漸變透明度動畫效果
- @ScaleAnimation 漸變尺寸伸縮動畫效果
- @TranslateAnimation 畫面轉換位置移動動畫效果
@RotateAnimation 畫面轉移旋轉動畫效果 */ public class MyViewAnimation extends View { private Paint paint; private Bitmap bmp; private int x = 50; private Animation mAlphaAnimation; private Animation mScaleAnimation; private Animation mTranslateAnimation; private Animation mRotateAnimation;
public MyViewAnimation(Context context) {
super(context); paint = new Paint(); paint.setAntiAlias(true); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon); this.setFocusable(true);// 只有當該View獲得焦點時才會調用onKeyDown方法
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.drawColor(Color.BLACK); paint.setColor(Color.WHITE); canvas.drawText("Himi", x, 50, paint);// 備注1 canvas.drawText("方向鍵↑ 漸變透明度動畫效果", 80, this.getHeight() - 80, paint); canvas.drawText("方向鍵↓ 漸變尺寸伸縮動畫效果", 80, this.getHeight() - 60, paint); canvas.drawText("方向鍵← 畫面轉換位置移動動畫效果", 80, this.getHeight() - 40, paint); canvas.drawText("方向鍵→ 畫面轉移旋轉動畫效果", 80, this.getHeight() - 20, paint); canvas.drawBitmap(bmp, this.getWidth() / 2 - bmp.getWidth() / 2, this.getHeight() / 2 - bmp.getHeight() / 2, paint); x += 1;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {// 漸變透明度動畫效果 mAlphaAnimation = new AlphaAnimation(0.1f, 1.0f); // 第一個參數fromAlpha 為動畫開始時候透明度 // 第二個參數toAlpha 為動畫結束時候透明度 // 注意:取值范圍[0-1];[完全透明-完全不透明] mAlphaAnimation.setDuration(3000); // //設置時間持續時間為3000 毫秒=3秒 this.startAnimation(mAlphaAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {// 漸變尺寸伸縮動畫效果 mScaleAnimation = new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f); // 第一個參數fromX為動畫起始時X坐標上的伸縮尺寸 // 第二個參數toX為動畫結束時X坐標上的伸縮尺寸 // 第三個參數fromY為動畫起始時Y坐標上的伸縮尺寸 // 第四個參數toY 為動畫結束時Y 坐標上的伸縮尺寸 // 注意: // 0.0表示收縮到沒有 // 1.0表示正常無伸縮 // 值小于1.0表示收縮 // 值大于1.0表示放大 // -----我這里1-4參數表明是起始圖像大小不變,動畫終止的時候圖像被放大1.5倍 // 第五個參數pivotXType 為動畫在X 軸相對于物件位置類型 // 第六個參數pivotXValue 為動畫相對于物件的X 坐標的開始位置 // 第七個參數pivotXType 為動畫在Y 軸相對于物件位置類型 // 第八個參數pivotYValue 為動畫相對于物件的Y 坐標的開始位置 // 提示:位置類型有三種,每種效果大家自己嘗試哈~這里偷下懶~ // 畢竟親眼看到效果的區別才記憶深刻~ // Animation.ABSOLUTE // 、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT mScaleAnimation.setDuration(2000); this.startAnimation(mScaleAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {// 畫面轉換位置移動動畫效果 mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100); // 第一個參數fromXDelta為動畫起始時X坐標上的移動位置 // 第二個參數toXDelta為動畫結束時X坐標上的移動位置 // 第三個參數fromYDelta為動畫起始時Y坐標上的移動位置 // 第四個參數toYDelta 為動畫結束時Y 坐標上的移動位置 mTranslateAnimation.setDuration(2000); this.startAnimation(mTranslateAnimation); } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {// 畫面轉移旋轉動畫效果 mRotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 第一個參數fromDegrees為動畫起始時的旋轉角度 // 第二個參數toDegrees 為動畫旋轉到的角度 // 第三個參數pivotXType 為動畫在X 軸相對于物件位置類型 // 第四個參數pivotXValue 為動畫相對于物件的X 坐標的開始位置 // 第五個參數pivotXType 為動畫在Y 軸相對于物件位置類型 // 第六個參數pivotYValue 為動畫相對于物件的Y 坐標的開始位置 mRotateAnimation.setDuration(3000); this.startAnimation(mRotateAnimation); } return super.onKeyDown(keyCode, event);
} }</pre>
本文由用戶 nnkde 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!