Android中Gif图片的显示

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Android中Gif图片的显示

分类: android   1620人阅读  评论(2)  收藏  举报

         最近闲来无事,折腾了一下关于gif图片在Android上的显示(大家都知道,Android本身不支持gif图片的显示,当然通过Media还是能够实现gif的播放的)。网上找到的实现gif图片展示的主要是两种方式:使用java实现解码,或者使用编辑工具将gif图片拆分为多张图片,并编写xml文件,以帧动画的形式播放,另外还有个牛人,直接修改了Android框架层的源码,让android系统支持gif解码的。

        最后,我参考了一个android的开源项目,gifView,实现了一个基于native层的gif解码。

        以下是我参考的资料:

         gif文件格式说明

         LZW编码

         LZW算法和GIF数据压缩

         gifView项目

          解码的算法是直接抄袭了GifView,基本上就是C++语言重新实现了一下,解码重写了一下SurfaceView控件来实现gif的播放。以下贴上部分的核心代码:

          Gif.java

[java]  view plain copy
  1. package com.ray.test.gif;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class Gif {  
  6.     public class Frame {  
  7.         private int delayTime;  
  8.         private Bitmap image;  
  9.         private boolean userInput = false;  
  10.   
  11.         public Frame(int delay, int[] color) {  
  12.             delayTime = delay;  
  13.             image = Bitmap.createBitmap(color, mWidth, mHeight, Config.RGB_565);  
  14.         }  
  15.   
  16.         private Frame setUserInput() {  
  17.             userInput = true;  
  18.             return this;  
  19.         }  
  20.   
  21.         public int getDelay() {  
  22.             return delayTime;  
  23.         }  
  24.   
  25.         public Bitmap getImage() {  
  26.             return image;  
  27.         }  
  28.   
  29.         public boolean isUserInput() {  
  30.             return userInput;  
  31.         }  
  32.     }  
  33.   
  34.     private int mWidth;  
  35.     private int mHeight;  
  36.     private List<Frame> mFrames = new ArrayList<Frame>();  
  37.   
  38.     public Gif(int width, int height) {  
  39.         mWidth = width;  
  40.         mHeight = height;  
  41.     }  
  42.   
  43.     public int getWidth() {  
  44.         return mWidth;  
  45.     }  
  46.   
  47.     public int getHeight() {  
  48.         return mHeight;  
  49.     }  
  50.   
  51.     public void addFrame(int delay, int[] color, boolean userInput) {  
  52.         synchronized (mFrames) {  
  53.             if (!userInput)  
  54.                 mFrames.add(new Frame(delay, color));  
  55.             else  
  56.                 mFrames.add(new Frame(delay, color).setUserInput());  
  57.         }  
  58.     }  
  59.   
  60.     public int getFrameCount() {  
  61.         synchronized (mFrames) {  
  62.             return mFrames.size();  
  63.         }  
  64.     }  
  65.   
  66.     public Frame getFrame(int idx) {  
  67.         synchronized (mFrames) {  
  68.             if (idx < 0 || idx >= mFrames.size())  
  69.                 return null;  
  70.             return mFrames.get(idx);  
  71.         }  
  72.     }  
  73. }  

GifDecoder.java

