论坛首页 移动开发技术论坛

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

浏览 5233 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-05  

由于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类,这个是别人实现的,可以拿过来直接用,完整源代码在下面

效果如下:


  • 大小: 23.9 KB
   发表时间:2011-08-05  
讲的很简单易懂啊。呵呵
0 请登录后投票
   发表时间:2011-08-06  
不错  很有用的内容
0 请登录后投票
   发表时间:2011-08-09  
在android的ApiDemos里面com.example.android.apis.graphics.BitmapDecode.java这个类就是来解析一个gif图片的用的是Movie的方法,官方的例子。
0 请登录后投票
   发表时间:2011-08-10  
真是让人郁闷啊,android系统居然不支持gif
0 请登录后投票
   发表时间:2011-08-11  
这个的代码,真有点看不懂,break ,continue的用法,不知道是不是我理解有问题,还是其他原因。
0 请登录后投票
   发表时间:2011-08-12  
其实,实现GIF真的不难...只是,我一直比较担心这个GIF会不会对系统造成太大的压力,应为一般涉及gif图片的地方,较多的情况是 头像,表情,签名之流。
而这些地方通常会有几张,或十几张GIF在一起.....懒人一个...一直没有去测过...
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics