• animation动画应用--android游戏开发


    主要函数:

               /**
                 * AlphaAnimation
                 * float 起始透明度
                 * float 结束透明度
                 */
                alphaAnimation=new AlphaAnimation(0.0f, 1.0f);// 渐变透明度动画效果
                alphaAnimation.setAnimationListener(this);
                alphaAnimation.setDuration(10000);
                this.startAnimation(alphaAnimation);
     1            /**
     2              * ScaleAnimation
     3              *float toX 动画结束时 X坐标上的伸缩尺寸
     4               float fromX 动画起始时 X坐标上的伸缩尺寸
     5               float toX 动画结束时 X坐标上的伸缩尺寸
     6               float fromY 动画起始时Y坐标上的伸缩尺寸
     7               float toY 动画结束时Y坐标上的伸缩尺寸
     8               int pivotXType 动画在X轴相对于物件位置类型
     9               float pivotXValue 动画相对于物件的X坐标的开始位置
    10               int pivotYType 动画在Y轴相对于物件位置类型
    11               float pivotYValue 动画相对于物件的Y坐标的开始位置 
    12              */
    13              scaleAnimation=new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);// 渐变尺寸伸缩动画效果
    14              scaleAnimation.setAnimationListener(this);
    15              scaleAnimation.setDuration(2000);
    16              this.startAnimation(scaleAnimation);
     1 /**
     2              * TranslateAnimation
     3              * 参数1:fromXDelta 动画开始的点离当前View X坐标上的差值
     4              * 参数2:toXDelta 动画结束的点离当前View X坐标上的差值
     5              * 参数3:fromYDelta 动画开始的点离当前View Y坐标上的差值
     6              * 参数4:toYDelta 动画开始的点离当前View Y坐标上的差值 
     7              */
     8             translateAnimation=new TranslateAnimation(0,100,0,100);
     9             translateAnimation.setAnimationListener(this);
    10             translateAnimation.setDuration(2000);
    11             this.startAnimation(translateAnimation);
     1 /**
     2              * RotateAnimation
     3              * loat fromDegrees:旋转的开始角度。
     4                float toDegrees:旋转的结束角度。
     5                int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
     6                float pivotXValue:X坐标的伸缩值。
     7                int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
     8                float pivotYValue:Y坐标的伸缩值。 
     9              */
    10             rotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    11             rotateAnimation.setAnimationListener(this);
    12             rotateAnimation.setDuration(3000);
    13             this.startAnimation(rotateAnimation);

    界面截图:

    案例源码:

      1 package caicai.animation;
      2 
      3 import android.content.Context;
      4 import android.graphics.Bitmap;
      5 import android.graphics.BitmapFactory;
      6 import android.graphics.Canvas;
      7 import android.graphics.Color;
      8 import android.graphics.Paint;
      9 import android.graphics.PaintFlagsDrawFilter;
     10 import android.view.KeyEvent;
     11 import android.view.MotionEvent;
     12 import android.view.View;
     13 import android.view.animation.AlphaAnimation;
     14 import android.view.animation.Animation;
     15 import android.view.animation.Animation.AnimationListener;
     16 import android.view.animation.RotateAnimation;
     17 import android.view.animation.ScaleAnimation;
     18 import android.view.animation.Transformation;
     19 import android.view.animation.TranslateAnimation;
     20 
     21 public class animationView extends View implements AnimationListener {
     22     private Paint paint;
     23     public static animationView mv;
     24     private int x = 50;
     25     private Bitmap bmp;// 图片资源
     26     private Animation alphaAnimation;// 渐变透明度动画效果
     27     private Animation scaleAnimation;// 渐变尺寸伸缩动画效果
     28     private Animation translateAnimation;// 位置移动动画效果
     29     private Animation rotateAnimation;// 旋转动画效果
     30 
     31     public animationView(Context context) {
     32         super(context);
     33         mv = this;
     34         paint = new Paint();
     35         paint.setColor(Color.WHITE);
     36         paint.setAntiAlias(true);
     37         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tanke);
     38         setFocusable(true);
     39     }
     40 
     41     @Override
     42     protected void onDraw(Canvas canvas) {
     43         // TODO Auto-generated method stub
     44         super.onDraw(canvas);
     45         canvas.drawColor(Color.BLACK);
     46         canvas.drawBitmap(bmp, this.getWidth() / 2 - bmp.getWidth() / 2,
     47                 this.getHeight() / 2 - bmp.getHeight() / 2, paint);
     48         x += 1;
     49     }
     50 
     51     @Override
     52     public boolean onKeyDown(int keyCode, KeyEvent event) {
     53         if (keyCode ==KeyEvent.KEYCODE_DPAD_UP) {
     54             /**
     55              * AlphaAnimation
     56              * float 起始透明度
     57              * float 结束透明度
     58              */
     59             alphaAnimation=new AlphaAnimation(0.0f, 1.0f);// 渐变透明度动画效果
     60             alphaAnimation.setAnimationListener(this);
     61             alphaAnimation.setDuration(10000);
     62             this.startAnimation(alphaAnimation);
     63         }
     64         if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
     65             /**
     66              * ScaleAnimation
     67              *float toX 动画结束时 X坐标上的伸缩尺寸
     68               float fromX 动画起始时 X坐标上的伸缩尺寸
     69               float toX 动画结束时 X坐标上的伸缩尺寸
     70               float fromY 动画起始时Y坐标上的伸缩尺寸
     71               float toY 动画结束时Y坐标上的伸缩尺寸
     72               int pivotXType 动画在X轴相对于物件位置类型
     73               float pivotXValue 动画相对于物件的X坐标的开始位置
     74               int pivotYType 动画在Y轴相对于物件位置类型
     75               float pivotYValue 动画相对于物件的Y坐标的开始位置 
     76              */
     77              scaleAnimation=new ScaleAnimation(0.0f, 2.0f, 1.5f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);// 渐变尺寸伸缩动画效果
     78              scaleAnimation.setAnimationListener(this);
     79              scaleAnimation.setDuration(2000);
     80              this.startAnimation(scaleAnimation);
     81         }
     82         if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {// 画面转换位置移动动画效果
     83             /**
     84              * TranslateAnimation
     85              * 参数1:fromXDelta 动画开始的点离当前View X坐标上的差值
     86              * 参数2:toXDelta 动画结束的点离当前View X坐标上的差值
     87              * 参数3:fromYDelta 动画开始的点离当前View Y坐标上的差值
     88              * 参数4:toYDelta 动画开始的点离当前View Y坐标上的差值 
     89              */
     90             translateAnimation=new TranslateAnimation(0,100,0,100);
     91             translateAnimation.setAnimationListener(this);
     92             translateAnimation.setDuration(2000);
     93             this.startAnimation(translateAnimation);
     94         }
     95         if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {// 画面转移旋转动画效果
     96             /**
     97              * RotateAnimation
     98              * loat fromDegrees:旋转的开始角度。
     99                float toDegrees:旋转的结束角度。
    100                int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
    101                float pivotXValue:X坐标的伸缩值。
    102                int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
    103                float pivotYValue:Y坐标的伸缩值。 
    104              */
    105             rotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    106             rotateAnimation.setAnimationListener(this);
    107             rotateAnimation.setDuration(3000);
    108             this.startAnimation(rotateAnimation);
    109         }
    110         return super.onKeyDown(keyCode, event);
    111     }
    112     @Override
    113     public void onAnimationEnd(Animation animation) {
    114         // TODO Auto-generated method stub
    115         
    116     }
    117 
    118     @Override
    119     public void onAnimationRepeat(Animation animation) {
    120         // TODO Auto-generated method stub
    121         
    122     }
    123 
    124     @Override
    125     public void onAnimationStart(Animation animation) {
    126         // TODO Auto-generated method stub
    127         
    128     }
    129 
    130 }
    animationView
     1 package caicai.animation;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.Window;
     6 import android.view.WindowManager;
     7 
     8 public class MainActivity extends Activity {
     9     /** Called when the activity is first created. */
    10     @Override
    11     public void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         //设置全屏
    14         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    15         requestWindowFeature(Window.FEATURE_NO_TITLE);
    16         //显示自定义的SurfaceView视图
    17         setContentView(new animationView(this));
    18 
    19     }
    20 }
    MainActivity

    谢谢支持“趣淘鼓浪屿(www.qtgly.com)”

  • 相关阅读:
    4 stackstorm定时器基础 Sky
    3 stackstorm rule Sky
    2 stackstrom action Sky
    1 StackStorm介绍 Sky
    stackstorm安装 Sky
    stackstorm webui安装 Sky
    3.8 Go之并发和并行
    3.2 Go之语言竞争状态
    3.2 Go之语言并发通信
    图床_typora设置.md
  • 原文地址:https://www.cnblogs.com/clarence/p/3285420.html
Copyright © 2020-2023  润新知