[java]  view plain copy
  1. package com.ray.test.gif;  
  2.   
  3. import java.io.File;  
  4.   
  5. public class GifDecoder {  
  6.   
  7.     private static final String MYTAG = "Ray";  
  8.     private static final String CLASS_NAME = "GifDecoder";  
  9.   
  10.     public interface DecodeResult {  
  11.         public void onDecodeFinished(int count);  
  12.     }  
  13.   
  14.     private static Gif sGif;  
  15.     private static DecodeResult sListener;  
  16.     private static boolean sIsReady = false;  
  17.   
  18.     static void decode(String filePath, DecodeResult result) throws FileNotFoundException {  
  19.         File f = new File(filePath);  
  20.         if (f.exists()) {  
  21.             sListener = result;  
  22.             sIsReady = false;  
  23.             sGif = null;  
  24.             WorkThread thread = new WorkThread(filePath);  
  25.             thread.start();  
  26.         } else  
  27.             throw new FileNotFoundException("can not find file:" + filePath);  
  28.     }  
  29.   
  30.     static Gif getImage() {  
  31.         return sGif;  
  32.     }  
  33.   
  34.     private static void onDecodeFinished(String count) {  
  35.         Log.d(MYTAG, CLASS_NAME + ": onDecodeFinished, count = " + count);  
  36.         int c = Integer.parseInt(count);  
  37.         getFrames(c);  
  38.         if(c == 0)  
  39.             mHandler.obtainMessage(c).sendToTarget();  
  40.     }  
  41.   
  42.     private static void getFrames(int idx) {  
  43.         if(idx == 0)  
  44.             sGif = new Gif(getWidth(), getHeight());  
  45.         sGif.addFrame(getDelay(idx), getColors(idx), getUserInput(idx));  
  46.     }  
  47.   
  48.     private static 

         最近闲来无事,折腾了一下关于gif图片在Android上的显示(大家都知道,Android本身不支持gif图片的显示,当然通过Media还是能够实现gif的播放的)。网上找到的实现gif图片展示的主要是两种方式:使用java实现解码,或者使用编辑工具将gif图片拆分为多张图片,并编写xml文件,以帧动画的形式播放,另外还有个牛人,直接修改了Android框架层的源码,让android系统支持gif解码的。

        最后,我参考了一个android的开源项目,gifView,实现了一个基于native层的gif解码。

        以下是我参考的资料:

         gif文件格式说明

         LZW编码

         LZW算法和GIF数据压缩

         gifView项目

          解码的算法是直接抄袭了GifView,基本上就是C++语言重新实现了一下,解码重写了一下SurfaceView控件来实现gif的播放。以下贴上部分的核心代码:

          Gif.java

[java]  view plain copy
  1. package com.ray.test.gif;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class Gif {  
  6.     public class Frame {  
  7.         private int delayTime;  
  8.         private Bitmap image;  
  9.         private boolean userInput = false;  
  10.   
  11.         public Frame(int delay, int[] color) {  
  12.             delayTime = delay;  
  13.             image = Bitmap.createBitmap(color, mWidth, mHeight, Config.RGB_565);  
  14.         }  
  15.   
  16.         private Frame setUserInput() {  
  17.             userInput = true;  
  18.             return this;  
  19.         }  
  20.   
  21.         public int getDelay() {  
  22.             return delayTime;  
  23.         }  
  24.   
  25.         public Bitmap getImage() {  
  26.             return image;  
  27.         }  
  28.   
  29.         public boolean isUserInput() {  
  30.             return userInput;  
  31.         }  
  32.     }  
  33.   
  34.     private int mWidth;  
  35.     private int mHeight;  
  36.     private List<Frame> mFrames = new ArrayList<Frame>();  
  37.   
  38.     public Gif(int width, int height) {  
  39.         mWidth = width;  
  40.         mHeight = height;  
  41.     }  
  42.   
  43.     public int getWidth() {  
  44.         return mWidth;  
  45.     }  
  46.   
  47.     public int getHeight() {  
  48.         return mHeight;  
  49.     }  
  50.   
  51.     public void addFrame(int delay, int[] color, boolean userInput) {  
  52.         synchronized (mFrames) {  
  53.             if (!userInput)  
  54.                 mFrames.add(new Frame(delay, color));  
  55.             else  
  56.                 mFrames.add(new Frame(delay, color).setUserInput());  
  57.         }  
  58.     }  
  59.   
  60.     public int getFrameCount() {  
  61.         synchronized (mFrames) {  
  62.             return mFrames.size();  
  63.         }  
  64.     }  
  65.   
  66.     public Frame getFrame(int idx) {  
  67.         synchronized (mFrames) {  
  68.             if (idx < 0 || idx >= mFrames.size())  
  69.                 return null;  
  70.             return mFrames.get(idx);  
  71.         }  
  72.     }  
  73. }  

GifDecoder.java

[java]  view plain copy
  1. package com.ray.test.gif;  
  2.   
  3. import java.io.File;  
  4.   
  5. public class GifDecoder {  
  6.   
  7.     private static final String MYTAG = "Ray";  
  8.     private static final String CLASS_NAME = "GifDecoder";  
  9.   
  10.     public interface DecodeResult {  
  11.         public void onDecodeFinished(int count);  
  12.     }  
  13.   
  14.     private static Gif sGif;  
  15.     private static DecodeResult sListener;  
  16.     private static boolean sIsReady = false;  
  17.   
  18.     static void decode(String filePath, DecodeResult result) throws FileNotFoundException {  
  19.         File f = new File(filePath);  
  20.         if (f.exists()) {  
  21.             sListener = result;  
  22.             sIsReady = false;  
  23.             sGif = null;  
  24.             WorkThread thread = new WorkThread(filePath);  
  25.             thread.start();  
  26.         } else  
  27.             throw new FileNotFoundException("can not find file:" + filePath);  
  28.     }  
  29.   
  30.     static Gif getImage() {  
  31.         return sGif;  
  32.     }  
  33.   
  34.     private static void onDecodeFinished(String count) {  
  35.         Log.d(MYTAG, CLASS_NAME + ": onDecodeFinished, count = " + count);  
  36.         int c = Integer.parseInt(count);  
  37.         getFrames(c);  
  38.         if(c == 0)  
  39.             mHandler.obtainMessage(c).sendToTarget();  
  40.     }  
  41.   
  42.     private static void getFrames(int idx) {  
  43.         if(idx == 0)  
  44.             sGif = new Gif(getWidth(), getHeight());  
  45.         sGif.addFrame(getDelay(idx), getColors(idx), getUserInput(idx));  
  46.     }  
  47.   
  48.     private static 

猜你喜欢

转载自blog.csdn.net/hggjgff/article/details/83858272
今日推荐