本篇文章用于实现Android的gif动画


第一步:找到需要实现动画效果的gif动画图片


第二步:新建Android工程


第三步:添加实现gif动画的源文件

1.GifDecoder.xml

public class GifDecoder{private intE0;private intE1[];private intE2;private intE6;private booleanE7;private intE8[];private intwidth;private intheight;private intED;private booleanEE;private booleanEF;private intF0[];private intF1;private booleanF2;private intF3;private longF4;private intF5;private static final intF6[]= { 8, 8, 4, 2 };private static final intF8[]= { 0, 4, 2, 1 };intcurFrame;intpoolsize;intFA;byteC2[];intFB;intFC;intFD;public GifDecoder(byte abyte0[]){E0 = -1;E1 = new int[280];E2 = -1;E6 = 0;E7 = false;E8 = null;width = 0;height = 0;ED = 0;EE = false;EF = false;F0 = null;F1 = 0;F5 = 0;curFrame = 0;C2 = abyte0;poolsize = C2.length;FA = 0;}public boolean moreFrames(){return poolsize - FA >= 16;}public void nextFrame(){curFrame++;}public Bitmap decodeImage(){return decodeImage(curFrame);}public Bitmap decodeImage(int i){if (i <= E0){return null;}if (E0 < 0){if (!E3()){return null;}if (!E4()){return null;}}do{if (!E9(1)){return null;}int j = E1[0];if (j == 59){return null;}if (j == 33){if (!E7()){return null;}}else if (j == 44){if (!E5()){return null;}Bitmap image = createImage();E0++;if (E0 < i){image = null;}else{return image;}}}while (true);}public void clear(){C2 = null;E1 = null;E8 = null;F0 = null;}private Bitmap createImage(){int i = width;int j = height;int j1 = 0;int k1 = 0;int ai[] = new int[4096];int ai1[] = new int[4096];int ai2[] = new int[8192];if (!E9(1)){return null;}int k = E1[0];int[] image = new int[width * height];int ai3[] = E8;if (EE){ai3 = F0;}if (E2 >= 0){ai3[E2] = 0xffffff;}int l2 = 1 << k;int j3 = l2 + 1;int k2 = k + 1;int l3 = l2 + 2;int k3 = -1;int j4 = -1;for (int l1 = 0; l1 < l2; l1++){ai1[l1] = l1;}int j2 = 0;E2();j1 = 0;label0: for (int i2 = 0; i2 < j; i2++){int i1 = 0;do{if (i1 >= i){break;}if (j2 == 0){int i4 = E1(k2);if (i4 < 0){return Bitmap.createBitmap(image, width, height, Config.RGB_565);}if (i4 > l3 || i4 == j3){return Bitmap.createBitmap(image, width, height, Config.RGB_565);}if (i4 == l2){k2 = k + 1;l3 = l2 + 2;k3 = -1;continue;}if (k3 == -1){ai2[j2++] = ai1[i4];k3 = i4;j4 = i4;continue;}int i3 = i4;if (i4 == l3){ai2[j2++] = j4;i4 = k3;}for (; i4 > l2; i4 = ai[i4]){ai2[j2++] = ai1[i4];}j4 = ai1[i4];if (l3 >= 4096){return Bitmap.createBitmap(image, width, height, Config.RGB_565);}ai2[j2++] = j4;ai[l3] = k3;ai1[l3] = j4;if (++l3 >= 1 << k2 && l3 < 4096){k2++;}k3 = i3;}int l = ai2[--j2];if (l < 0){return Bitmap.createBitmap(image, width, height, Config.RGB_565);}if (i1 == 0){FC = 0;FB = ai3[l];FD = 0;}else if (FB != ai3[l]){for (int mm = FD; mm <= FD + FC; mm++){image[j1 * width + mm] = FB;}FC = 0;FB = ai3[l];FD = i1;if (i1 == i - 1){image[j1 * width + i1] = ai3[l];}}else{FC++;if (i1 == i - 1){for (int mm = FD; mm <= FD + FC; mm++){image[j1 * width + mm] = FB;}}}i1++;}while (true);if (EF){j1 += F6[k1];do{if (j1 < j){continue label0;}if (++k1 > 3){return Bitmap.createBitmap(image, width, height, Config.RGB_565);}j1 = F8[k1];}while (true);}j1++;}return Bitmap.createBitmap(image, width, height, Config.RGB_565);}private int E1(int i){while (F5 < i){if (F2){return -1;}if (F1 == 0){F1 = E8();F3 = 0;if (F1 <= 0){F2 = true;break;}}F4 += E1[F3] << F5;F3++;F5 += 8;F1--;}int j = (int) F4 & (1 << i) - 1;F4 >>= i;F5 -= i;return j;}private void E2(){F5 = 0;F1 = 0;F4 = 0L;F2 = false;F3 = -1;}private boolean E3(){if (!E9(6)){return false;}return E1[0] == 71 && E1[1] == 73 && E1[2] == 70 && E1[3] == 56 && (E1[4] == 55 || E1[4] == 57) && E1[5] == 97;}private boolean E4(){if (!E9(7)){return false;}int i = E1[4];E6 = 2 << (i & 7);E7 = EB(i, 128);E8 = null;return !E7 || E6(E6, true);}private boolean E5(){if (!E9(9)){return false;}width = EA(E1[4], E1[5]);height = EA(E1[6], E1[7]);int i = E1[8];EE = EB(i, 128);ED = 2 << (i & 7);EF = EB(i, 64);F0 = null;return !EE || E6(ED, false);}private boolean E6(int i, boolean flag){int ai[] = new int[i];for (int j = 0; j < i; j++){if (!E9(3)){return false;}ai[j] = E1[0] << 16 | E1[1] << 8 | E1[2] | 0xff000000;}if (flag){E8 = ai;}else{F0 = ai;}return true;}private boolean E7(){if (!E9(1)){return false;}int i = E1[0];int j = -1;switch (i){case 249:j = E8();if (j < 0){return true;}if ((E1[0] & 1) != 0){E2 = E1[3];}else{E2 = -1;}break;}do{j = E8();}while (j > 0);return true;}private int E8(){if (!E9(1)){return -1;}int i = E1[0];if (i != 0 && !E9(i)){return -1;}else{return i;}}private boolean E9(int i){if (FA + i >= poolsize){return false;}for (int j = 0; j < i; j++){int k = C2[FA + j];if (k < 0){k += 256;}E1[j] = k;}FA += i;return true;}private static final int EA(int i, int j){return j << 8 | i;}private static final boolean EB(int i, int j){return (i & j) == j;}}

