android播放动画

来源:互联网 发布:南京大学浙江大学知乎 编辑:程序博客网 时间:2024/06/05 17:22
图片的绘制以及旋转缩放的实现


        在这点上Android 确实比J2ME 强大很多  手机游戏开发最痛苦的是什么?? 是游戏引擎的开发,但是工程师会把大部分时间浪费在对坐标上,如果写引擎的时候没有把自适应考虑周全后期会非常痛苦,现在手机屏幕分辨率是各式各样 内存大小也是各式各样 所以可见自适应屏幕算法有多么的重要。

  1. package cn.m15.xys;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Matrix;
  8. import android.graphics.Paint;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;

  14. public class Image extends Activity {
  15.     ImageView imageView = null;

  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         imageView = new ImageView(this);
  19.         setContentView(R.layout.image);
  20.         LinearLayout ll = (LinearLayout) findViewById(R.id.iamgeid);
  21.         ll.addView(imageView);
  22.         // 向左移动
  23.         Button botton0 = (Button) findViewById(R.id.buttonLeft);
  24.         botton0.setOnClickListener(new OnClickListener() {
  25.             @Override
  26.             public void onClick(View arg0) {
  27.                 imageView.setPosLeft();
  28.             }
  29.         });

  30.         // 向右移动
  31.         Button botton1 = (Button) findViewById(R.id.buttonRight);
  32.         botton1.setOnClickListener(new OnClickListener() {
  33.             @Override
  34.             public void onClick(View arg0) {
  35.                 imageView.setPosRight();
  36.             }
  37.         });
  38.         // 左旋转
  39.         Button botton2 = (Button) findViewById(R.id.buttonRotationLeft);
  40.         botton2.setOnClickListener(new OnClickListener() {
  41.             @Override
  42.             public void onClick(View arg0) {
  43.                 imageView.setRotationLeft();
  44.             }
  45.         });

  46.         // 右旋转
  47.         Button botton3 = (Button) findViewById(R.id.buttonRotationRight);
  48.         botton3.setOnClickListener(new OnClickListener() {
  49.             @Override
  50.             public void onClick(View arg0) {
  51.                 imageView.setRotationRight();
  52.             }
  53.         });

  54.         // 缩小
  55.         Button botton4 = (Button) findViewById(R.id.buttonNarrow);
  56.         botton4.setOnClickListener(new OnClickListener() {

  57.             @Override
  58.             public void onClick(View arg0) {
  59.                 imageView.setNarrow();
  60.             }
  61.         });

  62.         // 放大
  63.         Button botton5 = (Button) findViewById(R.id.buttonEnlarge);
  64.         botton5.setOnClickListener(new OnClickListener() {

  65.             @Override
  66.             public void onClick(View arg0) {
  67.                 imageView.setEnlarge();
  68.             }
  69.         });

  70.         super.onCreate(savedInstanceState);

  71.     }

  72.     class ImageView extends View {
  73.         Paint mPaint = null;
  74.         Bitmap bitMap = null;
  75.         Bitmap bitMapDisplay = null;
  76.         int m_posX = 120;
  77.         int m_posY = 50;
  78.         int m_bitMapWidth = 0;
  79.         int m_bitMapHeight = 0;
  80.         Matrix mMatrix = null;
  81.         float mAngle = 0.0f;
  82.         float mScale = 1f;//1为原图的大小

  83.         public ImageView(Context context) {
  84.             super(context);
  85.             mPaint = new Paint();
  86.             mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  87.             bitMap = BitmapFactory.decodeResource(this.getResources(),
  88.                     R.drawable.image);
  89.             bitMapDisplay = bitMap;
  90.             mMatrix = new Matrix();
  91.             // 获取图片宽高
  92.             m_bitMapWidth = bitMap.getWidth();
  93.             m_bitMapHeight = bitMap.getHeight();
  94.         }

  95.         // 向左移动
  96.         public void setPosLeft() {
  97.             m_posX -= 10;
  98.         }

  99.         // 向右移动
  100.         public void setPosRight() {
  101.             m_posX += 10;
  102.         }

  103.         // 向左旋转
  104.         public void setRotationLeft() {
  105.             mAngle--;
  106.             setAngle();
  107.         }

  108.         // 向右旋转
  109.         public void setRotationRight() {
  110.             mAngle++;
  111.             setAngle();
  112.         }

  113.         // 缩小图片
  114.         public void setNarrow() {
  115.             if (mScale > 0.5) {
  116.                 mScale -= 0.1;
  117.                 setScale();
  118.             }
  119.         }

  120.         // 放大图片
  121.         public void setEnlarge() {
  122.             if (mScale < 2) {
  123.                 mScale += 0.1;
  124.                 setScale();
  125.             }
  126.         }

  127.         // 设置缩放比例
  128.         public void setAngle() {
  129.             mMatrix.reset();
  130.             mMatrix.setRotate(mAngle);
  131.             bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
  132.                     m_bitMapHeight, mMatrix, true);
  133.         }

  134.         // 设置旋转比例
  135.         public void setScale() {
  136.             mMatrix.reset();
  137.             //float sx X轴缩放 
  138.             //float sy Y轴缩放
  139.             mMatrix.postScale(mScale, mScale);
  140.             bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
  141.                     m_bitMapHeight, mMatrix, true);
  142.         }

  143.         @Override
  144.         protected void onDraw(Canvas canvas) {
  145.             super.onDraw(canvas);
  146.             canvas.drawBitmap(bitMapDisplay, m_posX, m_posY, mPaint);
  147.             invalidate();
  148.         }
  149.     }
  150. }
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:id="@+id/iamgeid"
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     >
  8.         <Button android:id="@+id/buttonLeft"
  9.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  10.             android:text="图片向左移动"
  11.             />
  12.         <Button android:id="@+id/buttonRight"
  13.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  14.             android:text="图片向右移动"
  15.             />
  16.         <Button android:id="@+id/buttonRotationLeft"
  17.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  18.             android:text="图片左旋转"
  19.             />
  20.         <Button android:id="@+id/buttonRotationRight"
  21.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  22.             android:text="图片右旋转"
  23.             />
  24.         <Button android:id="@+id/buttonNarrow"
  25.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  26.             android:text="图片缩小"
  27.             />
  28.         <Button android:id="@+id/buttonEnlarge"
  29.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  30.             android:text="图片放大"
  31.             />
  32. </LinearLayout>
