你好,游客 登录 注册 搜索
背景:
阅读新闻

Android游戏开发之游戏帧动画的播放与处理

[日期:2011-08-12] 来源:Linux社区  作者:雨松MOMO [字体:大 中 小]
  1. package cn.m15.xys;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import Android.content.Context;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.graphics.Canvas;  
  9. import android.graphics.Paint;  
  10.   
  11. public class Animation {  
  12.   
  13.     /** 上一帧播放时间 **/  
  14.     private long mLastPlayTime = 0;  
  15.     /** 播放当前帧的ID **/  
  16.     private int mPlayID = 0;  
  17.     /** 动画frame数量 **/  
  18.     private int mFrameCount = 0;  
  19.     /** 用于储存动画资源图片 **/  
  20.     private Bitmap[] mframeBitmap = null;  
  21.     /** 是否循环播放 **/  
  22.     private boolean mIsLoop = false;  
  23.     /** 播放结束 **/  
  24.     private boolean mIsend = false;  
  25.     /** 动画播放间隙时间 **/  
  26.     private static final int ANIM_TIME = 100;  
  27.       
  28.       
  29.     /** 
  30.      * 构造函数  
  31.      * @param context 
  32.      * @param frameBitmapID 
  33.      * @param isloop 
  34.      */  
  35.     public Animation(Context context, int [] frameBitmapID, boolean isloop) {  
  36.     mFrameCount = frameBitmapID.length;  
  37.     mframeBitmap = new Bitmap[mFrameCount];  
  38.     for(int i =0; i < mFrameCount; i++) {  
  39.         mframeBitmap[i] = ReadBitMap(context,frameBitmapID[i]);  
  40.     }  
  41.     mIsLoop = isloop;  
  42.     }  
  43.       
  44.     /** 
  45.      * 构造函数 
  46.      * @param context 
  47.      * @param frameBitmap 
  48.      * @param isloop 
  49.      */  
  50.     public Animation(Context context, Bitmap [] frameBitmap, boolean isloop) {  
  51.     mFrameCount = frameBitmap.length;  
  52.     mframeBitmap = frameBitmap;  
  53.     mIsLoop = isloop;  
  54.     }  
  55.       
  56.       
  57.       
  58.    /** 
  59.     * 绘制动画中的其中一帧 
  60.     * @param Canvas 
  61.     * @param paint 
  62.     * @param x 
  63.     * @param y 
  64.     * @param frameID 
  65.     */  
  66.     public void DrawFrame(Canvas Canvas, Paint paint, int x, int y,int frameID) {  
  67.     Canvas.drawBitmap(mframeBitmap[frameID], x, y, paint);  
  68.     }  
  69.       
  70.       
  71.     /** 
  72.      * 绘制动画 
  73.      * @param Canvas 
  74.      * @param paint 
  75.      * @param x 
  76.      * @param y 
  77.      */  
  78.     public void DrawAnimation(Canvas Canvas, Paint paint, int x, int y) {  
  79.     //如果没有播放结束则继续播放   
  80.     if (!mIsend) {  
  81.         Canvas.drawBitmap(mframeBitmap[mPlayID], x, y, paint);  
  82.         long time = System.currentTimeMillis();  
  83.         if (time - mLastPlayTime > ANIM_TIME) {  
  84.         mPlayID++;  
  85.         mLastPlayTime = time;  
  86.         if (mPlayID >= mFrameCount) {  
  87.             //标志动画播放结束   
  88.             mIsend = true;  
  89.             if (mIsLoop) {  
  90.             //设置循环播放   
  91.             mIsend = false;  
  92.             mPlayID = 0;  
  93.             }  
  94.         }  
  95.         }  
  96.     }  
  97.     }  
  98.       
  99.     /** 
  100.      * 读取图片资源 
  101.      * @param context 
  102.      * @param resId 
  103.      * @return 
  104.      */  
  105.     public Bitmap ReadBitMap(Context context, int resId) {  
  106.     BitmapFactory.Options opt = new BitmapFactory.Options();  
  107.     opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  108.     opt.inPurgeable = true;  
  109.     opt.inInputShareable = true;  
  110.     // 获取资源图片   
  111.     InputStream is = context.getResources().openRawResource(resId);  
  112.     return BitmapFactory.decodeStream(is, null, opt);  
  113.     }  
  114. }  
linux
相关资讯       Android教程 
本文评论   查看全部评论 (3)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 3 楼
* dun 发表于 2012/8/11 19:57:44
我想知道 生成的sprite文件具体怎么在代码中使用的啊
第 2 楼
* 阿周 发表于 2011/11/1 11:44:57
为什么 http://linux.linuxidc.com/上面的资料,我都下载不了啊!
第 1 楼
* qinglfxy 发表于 2011/10/8 17:46:24
非常感谢提供的资料。