2.GifFrame.xml

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

3.GameView.xml

注意:在这个类中GameView函数中解析GIF动画处将资源名称换成自己的资源

public class GameView extends View implements Runnable{ContextmContext= null;/* 声明GifFrame对象 */GifFramemGifFrame= null;public GameView(Context context){super(context);setBackgroundColor(Color.BLACK);mContext = context;/* 解析GIF动画 */mGifFrame=GifFrame.CreateGifImage(fileConnect(this.getResources().openRawResource(R.drawable.gif1)));//将这换成资源名称即可/* 开启线程 */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(500);//设置延时}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;}}}

第四步:在主界面显示

主界面中将显示视图设置为自己刚才设置的视图

public class MainActivity extends Activity{private GameViewmGameView= null;@Overrideprotected void onCreate(Bundle savedInstanceState){//GIF动画界面super.onCreate(savedInstanceState);mGameView = new GameView(this);setContentView(mGameView);}}

第五步:查看效果

下面是我添加的两个GIF动画实现后的截图



源码下载



更多相关文章

  1. 【边做项目边学Android】手机安全卫士05_1:程序主界面
  2. android界面布局
  3. android技术开发例子,解析以及源码集锦
  4. android启动画面
  5. Intent实现Android间的页面跳转
  6. Android之Handler用法总结
  7. Android消息处理系统
  8. Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
  9. 享受Android应用程序的Java技术盛宴

随机推荐

  1. Android(安卓)Translucent System Bar 的
  2. android:autoLink和Linkify
  3. Android(安卓)自定义UI圆角按钮
  4. View 控件EditText属性
  5. Say Hello To Android
  6. 在模拟器上运行编译好的android
  7. 《Android开发从零开始》——13.Table La
  8. 关于android中的内部存储与外部存储
  9. Android相对布局实现各种梅花效果
  10. Android 并发之Handler、Looper、Message