复制代码
4.播放frame动画


        做游戏的话播放动画可就是必不可少的元素 帧动画帧动画 顾名思义是一帧一帧的播放 。 实际在开发中为了节省内存美术会把人物的图片切成一小块一小块然后由程序根据编辑器生成的点把图片在拼接起来这样就可以做到用更少的图片去实现更多的动画效果因为不太方便介绍图片编辑器 这个demo我只给大家简单的介绍一下播放动画的原理 后期我会深入讲解。
              如图所示这个小人一直在行走 实际上是4张图片在来回切换 每张图片延迟500毫秒 后播下一张 以此类推。
  1. package cn.m15.xys;

  2. import android.app.Activity;
  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.os.Bundle;
  10. import android.view.View;

  11. public class FramAnimation extends Activity {
  12.     public final static int ANIM_COUNT = 4;

  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         setContentView(new FramView(this));
  16.         super.onCreate(savedInstanceState);

  17.     }

  18.     class FramView extends View {
  19.         Bitmap[] bitmap = new Bitmap[ANIM_COUNT];
  20.         Bitmap display = null;
  21.         Paint paint = null;
  22.         long startTime = 0;
  23.         int playID = 0;

  24.         public FramView(Context context) {
  25.             super(context);
  26.             for (int i = 0; i < ANIM_COUNT; i++) {
  27.                 bitmap[i] = BitmapFactory.decodeResource(this.getResources(),
  28.                         R.drawable.hero_a + i);
  29.             }
  30.             display = bitmap[0];
  31.             paint = new Paint();
  32.             startTime = System.currentTimeMillis();
  33.         }

  34.         @Override
  35.         protected void onDraw(Canvas canvas) {
  36.             super.onDraw(canvas);
  37.             paint.setColor(Color.WHITE);
  38.             canvas.drawText("播放动画中...", 100, 30, paint);
  39.             long nowTime = System.currentTimeMillis();
  40.             if (nowTime - startTime >= 500) {
  41.                 startTime=nowTime;
  42.                 playID++;
  43.                 if (playID >= ANIM_COUNT) {
  44.                     playID = 0;
  45.                 }
  46.                 canvas.drawBitmap(bitmap[playID], 100, 100, paint);
  47.             }
  48.             invalidate();
  49.         }
  50.     }

  51. }

0 0
原创粉丝点击