原址:http://hualang.iteye.com/category/143855
由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的麻烦。
要想播放gif动画,首先需要对gif动画进行解码,然后将gif中的每一帧提取出来,放在一个容器中,然后根据需要绘制每一帧,这样就实现了gif动画在手机中直接播放了
GameView.gif
- package org.hualang.giftest;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.view.View;
- public class GameView extends View implements Runnable
- {
- Context mContext = null;
- /* 声明GifFrame对象 */
- GifFrame mGifFrame = null;
- public GameView(Context context)
- {
- super(context);
- mContext = context;
- /* 解析GIF动画 */
- mGifFrame=GifFrame.CreateGifImage(fileConnect(this.getResources().openRawResource(R.drawable.girl)));
- /* 开启线程 */
- new Thread(this).start();
- }
- public void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- /* 下一帧 */
- mGifFrame.nextFrame();
- /* 得到当前帧的图片 */
- Bitmap b=mGifFrame.getImage();
- /* 绘制当前帧的图片 */
- if(b!=null)
- canvas.drawBitmap(b,10,10,null);
- }
- /**
- * 线程处理
- */
- public void run()
- {
- while (!Thread.currentThread().isInterrupted())
- {
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException e)
- {
- Thread.currentThread().interrupt();
- }
- //使用postInvalidate可以直接在线程中更新界面
- postInvalidate();
- }
- }
- /* 读取文件 */
- public byte[] fileConnect(InputStream is)
- {
- try
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int ch = 0;
- while( (ch = is.read()) != -1)
- {
- baos.write(ch);
- }
- byte[] datas = baos.toByteArray();
- baos.close();
- baos = null;
- is.close();
- is = null;
- return datas;
- }
- catch(Exception e)
- {
- return null;
- }
- }
- }
GifFrame.gif
- package org.hualang.giftest;
- import java.util.Vector;
- import android.graphics.Bitmap;
- public class GifFrame
- {
- /* 保存gif中所有帧的向量 */
- private Vector frames;
- /* 当前播放的帧的索引 */
- private int index;
- public GifFrame()
- {
- frames = new Vector(1);
- index = 0;
- }
- /* 添加一帧 */
- public void addImage(Bitmap image)
- {
- frames.addElement(image);
- }
- /* 返回帧数 */
- public int size()
- {
- return frames.size();
- }
- /* 得到当前帧的图片 */
- public Bitmap getImage()
- {
- if (size() == 0)
- {
- return null;
- }
- else
- {
- return (Bitmap) frames.elementAt(index);
- }
- }
- /* 下一帧 */
- public void nextFrame()
- {
- if (index + 1 < size())
- {
- index++;
- }
- else
- {
- index = 0;
- }
- }
- /* 创建GifFrame */
- public static GifFrame CreateGifImage(byte abyte0[])
- {
- try
- {
- GifFrame GF = new GifFrame();
- Bitmap image = null;
- GifDecoder gifdecoder = new GifDecoder(abyte0);
- for (; gifdecoder.moreFrames(); gifdecoder.nextFrame())
- {
- try
- {
- image = gifdecoder.decodeImage();
- if (GF != null && image != null)
- {
- GF.addImage(image);
- }
- continue;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- break;
- }
- gifdecoder.clear();
- gifdecoder = null;
- return GF;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return null;
- }
- }
- }
还要实现GifShow.java
- package org.hualang.giftest;
- import android.app.Activity;
- import android.os.Bundle;
- public class GifShow extends Activity {
- /** Called when the activity is first created. */
- GameView myGameView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- myGameView = new GameView(this);
- setContentView(myGameView);
- }
- }
当然,这里还需要一个GifDecoder类,这个是别人实现的,可以拿过来直接用,完整源代码在下面
效果如下: