• [转]Android中的Frame动画


    本文转自:http://vlinux.iteye.com/blog/472855

    相信有Android手机的人都玩过一款Kuba的游戏(没玩过的我推荐去玩一下),里面用手指接触到屏幕后产生的爆炸效果确实增加了游戏的不少色彩。那么这个是怎么做出来的呢?

    很明显,这个效果应该是一个动画序列图实现的,即Frame-by-Frame动画。Android实现Frame-by-Frame动画我会的有两种方法:

    1、animation-list配置,预先将一个动画按照每帧分解成的多个图片所组成的序列。然后再在Android的配置文件中将这些图片配置到动画里面。

    Xml代码 复制代码 收藏代码
    1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     android:oneshot="false">  
    3.     <item android:drawable="@drawable/explode1" android:duration="50" />  
    4.     <item android:drawable="@drawable/explode2" android:duration="50" />  
    5.     <item android:drawable="@drawable/explode3" android:duration="50" />  
    6.     <item android:drawable="@drawable/explode4" android:duration="50" />  
    7. </animation-list>  

    但是由此带来的不便也是显而易见的:drawable目录下拥挤了过多的动画帧文件。如果游戏大起来,动画效果丰富,那么drawable目录下将拥有数量庞大的图片文件,这将是开发人员的灾难(见下图)。


    2、AnimationDrawable动画。其实我们发现,我们完全可以将同一动画序列的每帧图片都合并到一个大的图片中去,然后读取图片的时候按照约定好的宽、高去读就能准确的将该帧图片精确的读出来了。下图是小雪行走序列图。



     将序列图读出并且转化为动画的核心代码为

    Java代码 复制代码 收藏代码
    1. animationDrawable = new AnimationDrawable();   
    2. Bitmap[] bitmaps = new Bitmap[PlayerConst.PLAYER_XIAOXUE_WALK_FRAME];   
    3. for (int frame = 0; frame < bitmaps.length; frame++) {   
    4.     Bitmap bitmap = Bitmap.createBitmap(xiaoxueWalkSerBitmap,    
    5.             frame*PlayerConst.PLAYER_XIAOXUE_WALK_WIDTH,    
    6.             lay*PlayerConst.PLAYER_XIAOXUE_WALK_HEIGHT,    
    7.             PlayerConst.PLAYER_XIAOXUE_WALK_WIDTH,   
    8.             PlayerConst.PLAYER_XIAOXUE_WALK_HEIGHT);   
    9.     animationDrawable.addFrame(new BitmapDrawable(bitmap),100);   
    10. }// for,每层有 PLAYER_XIAOXUE_WALK_FRAME 帧   
    11. animationDrawable.setOneShot(false);   
    12. setBackgroundDrawable(animationDrawable);  

     具体例子可以从附件中找到。

    3、SurfaceView动画。也许你很快就发现,前两个动画都必须依赖View才能展示,并且每个View只能展示一个动画。而在游戏中不可能只有一动画,更恐怖的是很多动画都是随机产生的,并不是事先约定好的,而动态创建/删除View的代价非常高,并不适合做高性能的游戏。这个时候你需要的是SurfaceView。

    在SurfaceView中的动画有一点是和前边两种动画有区别的:那就是画布上所有的一切都必须自己亲自打理。在前边几个基于Animation的动画你只需关心当前动画的序列即可,其他都由系统帮你处理完毕。而在SurfaceView中,你就是那个处理程序,所有的一切包括背景都必须有你来亲自打理。

    为此我写了一个框架专门来处理这个琐事,框架只有两个类:AnimationDraw和DrawRunning。其中AnimationDraw则是一个动画类,它负责描述当前动画元素的位置、当前播放到第几帧、每帧的延时是多少、是否重复播放等。

    Java代码 复制代码 收藏代码
    1. import java.util.Date;   
    2.   
    3. import android.graphics.Bitmap;   
    4.   
    5. /**  
    6.  * 动画绘画元素  
    7.  * @author vlinux  
    8.  *  
    9.  */  
    10. public class AnimationDraw {   
    11.   
    12.     protected float x;   
    13.     protected float y;   
    14.     protected Bitmap[] bitmaps;   
    15.     protected long duration;   
    16.        
    17.     protected Long lastBitmapTime;   
    18.     protected int step;   
    19.     protected boolean repeat;   
    20.        
    21.     /**  
    22.      * 动画构造函数-for静态图片  
    23.      * @param x:X坐标<br/>  
    24.      * @param y:Y坐标<br/>  
    25.      * @param bitmap:显示的图片<br/>  
    26.      * @param duration:图片显示的时间<br/>  
    27.      */  
    28.     public AnimationDraw(float x, float y, Bitmap bitmap, long duration) {   
    29.         Bitmap[] bitmaps = {bitmap};   
    30.         this.x = x;   
    31.         this.y = y;   
    32.         this.bitmaps = bitmaps;   
    33.         this.duration = duration;   
    34.         this.repeat = true;   
    35.         lastBitmapTime = null;   
    36.         step = 0;   
    37.     }   
    38.        
    39.     /**  
    40.      * 动画构造函数  
    41.      * @param x:X坐标<br/>  
    42.      * @param y:Y坐标<br/>  
    43.      * @param bitmap:显示的图片<br/>  
    44.      * @param duration:图片显示的时间<br/>  
    45.      * @param repeat:是否重复动画过程<br/>  
    46.      */  
    47.     public AnimationDraw(float x, float y, Bitmap[] bitmaps, long duration, boolean repeat) {   
    48.         this.x = x;   
    49.         this.y = y;   
    50.         this.bitmaps = bitmaps;   
    51.         this.duration = duration;   
    52.         this.repeat = repeat;   
    53.         lastBitmapTime = null;   
    54.         step = 0;   
    55.     }   
    56.        
    57.        
    58.     public Bitmap nextFrame() {   
    59.   
    60.         if (step >= bitmaps.length) {   
    61.             // 判断step是否越界   
    62.             if( !repeat ) {   
    63.                 return null;   
    64.             } else {   
    65.                 lastBitmapTime = null;   
    66.             }//if   
    67.         }// if   
    68.   
    69.         if (null == lastBitmapTime) {   
    70.             // 第一次执行   
    71.             lastBitmapTime = new Date().getTime();   
    72.             return bitmaps[step = 0];   
    73.         }// if   
    74.   
    75.         // 第X次执行   
    76.         long nowTime = System.currentTimeMillis();   
    77.         if (nowTime - lastBitmapTime <= duration) {   
    78.             // 如果还在duration的时间段内,则继续返回当前Bitmap   
    79.             // 如果duration的值小于0,则表明永远不失效,一般用于背景   
    80.             return bitmaps[step];   
    81.         }// if   
    82.         lastBitmapTime = nowTime;   
    83.         return bitmaps[step++];// 返回下一Bitmap   
    84.     }   
    85.        
    86.     public float getX() {   
    87.         return x;   
    88.     }   
    89.   
    90.     public float getY() {   
    91.         return y;   
    92.     }   
    93.        
    94. }  
     

    DrawRunning则是一个负责画图的线程,它是程序的核心。

    Java代码 复制代码 收藏代码
    1. import java.util.ArrayList;   
    2. import java.util.Iterator;   
    3. import java.util.List;   
    4.   
    5. import android.graphics.Bitmap;   
    6. import android.graphics.Canvas;   
    7. import android.view.SurfaceHolder;   
    8.   
    9. /**  
    10.  * 绘画线程  
    11.  *   
    12.  * @author vlinux  
    13.  *   
    14.  */  
    15. public class DrawRunning implements Runnable {   
    16.   
    17.     private List<AnimationDraw> animationDraws;//所有需要画动画的集合   
    18.     private List<AnimationDraw> buffers;//缓存前台传入需要展示的动画   
    19.     private SurfaceHolder surfaceHolder;   
    20.     private boolean running;   
    21.   
    22.     public DrawRunning(SurfaceHolder surfaceHolder) {   
    23.         this.surfaceHolder = surfaceHolder;   
    24.         animationDraws = new ArrayList<AnimationDraw>();   
    25.         buffers = new ArrayList<AnimationDraw>();   
    26.         running = true;   
    27.     }   
    28.   
    29.     @Override  
    30.     public void run() {   
    31.         // TODO Auto-generated method stub   
    32.         while (running) {   
    33.             synchronized (surfaceHolder) {   
    34.                 Canvas canvas = null;   
    35.                 try {   
    36.                     canvas = surfaceHolder.lockCanvas(null);   
    37.                     doDraw(canvas);   
    38.                 } finally {   
    39.                     if (null != canvas) {   
    40.                         surfaceHolder.unlockCanvasAndPost(canvas);   
    41.                     }// if   
    42.                 }// try   
    43.             }// syn   
    44.         }// while   
    45.     }   
    46.   
    47.     private void doDraw(Canvas canvas) {   
    48.         synchronized(this) {   
    49.             //检查缓存中是否有需要加入的动画   
    50.             if( !buffers.isEmpty() ) {   
    51.                 animationDraws.addAll(buffers);//加入animationDraws   
    52.                 buffers.clear();//清空缓存   
    53.             }//if   
    54.         }//syn   
    55.         if( animationDraws.isEmpty() ) {   
    56.             return;//如果animationDraws里面是空的那就不用画了   
    57.         }//if   
    58.         //---这里开始绘画   
    59.         Iterator<AnimationDraw> bombIt = animationDraws.iterator();   
    60.         while (bombIt.hasNext()) {   
    61.             AnimationDraw bomb = bombIt.next();   
    62.             Bitmap nextFrame = bomb.nextFrame();   
    63.             if (null == nextFrame) {   
    64.                 //下一Frame为null,说明动画序列已经结束   
    65.                 //该动画已经完成,从动画集合中删除   
    66.                 bombIt.remove();   
    67.                 continue;//while   
    68.             }// if   
    69.             canvas.drawBitmap(nextFrame, bomb.getX(), bomb.getY(), null);   
    70.         }// while   
    71.     }   
    72.   
    73.     public void addAnimationDraw(AnimationDraw bomb) {   
    74.         synchronized(this) {   
    75.             //尽量减少这个的同步响应时间,因为这个方法是前台响应的   
    76.             //多0.1秒都会直接反应到用户感知   
    77.             buffers.add(bomb);//将需要显示动画的内容加入到缓存   
    78.         }//syn   
    79.     }   
    80.   
    81.     public void stopDrawing() {   
    82.         running = false;   
    83.     }   
    84.   
    85. }  

    值得注意的是,我用了一个缓存和两个synchronized来提高前台的响应以及确保对集合类、SurfaceHolder的正确操作。

    例子可以见附件。

    • 大小: 8 KB
    • 描述: 小雪行走序列图
    • 大小: 38 KB
  • 相关阅读:
    Maven导包失败三种解决方案-Could not transfer artifact
    MySQL远程登录赋权操作各命令的意思
    大数据技术与应用课堂测试01
    软件体系架构课堂测试01
    设计模式复习笔记23
    设计模式复习笔记22
    设计模式复习笔记21
    设计模式复习笔记20
    设计模式复习笔记19
    设计模式复习笔记18
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2336586.html
Copyright © 2020-2023  润新知