职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 387|回复: 1

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

[复制链接]
找不到我 发表于 2011-8-10 09:37 | 显示全部楼层 |阅读模式
由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的麻烦。

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

Java代码  
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;   
        }   
    }   
}  

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
Java代码  
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;   
        }   
    }   
}  

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
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);   
    }   
}  

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
GIFshow.zip (637.7 KB)
下载次数: 5
查看图片附件

北大青鸟 发表于 2011-8-10 09:37 | 显示全部楼层
讲的很简单易懂啊。呵呵
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-2 21:51 , Processed in 0.103603 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表