Android中Gif圖片的顯示
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Android中Gif圖片的顯示
分類: android 2011-09-07 16:28 1620人閱讀 評論(2)最近閒來無事,折騰了一下關於gif圖片在Android上的顯示(大家都知道,Android本身不支援gif圖片的顯示,當然通過Media還是能夠實現gif的播放的)。網上找到的實現gif圖片展示的主要是兩種方式:使用java實現解碼,或者使用編輯工具將gif圖片拆分為多張圖片,並編寫xml檔案,以幀動畫的形式播放,另外還有個牛人,直接修改了Android框架層的原始碼,讓android系統支援gif解碼的。
最後,我參考了一個android的開源專案,gifView,實現了一個基於native層的gif解碼。
以下是我參考的資料:
解碼的演算法是直接抄襲了GifView,基本上就是C++語言重新實現了一下,解碼重寫了一下SurfaceView控制元件來實現gif的播放。以下貼上部分的核心程式碼:
Gif.java
[java] view plain copy- package com.ray.test.gif;
- import java.util.ArrayList;
- public class Gif {
- public class Frame {
- private int delayTime;
- private Bitmap image;
- private boolean userInput = false;
- public Frame(int delay, int[] color) {
- delayTime = delay;
- image = Bitmap.createBitmap(color, mWidth, mHeight, Config.RGB_565);
- }
- private Frame setUserInput() {
- userInput = true;
- return this;
- }
- public int getDelay() {
- return delayTime;
- }
- public Bitmap getImage() {
- return image;
- }
- public boolean isUserInput() {
- return userInput;
- }
- }
- private int mWidth;
- private int mHeight;
- private List<Frame> mFrames = new ArrayList<Frame>();
- public Gif(int width, int height) {
- mWidth = width;
- mHeight = height;
- }
- public int getWidth() {
- return mWidth;
- }
- public int getHeight() {
- return mHeight;
- }
- public void addFrame(int delay, int[] color, boolean userInput) {
- synchronized (mFrames) {
- if (!userInput)
- mFrames.add(new Frame(delay, color));
- else
- mFrames.add(new Frame(delay, color).setUserInput());
- }
- }
- public int getFrameCount() {
- synchronized (mFrames) {
- return mFrames.size();
- }
- }
- public Frame getFrame(int idx) {
- synchronized (mFrames) {
- if (idx < 0 || idx >= mFrames.size())
- return null;
- return mFrames.get(idx);
- }
- }
- }
GifDecoder.java
[java] view plain copy- package com.ray.test.gif;
- import java.io.File;
- public class GifDecoder {
- private static final String MYTAG = "Ray";
- private static final String CLASS_NAME = "GifDecoder";
- public interface DecodeResult {
- public void onDecodeFinished(int count);
- }
- private static Gif sGif;
- private static DecodeResult sListener;
- private static boolean sIsReady = false;
- static void decode(String filePath, DecodeResult result) throws FileNotFoundException {
- File f = new File(filePath);
- if (f.exists()) {
- sListener = result;
- sIsReady = false;
- sGif = null;
- WorkThread thread = new WorkThread(filePath);
- thread.start();
- } else
- throw new FileNotFoundException("can not find file:" + filePath);
- }
- static Gif getImage() {
- return sGif;
- }
- private static void onDecodeFinished(String count) {
- Log.d(MYTAG, CLASS_NAME + ": onDecodeFinished, count = " + count);
- int c = Integer.parseInt(count);
- getFrames(c);
- if(c == 0)
- mHandler.obtainMessage(c).sendToTarget();
- }
- private static void getFrames(int idx) {
- if(idx == 0)
- sGif = new Gif(getWidth(), getHeight());
- sGif.addFrame(getDelay(idx), getColors(idx), getUserInput(idx));
- }
- private static
最近閒來無事,折騰了一下關於gif圖片在Android上的顯示(大家都知道,Android本身不支援gif圖片的顯示,當然通過Media還是能夠實現gif的播放的)。網上找到的實現gif圖片展示的主要是兩種方式:使用java實現解碼,或者使用編輯工具將gif圖片拆分為多張圖片,並編寫xml檔案,以幀動畫的形式播放,另外還有個牛人,直接修改了Android框架層的原始碼,讓android系統支援gif解碼的。
最後,我參考了一個android的開源專案,gifView,實現了一個基於native層的gif解碼。
以下是我參考的資料:
解碼的演算法是直接抄襲了GifView,基本上就是C++語言重新實現了一下,解碼重寫了一下SurfaceView控制元件來實現gif的播放。以下貼上部分的核心程式碼:
Gif.java
[java] view plain copy- package com.ray.test.gif;
- import java.util.ArrayList;
- public class Gif {
- public class Frame {
- private int delayTime;
- private Bitmap image;
- private boolean userInput = false;
- public Frame(int delay, int[] color) {
- delayTime = delay;
- image = Bitmap.createBitmap(color, mWidth, mHeight, Config.RGB_565);
- }
- private Frame setUserInput() {
- userInput = true;
- return this;
- }
- public int getDelay() {
- return delayTime;
- }
- public Bitmap getImage() {
- return image;
- }
- public boolean isUserInput() {
- return userInput;
- }
- }
- private int mWidth;
- private int mHeight;
- private List<Frame> mFrames = new ArrayList<Frame>();
- public Gif(int width, int height) {
- mWidth = width;
- mHeight = height;
- }
- public int getWidth() {
- return mWidth;
- }
- public int getHeight() {
- return mHeight;
- }
- public void addFrame(int delay, int[] color, boolean userInput) {
- synchronized (mFrames) {
- if (!userInput)
- mFrames.add(new Frame(delay, color));
- else
- mFrames.add(new Frame(delay, color).setUserInput());
- }
- }
- public int getFrameCount() {
- synchronized (mFrames) {
- return mFrames.size();
- }
- }
- public Frame getFrame(int idx) {
- synchronized (mFrames) {
- if (idx < 0 || idx >= mFrames.size())
- return null;
- return mFrames.get(idx);
- }
- }
- }
GifDecoder.java
[java] view plain copy- package com.ray.test.gif;
- import java.io.File;
- public class GifDecoder {
- private static final String MYTAG = "Ray";
- private static final String CLASS_NAME = "GifDecoder";
- public interface DecodeResult {
- public void onDecodeFinished(int count);
- }
- private static Gif sGif;
- private static DecodeResult sListener;
- private static boolean sIsReady = false;
- static void decode(String filePath, DecodeResult result) throws FileNotFoundException {
- File f = new File(filePath);
- if (f.exists()) {
- sListener = result;
- sIsReady = false;
- sGif = null;
- WorkThread thread = new WorkThread(filePath);
- thread.start();
- } else
- throw new FileNotFoundException("can not find file:" + filePath);
- }
- static Gif getImage() {
- return sGif;
- }
- private static void onDecodeFinished(String count) {
- Log.d(MYTAG, CLASS_NAME + ": onDecodeFinished, count = " + count);
- int c = Integer.parseInt(count);
- getFrames(c);
- if(c == 0)
- mHandler.obtainMessage(c).sendToTarget();
- }
- private static void getFrames(int idx) {
- if(idx == 0)
- sGif = new Gif(getWidth(), getHeight());
- sGif.addFrame(getDelay(idx), getColors(idx), getUserInput(idx));
- }
- private static