玩转Android---2D图形及动画---Gif动画
2015-07-30 19:39
573 查看
原址:http://hualang.iteye.com/category/143855
由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的麻烦。
要想播放gif动画,首先需要对gif动画进行解码,然后将gif中的每一帧提取出来,放在一个容器中,然后根据需要绘制每一帧,这样就实现了gif动画在手机中直接播放了
GameView.gif
Java代码
![](http://hualang.iteye.com/images/icon_star.png)
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代码
![](http://hualang.iteye.com/images/icon_star.png)
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代码
![](http://hualang.iteye.com/images/icon_star.png)
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类,这个是别人实现的,可以拿过来直接用,完整源代码在下面
效果如下:
由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的麻烦。
要想播放gif动画,首先需要对gif动画进行解码,然后将gif中的每一帧提取出来,放在一个容器中,然后根据需要绘制每一帧,这样就实现了gif动画在手机中直接播放了
GameView.gif
Java代码
![](http://hualang.iteye.com/images/icon_star.png)
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代码
![](http://hualang.iteye.com/images/icon_star.png)
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代码
![](http://hualang.iteye.com/images/icon_star.png)
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类,这个是别人实现的,可以拿过来直接用,完整源代码在下面
效果如下:
![](http://dl.iteye.com/upload/attachment/531087/6ee1741c-71fd-3502-9ef5-5d6089c1a872.gif)
相关文章推荐
- 玩转Android---2D图形及动画---图片处理
- 玩转Android---2D图形及动画---Tween动画
- 玩转Android---组件篇---TextSwitcher
- 玩转Android--组件篇---Handler的使用
- 玩转Android---组件篇---AnalogClock,DigitalClock
- 玩转Android---2D图形及动画---Frame动画
- 玩转Android---2D图形及动画---View类使用
- 玩转Android---组件篇---SeekBar,RatingBar,Chronometer
- 玩转Android---组件篇---Handler的使用(2)
- 让你的 EditText 全部清除
- Android的ListView中给RadioGroup添加setOnCheckChangeListener的问题之一
- android省市区三级联动案例:(三)主要功能实现
- android省市区三级联动案例:(三)布局文件
- Android内存泄漏分析及调试
- 关于 android 程序中的 context
- Chromium on Android: Android在系统Chromium为了实现主消息循环分析
- android省市区三级联动案例:(一)运行效果
- Android中Handler的简单应用(一)
- android ViewDragHelper完全解析 自定义ViewGroup神器
- android沉浸式状态栏实现细节