玩转Android---2D图形及动画---Gif动画

原址:http://hualang.iteye.com/category/143855

由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的麻烦。

 

要想播放gif动画,首先需要对gif动画进行解码,然后将gif中的每一帧提取出来,放在一个容器中,然后根据需要绘制每一帧,这样就实现了gif动画在手机中直接播放了

GameView.gif

 

 

Java代码  收藏代码
  1. package org.hualang.giftest;  
  2.   
  3.   
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.InputStream;  
  6. import android.content.Context;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Canvas;  
  9. import android.view.View;  
  10.   
  11. public class GameView extends View implements Runnable  
  12. {  
  13.     Context     mContext    = null;  
  14.   
  15.     /* 声明GifFrame对象 */  
  16.     GifFrame    mGifFrame   = null;  
  17.   
  18.     public GameView(Context context)  
  19.     {  
  20.         super(context);  
  21.           
  22.         mContext = context;  
  23.         /* 解析GIF动画 */  
  24.         mGifFrame=GifFrame.CreateGifImage(fileConnect(this.getResources().openRawResource(R.drawable.girl)));  
  25.         /* 开启线程 */  
  26.         new Thread(this).start();  
  27.     }  
  28.       
  29.     public void onDraw(Canvas canvas)  
  30.     {  
  31.         super.onDraw(canvas);  
  32.         /* 下一帧 */  
  33.         mGifFrame.nextFrame();  
  34.         /* 得到当前帧的图片 */  
  35.         Bitmap b=mGifFrame.getImage();  
  36.           
  37.         /* 绘制当前帧的图片 */  
  38.         if(b!=null)  
  39.             canvas.drawBitmap(b,10,10,null);  
  40.     }  
  41.       
  42.       
  43.     /** 
  44.      * 线程处理 
  45.      */  
  46.     public void run()  
  47.     {  
  48.         while (!Thread.currentThread().isInterrupted())  
  49.         {  
  50.             try  
  51.             {  
  52.                 Thread.sleep(100);  
  53.             }  
  54.             catch (InterruptedException e)  
  55.             {  
  56.                 Thread.currentThread().interrupt();  
  57.             }  
  58.             //使用postInvalidate可以直接在线程中更新界面  
  59.             postInvalidate();  
  60.         }  
  61.     }  
  62.       
  63.     /* 读取文件 */  
  64.     public byte[] fileConnect(InputStream is)  
  65.     {  
  66.         try  
  67.         {                         
  68.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  69.             int ch = 0;  
  70.             while( (ch = is.read()) != -1)   
  71.             {  
  72.                 baos.write(ch);  
  73.             }                   
  74.             byte[] datas = baos.toByteArray();  
  75.             baos.close();   
  76.             baos = null;  
  77.             is.close();  
  78.             is = null;  
  79.             return datas;  
  80.         }  
  81.         catch(Exception e)  
  82.         {  
  83.             return null;  
  84.         }  
  85.     }  
  86. }  

 GifFrame.gif

 

Java代码  收藏代码
  1. package org.hualang.giftest;  
  2.   
  3. import java.util.Vector;  
  4. import android.graphics.Bitmap;  
  5.   
  6. public class GifFrame  
  7. {     
  8.     /* 保存gif中所有帧的向量 */  
  9.     private Vector frames;  
  10.       
  11.     /* 当前播放的帧的索引 */  
  12.     private int index;  
  13.   
  14.     public GifFrame()   
  15.     {  
  16.         frames = new Vector(1);  
  17.         index = 0;  
  18.     }  
  19.       
  20.     /* 添加一帧 */  
  21.     public void addImage(Bitmap image)   
  22.     {  
  23.         frames.addElement(image);  
  24.     }  
  25.   
  26.     /* 返回帧数 */  
  27.     public int size()   
  28.     {  
  29.         return frames.size();  
  30.     }  
  31.   
  32.     /* 得到当前帧的图片 */  
  33.     public Bitmap getImage()   
  34.     {  
  35.         if (size() == 0)   
  36.         {  
  37.             return null;  
  38.         }   
  39.         else   
  40.         {  
  41.             return (Bitmap) frames.elementAt(index);  
  42.         }  
  43.     }  
  44.   
  45.     /* 下一帧 */  
  46.     public void nextFrame()   
  47.     {  
  48.         if (index + 1 < size())   
  49.         {  
  50.             index++;  
  51.         }   
  52.         else   
  53.         {  
  54.             index = 0;  
  55.         }  
  56.     }  
  57.       
  58.     /* 创建GifFrame */  
  59.     public static GifFrame CreateGifImage(byte abyte0[])   
  60.     {  
  61.         try   
  62.         {  
  63.             GifFrame GF = new GifFrame();  
  64.             Bitmap image = null;  
  65.             GifDecoder gifdecoder = new GifDecoder(abyte0);  
  66.             for (; gifdecoder.moreFrames(); gifdecoder.nextFrame())   
  67.             {  
  68.                 try   
  69.                 {  
  70.                     image = gifdecoder.decodeImage();  
  71.                     if (GF != null && image != null)   
  72.                     {  
  73.                         GF.addImage(image);  
  74.                     }  
  75.                     continue;  
  76.                 }  
  77.                 catch (Exception e)   
  78.                 {  
  79.                     e.printStackTrace();  
  80.                 }  
  81.                 break;  
  82.             }  
  83.             gifdecoder.clear();  
  84.             gifdecoder = null;  
  85.             return GF;  
  86.         }   
  87.         catch (Exception e)   
  88.         {  
  89.             e.printStackTrace();  
  90.             return null;  
  91.         }  
  92.     }   
  93. }  

还要实现GifShow.java

 

Java代码  收藏代码
  1. package org.hualang.giftest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class GifShow extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     GameView myGameView = null;  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         myGameView = new GameView(this);  
  13.         setContentView(myGameView);  
  14.     }  
  15. }  

 当然,这里还需要一个GifDecoder类,这个是别人实现的,可以拿过来直接用,完整源代码在下面

效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值