How to play GIF file in Android??

Put your problem here if it does not fit any of the other categories.

Postby judi » Wed Sep 02, 2009 9:45 am

This code is not running.....Plz explain in detail
judi
Freshman
Freshman
 
Posts: 4
Joined: Mon Aug 24, 2009 9:37 am

Top

Postby quocvietit » Fri Sep 04, 2009 4:06 pm

this code is not available SDK 1.5 any more.i find out function decode movie always return null.it's a bug.hope it will be fixed soon
quocvietit
Freshman
Freshman
 
Posts: 4
Joined: Mon Aug 24, 2009 9:04 am

Postby genxsol » Fri Sep 25, 2009 2:57 pm

i have the same NullPointerException and it seems Movie.decodeStream(is) returns null to the movie object
genxsol
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Feb 17, 2009 11:24 am

Postby greenlgy » Mon Sep 28, 2009 3:16 pm

alpesh_harsoda wrote:ya sure why not.
this is the code but it has problem of flickering the screen when the gif file is large. :(
so if u find the solution then tell me.
:)


Can you make it into a components? Can be used directly in the xml. If so, very grateful.

Eg :
<GIFView
//......
//......
>
</GIFView>
greenlgy
Freshman
Freshman
 
Posts: 2
Joined: Mon Sep 28, 2009 3:08 pm

Postby genxsol » Mon Sep 28, 2009 5:14 pm

i cant find <GIFView> tag in android sdk.
can you explain how did u use it plz?
genxsol
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Feb 17, 2009 11:24 am

Postby greenlgy » Wed Oct 14, 2009 2:53 pm

genxsol wrote:i cant find <GIFView> tag in android sdk.
can you explain how did u use it plz?


I want to make it into a component,

Becasue some people say that custom label can be used in xml, as long as you use the class full name , eg :
<com.mygif.GIFView
// ...
// ...
>
</com.mygif.GIFView>

but, i try it failed.
who can tell me how do it.
greenlgy
Freshman
Freshman
 
Posts: 2
Joined: Mon Sep 28, 2009 3:08 pm

Top

Playing GIF for SDK >= 1.5

Postby 144key » Fri Apr 16, 2010 2:42 pm

For gif player using SDK1.5, recently i've been using a code-porting of GIFdecoder that i've got from
www.fmsware.com. Initially, this is a j2se code, here the ported code :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.net.*;
  3.  
  4. import java.io.*;
  5.  
  6. import java.util.*;
  7.  
  8. import android.graphics.*;
  9.  
  10. import android.graphics.Paint.Style;
  11.  
  12.  
  13.  
  14. /**
  15.  
  16.  * Class GifDecoder - Decodes a GIF file into one or more frames.
  17.  
  18.  * <br><pre>
  19.  
  20.  * Example:
  21.  
  22.  *    GifDecoder d = new GifDecoder();
  23.  
  24.  *    d.read("sample.gif");
  25.  
  26.  *    int n = d.getFrameCount();
  27.  
  28.  *    for (int i = 0; i < n; i++) {
  29.  
  30.  *       BufferedImage frame = d.getFrame(i);  // frame i
  31.  
  32.  *       int t = d.getDelay(i);  // display duration of frame in milliseconds
  33.  
  34.  *       // do something with frame
  35.  
  36.  *    }
  37.  
  38.  * </pre>
  39.  
  40.  * No copyright asserted on the source code of this class.  May be used for
  41.  
  42.  * any purpose, however, refer to the Unisys LZW patent for any additional
  43.  
  44.  * restrictions.  Please forward any corrections to kweiner@fmsware.com.
  45.  
  46.  *
  47.  
  48.  * @author Kevin Weiner, FM Software; LZW decoder adapted from John Cristy's ImageMagick.
  49.  
  50.  * @version 1.03 November 2003
  51.  
  52.  *
  53.  
  54.  */
  55.  
  56.  
  57.  
  58. public class GifDecoder {
  59.  
  60.  
  61.  
  62.   /**
  63.  
  64.    * File read status: No errors.
  65.  
  66.    */
  67.  
  68.   public static final int STATUS_OK = 0;
  69.  
  70.  
  71.  
  72.   /**
  73.  
  74.    * File read status: Error decoding file (may be partially decoded)
  75.  
  76.    */
  77.  
  78.   public static final int STATUS_FORMAT_ERROR = 1;
  79.  
  80.  
  81.  
  82.   /**
  83.  
  84.    * File read status: Unable to open source.
  85.  
  86.    */
  87.  
  88.   public static final int STATUS_OPEN_ERROR = 2;
  89.  
  90.  
  91.  
  92.   protected BufferedInputStream in;
  93.  
  94.   protected int status;
  95.  
  96.  
  97.  
  98.   protected int width; // full image width
  99.  
  100.   protected int height; // full image height
  101.  
  102.   protected boolean gctFlag; // global color table used
  103.  
  104.   protected int gctSize; // size of global color table
  105.  
  106.   protected int loopCount = 1; // iterations; 0 = repeat forever
  107.  
  108.  
  109.  
  110.   protected int[] gct; // global color table
  111.  
  112.   protected int[] lct; // local color table
  113.  
  114.   protected int[] act; // active color table
  115.  
  116.  
  117.  
  118.   protected int bgIndex; // background color index
  119.  
  120.   protected int bgColor; // background color
  121.  
  122.   protected int lastBgColor; // previous bg color
  123.  
  124.   protected int pixelAspect; // pixel aspect ratio
  125.  
  126.  
  127.  
  128.   protected boolean lctFlag; // local color table flag
  129.  
  130.   protected boolean interlace; // interlace flag
  131.  
  132.   protected int lctSize; // local color table size
  133.  
  134.  
  135.  
  136.   protected int ix, iy, iw, ih; // current image rectangle
  137.  
  138.   protected Rect lastRect; // last image rect
  139.  
  140.   protected Bitmap image; // current frame
  141.  
  142.   protected Bitmap lastImage; // previous frame
  143.  
  144.  
  145.  
  146.   protected byte[] block = new byte[256]; // current data block
  147.  
  148.   protected int blockSize = 0; // block size
  149.  
  150.  
  151.  
  152.   // last graphic control extension info
  153.  
  154.   protected int dispose = 0;
  155.  
  156.   // 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev
  157.  
  158.   protected int lastDispose = 0;
  159.  
  160.   protected boolean transparency = false; // use transparent color
  161.  
  162.   protected int delay = 0; // delay in milliseconds
  163.  
  164.   protected int transIndex; // transparent color index
  165.  
  166.  
  167.  
  168.   protected static final int MaxStackSize = 4096;
  169.  
  170.   // max decoder pixel stack size
  171.  
  172.  
  173.  
  174.   // LZW decoder working arrays
  175.  
  176.   protected short[] prefix;
  177.  
  178.   protected byte[] suffix;
  179.  
  180.   protected byte[] pixelStack;
  181.  
  182.   protected byte[] pixels;
  183.  
  184.  
  185.  
  186.   protected ArrayList frames; // frames read from current file
  187.  
  188.   protected int frameCount;
  189.  
  190.  
  191.  
  192.   static class GifFrame {
  193.  
  194.     public GifFrame(Bitmap im, int del) {
  195.  
  196.       image = im;
  197.  
  198.       delay = del;
  199.  
  200.     }
  201.  
  202.     public Bitmap image;
  203.  
  204.     public int delay;
  205.  
  206.   }
  207.  
  208.  
  209.  
  210.   /**
  211.  
  212.    * Gets display duration for specified frame.
  213.  
  214.    *
  215.  
  216.    * @param n int index of frame
  217.  
  218.    * @return delay in milliseconds
  219.  
  220.    */
  221.  
  222.   public int getDelay(int n) {
  223.  
  224.     //
  225.  
  226.     delay = -1;
  227.  
  228.     if ((n >= 0) && (n < frameCount)) {
  229.  
  230.       delay = ((GifFrame) frames.get(n)).delay;
  231.  
  232.     }
  233.  
  234.     return delay;
  235.  
  236.   }
  237.  
  238.  
  239.  
  240.   /**
  241.  
  242.    * Gets the number of frames read from file.
  243.  
  244.    * @return frame count
  245.  
  246.    */
  247.  
  248.   public int getFrameCount() {
  249.  
  250.     return frameCount;
  251.  
  252.   }
  253.  
  254.  
  255.  
  256.   /**
  257.  
  258.    * Gets the first (or only) image read.
  259.  
  260.    *
  261.  
  262.    * @return BufferedImage containing first frame, or null if none.
  263.  
  264.    */
  265.  
  266.   public Bitmap getImage() {
  267.  
  268.     return getFrame(0);
  269.  
  270.   }
  271.  
  272.  
  273.  
  274.   /**
  275.  
  276.    * Gets the "Netscape" iteration count, if any.
  277.  
  278.    * A count of 0 means repeat indefinitiely.
  279.  
  280.    *
  281.  
  282.    * @return iteration count if one was specified, else 1.
  283.  
  284.    */
  285.  
  286.   public int getLoopCount() {
  287.  
  288.     return loopCount;
  289.  
  290.   }
  291.  
  292.  
  293.  
  294.   public int[] copyPixelToSetInteger(Bitmap B) {
  295.  
  296.     System.out.println("GIF >> "+B.getWidth()+" X "+B.getHeight());
  297.  
  298.     int[] tmp = new int[B.getWidth()*B.getHeight()];
  299.  
  300.     for (int i = 0;i<B.getHeight();i++) {
  301.  
  302.         for (int j = 0;j<B.getWidth();j++) {
  303.  
  304.             tmp[j + i * B.getWidth()] = B.getPixel(j, i);
  305.  
  306.         }
  307.  
  308.     }
  309.  
  310.     return tmp;
  311.  
  312.   }
  313.  
  314.  
  315.  
  316.   /**
  317.  
  318.    * Creates new frame image from current data (and previous
  319.  
  320.    * frames as specified by their disposition codes).
  321.  
  322.    */
  323.  
  324.   protected void setPixels() {
  325.  
  326.     // expose destination image's pixels as int array
  327.  
  328.     int[] dest = copyPixelToSetInteger(image);
  329.  
  330.       // ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // 144key
  331.  
  332.  
  333.  
  334.     // fill in starting image contents based on last image's dispose code
  335.  
  336.     if (lastDispose > 0) {
  337.  
  338.       if (lastDispose == 3) {
  339.  
  340.         // use image before last
  341.  
  342.         int n = frameCount - 2;
  343.  
  344.         if (n > 0) {
  345.  
  346.           lastImage = getFrame(n - 1);
  347.  
  348.         } else {
  349.  
  350.           lastImage = null;
  351.  
  352.         }
  353.  
  354.       }
  355.  
  356.  
  357.  
  358.       if (lastImage != null) {
  359.  
  360.         int[] prev = copyPixelToSetInteger(lastImage);
  361.  
  362.           // ((DataBufferInt) lastImage.getRaster().getDataBuffer()).getData(); // 144key
  363.  
  364.         System.arraycopy(prev, 0, dest, 0, width * height);
  365.  
  366.         // copy pixels
  367.  
  368.  
  369.  
  370.         if (lastDispose == 2) {
  371.  
  372.             // fill last image rect area with background color
  373.  
  374.             System.gc();
  375.  
  376.             Paint paint = new Paint();
  377.  
  378.             paint.setStyle(Style.FILL);
  379.  
  380.             if (transparency) {
  381.  
  382.                 paint.setARGB(0, 0, 0, 0);
  383.  
  384.             } else {
  385.  
  386.                 paint.setColor(lastBgColor);
  387.  
  388.             }
  389.  
  390.             Canvas canvas = new Canvas();
  391.  
  392.             canvas.drawRect(lastRect, paint);
  393.  
  394.  
  395.  
  396.             //Graphics2D g = image.createGraphics();
  397.  
  398.             //Color c = null;
  399.  
  400.             //if (transparency) {
  401.  
  402.                 //c = Color.argb(0, 0, 0, 0); // new Color(0, 0, 0, 0);  // assume background is transparent
  403.  
  404.             //} else {
  405.  
  406.                 //c = new Color(lastBgColor); // use given background color
  407.  
  408.             //}
  409.  
  410.             //g.setColor(c);
  411.  
  412.             //g.setComposite(AlphaComposite.Src); // replace area
  413.  
  414.             //g.fill(lastRect);
  415.  
  416.             //g.dispose();
  417.  
  418.         }
  419.  
  420.       }
  421.  
  422.     }
  423.  
  424.  
  425.  
  426.     // copy each source line to the appropriate place in the destination
  427.  
  428.     int pass = 1;
  429.  
  430.     int inc = 8;
  431.  
  432.     int iline = 0;
  433.  
  434.     for (int i = 0; i < ih; i++) {
  435.  
  436.       int line = i;
  437.  
  438.       if (interlace) {
  439.  
  440.         if (iline >= ih) {
  441.  
  442.           pass++;
  443.  
  444.           switch (pass) {
  445.  
  446.             case 2 :
  447.  
  448.               iline = 4;
  449.  
  450.               break;
  451.  
  452.             case 3 :
  453.  
  454.               iline = 2;
  455.  
  456.               inc = 4;
  457.  
  458.               break;
  459.  
  460.             case 4 :
  461.  
  462.               iline = 1;
  463.  
  464.               inc = 2;
  465.  
  466.           }
  467.  
  468.         }
  469.  
  470.         line = iline;
  471.  
  472.         iline += inc;
  473.  
  474.       }
  475.  
  476.       line += iy;
  477.  
  478.       if (line < height) {
  479.  
  480.         int k = line * width;
  481.  
  482.         int dx = k + ix; // start of line in dest
  483.  
  484.         int dlim = dx + iw; // end of dest line
  485.  
  486.         if ((k + width) < dlim) {
  487.  
  488.           dlim = k + width; // past dest edge
  489.  
  490.         }
  491.  
  492.         int sx = i * iw; // start of line in source
  493.  
  494.         while (dx < dlim) {
  495.  
  496.           // map color and insert in destination
  497.  
  498.           int index = ((int) pixels[sx++]) & 0xff;
  499.  
  500.           int c = act[index];
  501.  
  502.           if (c != 0) {
  503.  
  504.             dest[dx] = c;            
  505.  
  506.           }
  507.  
  508.           dx++;
  509.  
  510.         }
  511.  
  512.       }
  513.  
  514.     }
  515.  
  516.     // Final Output
  517.  
  518.     for (int i=0;i<dest.length;i++)
  519.  
  520.         image.setPixel((i % image.getWidth()), ((i-(i % image.getWidth())) / image.getWidth()), dest[i]);
  521.  
  522.   }
  523.  
  524.  
  525.  
  526.   /**
  527.  
  528.    * Gets the image contents of frame n.
  529.  
  530.    *
  531.  
  532.    * @return BufferedImage representation of frame, or null if n is invalid.
  533.  
  534.    */
  535.  
  536.   public Bitmap getFrame(int n) {
  537.  
  538.     System.out.println("GIF FrameCount >> "+frames.size()+" >> "+frameCount);
  539.  
  540.     Bitmap tmp = null;
  541.  
  542.     if ((n >= 0) && (n < frameCount)) {
  543.  
  544.       System.out.println("GIF Frame "+n+" >> image : "+((GifFrame) frames.get(n)).image.toString());
  545.  
  546.       System.out.println("GIF Frame "+n+" >> delay : "+((GifFrame) frames.get(n)).delay);
  547.  
  548.       tmp = ((GifFrame) frames.get(n)).image;
  549.  
  550.     }
  551.  
  552.     return tmp;
  553.  
  554.   }
  555.  
  556.  
  557.  
  558.   /**
  559.  
  560.    * Gets image size.
  561.  
  562.    *
  563.  
  564.    * @return GIF image dimensions
  565.  
  566.    */
  567.  
  568.   //public Dimension getFrameSize() {
  569.  
  570.   //  return new Dimension(width, height);
  571.  
  572.   //} // 144key
  573.  
  574.   public int getWidth() {
  575.  
  576.       return width;
  577.  
  578.   }
  579.  
  580.  
  581.  
  582.   public int getHeight() {
  583.  
  584.       return height;
  585.  
  586.   }
  587.  
  588.  
  589.  
  590.   /**
  591.  
  592.    * Reads GIF image from stream
  593.  
  594.    *
  595.  
  596.    * @param BufferedInputStream containing GIF file.
  597.  
  598.    * @return read status code (0 = no errors)
  599.  
  600.    */
  601.  
  602.   public int read(BufferedInputStream is) {
  603.  
  604.     init();
  605.  
  606.     if (is != null) {
  607.  
  608.       in = is;
  609.  
  610.       readHeader();
  611.  
  612.       if (!err()) {
  613.  
  614.         readContents();
  615.  
  616.         if (frameCount < 0) {
  617.  
  618.           status = STATUS_FORMAT_ERROR;
  619.  
  620.         }
  621.  
  622.       }
  623.  
  624.     } else {
  625.  
  626.       status = STATUS_OPEN_ERROR;
  627.  
  628.     }
  629.  
  630.     try {
  631.  
  632.       is.close();
  633.  
  634.     } catch (IOException e) {
  635.  
  636.     }
  637.  
  638.     return status;
  639.  
  640.   }
  641.  
  642.  
  643.  
  644.   /**
  645.  
  646.    * Reads GIF image from stream
  647.  
  648.    *
  649.  
  650.    * @param InputStream containing GIF file.
  651.  
  652.    * @return read status code (0 = no errors)
  653.  
  654.    */
  655.  
  656.   public int read(InputStream is) {
  657.  
  658.     init();
  659.  
  660.     if (is != null) {
  661.  
  662.       if (!(is instanceof BufferedInputStream))
  663.  
  664.         is = new BufferedInputStream(is);
  665.  
  666.       in = (BufferedInputStream) is;
  667.  
  668.       readHeader();
  669.  
  670.       if (!err()) {
  671.  
  672.         readContents();
  673.  
  674.         if (frameCount < 0) {
  675.  
  676.           status = STATUS_FORMAT_ERROR;
  677.  
  678.         }
  679.  
  680.       }
  681.  
  682.     } else {
  683.  
  684.       status = STATUS_OPEN_ERROR;
  685.  
  686.     }
  687.  
  688.     try {
  689.  
  690.       is.close();
  691.  
  692.     } catch (IOException e) {
  693.  
  694.     }
  695.  
  696.     return status;
  697.  
  698.   }
  699.  
  700.  
  701.  
  702.   /**
  703.  
  704.    * Reads GIF file from specified file/URL source
  705.  
  706.    * (URL assumed if name contains ":/" or "file:")
  707.  
  708.    *
  709.  
  710.    * @param name String containing source
  711.  
  712.    * @return read status code (0 = no errors)
  713.  
  714.    */
  715.  
  716.   public int read(String name) {
  717.  
  718.     status = STATUS_OK;
  719.  
  720.     try {
  721.  
  722.       name = name.trim().toLowerCase();
  723.  
  724.       if ((name.indexOf("file:") >= 0) ||
  725.  
  726.         (name.indexOf(":/") > 0)) {
  727.  
  728.         URL url = new URL(name);
  729.  
  730.         in = new BufferedInputStream(url.openStream());
  731.  
  732.       } else {
  733.  
  734.         in = new BufferedInputStream(new FileInputStream(name));
  735.  
  736.       }
  737.  
  738.       status = read(in);
  739.  
  740.     } catch (IOException e) {
  741.  
  742.       status = STATUS_OPEN_ERROR;
  743.  
  744.     }
  745.  
  746.  
  747.  
  748.     return status;
  749.  
  750.   }
  751.  
  752.  
  753.  
  754.   /**
  755.  
  756.    * Decodes LZW image data into pixel array.
  757.  
  758.    * Adapted from John Cristy's ImageMagick.
  759.  
  760.    */
  761.  
  762.   protected void decodeImageData() {
  763.  
  764.     int NullCode = -1;
  765.  
  766.     int npix = iw * ih;
  767.  
  768.     int available,
  769.  
  770.       clear,
  771.  
  772.       code_mask,
  773.  
  774.       code_size,
  775.  
  776.       end_of_information,
  777.  
  778.       in_code,
  779.  
  780.       old_code,
  781.  
  782.       bits,
  783.  
  784.       code,
  785.  
  786.       count,
  787.  
  788.       i,
  789.  
  790.       datum,
  791.  
  792.       data_size,
  793.  
  794.       first,
  795.  
  796.       top,
  797.  
  798.       bi,
  799.  
  800.       pi;
  801.  
  802.  
  803.  
  804.     if ((pixels == null) || (pixels.length < npix)) {
  805.  
  806.       pixels = new byte[npix]; // allocate new pixel array
  807.  
  808.     }
  809.  
  810.     if (prefix == null) prefix = new short[MaxStackSize];
  811.  
  812.     if (suffix == null) suffix = new byte[MaxStackSize];
  813.  
  814.     if (pixelStack == null) pixelStack = new byte[MaxStackSize + 1];
  815.  
  816.  
  817.  
  818.     //  Initialize GIF data stream decoder.
  819.  
  820.  
  821.  
  822.     data_size = read();
  823.  
  824.     clear = 1 << data_size;
  825.  
  826.     end_of_information = clear + 1;
  827.  
  828.     available = clear + 2;
  829.  
  830.     old_code = NullCode;
  831.  
  832.     code_size = data_size + 1;
  833.  
  834.     code_mask = (1 << code_size) - 1;
  835.  
  836.     for (code = 0; code < clear; code++) {
  837.  
  838.       prefix[code] = 0;
  839.  
  840.       suffix[code] = (byte) code;
  841.  
  842.     }
  843.  
  844.  
  845.  
  846.     //  Decode GIF pixel stream.
  847.  
  848.  
  849.  
  850.     datum = bits = count = first = top = pi = bi = 0;
  851.  
  852.  
  853.  
  854.     for (i = 0; i < npix;) {
  855.  
  856.       if (top == 0) {
  857.  
  858.         if (bits < code_size) {
  859.  
  860.           //  Load bytes until there are enough bits for a code.
  861.  
  862.           if (count == 0) {
  863.  
  864.             // Read a new data block.
  865.  
  866.             count = readBlock();
  867.  
  868.             if (count <= 0)
  869.  
  870.               break;
  871.  
  872.             bi = 0;
  873.  
  874.           }
  875.  
  876.           datum += (((int) block[bi]) & 0xff) << bits;
  877.  
  878.           bits += 8;
  879.  
  880.           bi++;
  881.  
  882.           count--;
  883.  
  884.           continue;
  885.  
  886.         }
  887.  
  888.  
  889.  
  890.         //  Get the next code.
  891.  
  892.  
  893.  
  894.         code = datum & code_mask;
  895.  
  896.         datum >>= code_size;
  897.  
  898.         bits -= code_size;
  899.  
  900.  
  901.  
  902.         //  Interpret the code
  903.  
  904.  
  905.  
  906.         if ((code > available) || (code == end_of_information))
  907.  
  908.           break;
  909.  
  910.         if (code == clear) {
  911.  
  912.           //  Reset decoder.
  913.  
  914.           code_size = data_size + 1;
  915.  
  916.           code_mask = (1 << code_size) - 1;
  917.  
  918.           available = clear + 2;
  919.  
  920.           old_code = NullCode;
  921.  
  922.           continue;
  923.  
  924.         }
  925.  
  926.         if (old_code == NullCode) {
  927.  
  928.           pixelStack[top++] = suffix[code];
  929.  
  930.           old_code = code;
  931.  
  932.           first = code;
  933.  
  934.           continue;
  935.  
  936.         }
  937.  
  938.         in_code = code;
  939.  
  940.         if (code == available) {
  941.  
  942.           pixelStack[top++] = (byte) first;
  943.  
  944.           code = old_code;
  945.  
  946.         }
  947.  
  948.         while (code > clear) {
  949.  
  950.           pixelStack[top++] = suffix[code];
  951.  
  952.           code = prefix[code];
  953.  
  954.         }
  955.  
  956.         first = ((int) suffix[code]) & 0xff;
  957.  
  958.  
  959.  
  960.         //  Add a new string to the string table,
  961.  
  962.  
  963.  
  964.         if (available >= MaxStackSize)
  965.  
  966.           break;
  967.  
  968.         pixelStack[top++] = (byte) first;
  969.  
  970.         prefix[available] = (short) old_code;
  971.  
  972.         suffix[available] = (byte) first;
  973.  
  974.         available++;
  975.  
  976.         if (((available & code_mask) == 0)
  977.  
  978.           && (available < MaxStackSize)) {
  979.  
  980.           code_size++;
  981.  
  982.           code_mask += available;
  983.  
  984.         }
  985.  
  986.         old_code = in_code;
  987.  
  988.       }
  989.  
  990.  
  991.  
  992.       //  Pop a pixel off the pixel stack.
  993.  
  994.  
  995.  
  996.       top--;
  997.  
  998.       pixels[pi++] = pixelStack[top];
  999.  
  1000.       i++;
  1001.  
  1002.     }
  1003.  
  1004.  
  1005.  
  1006.     for (i = pi; i < npix; i++) {
  1007.  
  1008.       pixels[i] = 0; // clear missing pixels
  1009.  
  1010.     }
  1011.  
  1012.  
  1013.  
  1014.   }
  1015.  
  1016.  
  1017.  
  1018.   /**
  1019.  
  1020.    * Returns true if an error was encountered during reading/decoding
  1021.  
  1022.    */
  1023.  
  1024.   protected boolean err() {
  1025.  
  1026.     return status != STATUS_OK;
  1027.  
  1028.   }
  1029.  
  1030.  
  1031.  
  1032.   /**
  1033.  
  1034.    * Initializes or re-initializes reader
  1035.  
  1036.    */
  1037.  
  1038.   protected void init() {
  1039.  
  1040.     status = STATUS_OK;
  1041.  
  1042.     frameCount = 0;
  1043.  
  1044.     frames = new ArrayList();
  1045.  
  1046.     gct = null;
  1047.  
  1048.     lct = null;
  1049.  
  1050.   }
  1051.  
  1052.  
  1053.  
  1054.   /**
  1055.  
  1056.    * Reads a single byte from the input stream.
  1057.  
  1058.    */
  1059.  
  1060.   protected int read() {
  1061.  
  1062.     int curByte = 0;
  1063.  
  1064.     try {
  1065.  
  1066.       curByte = in.read();
  1067.  
  1068.     } catch (IOException e) {
  1069.  
  1070.       status = STATUS_FORMAT_ERROR;
  1071.  
  1072.     }
  1073.  
  1074.     return curByte;
  1075.  
  1076.   }
  1077.  
  1078.  
  1079.  
  1080.   /**
  1081.  
  1082.    * Reads next variable length block from input.
  1083.  
  1084.    *
  1085.  
  1086.    * @return number of bytes stored in "buffer"
  1087.  
  1088.    */
  1089.  
  1090.   protected int readBlock() {
  1091.  
  1092.     blockSize = read();
  1093.  
  1094.     int n = 0;
  1095.  
  1096.     if (blockSize > 0) {
  1097.  
  1098.       try {
  1099.  
  1100.         int count = 0;
  1101.  
  1102.         while (n < blockSize) {
  1103.  
  1104.           count = in.read(block, n, blockSize - n);
  1105.  
  1106.           if (count == -1)
  1107.  
  1108.             break;
  1109.  
  1110.           n += count;
  1111.  
  1112.         }
  1113.  
  1114.       } catch (IOException e) {
  1115.  
  1116.       }
  1117.  
  1118.  
  1119.  
  1120.       if (n < blockSize) {
  1121.  
  1122.         status = STATUS_FORMAT_ERROR;
  1123.  
  1124.       }
  1125.  
  1126.     }
  1127.  
  1128.     return n;
  1129.  
  1130.   }
  1131.  
  1132.  
  1133.  
  1134.   /**
  1135.  
  1136.    * Reads color table as 256 RGB integer values
  1137.  
  1138.    *
  1139.  
  1140.    * @param ncolors int number of colors to read
  1141.  
  1142.    * @return int array containing 256 colors (packed ARGB with full alpha)
  1143.  
  1144.    */
  1145.  
  1146.   protected int[] readColorTable(int ncolors) {
  1147.  
  1148.     int nbytes = 3 * ncolors;
  1149.  
  1150.     int[] tab = null;
  1151.  
  1152.     byte[] c = new byte[nbytes];
  1153.  
  1154.     int n = 0;
  1155.  
  1156.     try {
  1157.  
  1158.       n = in.read(c);
  1159.  
  1160.     } catch (IOException e) {
  1161.  
  1162.     }
  1163.  
  1164.     if (n < nbytes) {
  1165.  
  1166.       status = STATUS_FORMAT_ERROR;
  1167.  
  1168.     } else {
  1169.  
  1170.       tab = new int[256]; // max size to avoid bounds checks
  1171.  
  1172.       int i = 0;
  1173.  
  1174.       int j = 0;
  1175.  
  1176.       while (i < ncolors) {
  1177.  
  1178.         int r = ((int) c[j++]) & 0xff;
  1179.  
  1180.         int g = ((int) c[j++]) & 0xff;
  1181.  
  1182.         int b = ((int) c[j++]) & 0xff;
  1183.  
  1184.         tab[i++] = 0xff000000 | (r << 16) | (g << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" /> | b;
  1185.  
  1186.       }
  1187.  
  1188.     }
  1189.  
  1190.     return tab;
  1191.  
  1192.   }
  1193.  
  1194.  
  1195.  
  1196.   /**
  1197.  
  1198.    * Main file parser.  Reads GIF content blocks.
  1199.  
  1200.    */
  1201.  
  1202.   protected void readContents() {
  1203.  
  1204.     // read GIF file content blocks
  1205.  
  1206.     boolean done = false;
  1207.  
  1208.     while (!(done || err())) {
  1209.  
  1210.       int code = read();
  1211.  
  1212.       switch (code) {
  1213.  
  1214.  
  1215.  
  1216.         case 0x2C : // image separator
  1217.  
  1218.           readImage();
  1219.  
  1220.           break;
  1221.  
  1222.  
  1223.  
  1224.         case 0x21 : // extension
  1225.  
  1226.           code = read();
  1227.  
  1228.           switch (code) {
  1229.  
  1230.             case 0xf9 : // graphics control extension
  1231.  
  1232.               readGraphicControlExt();
  1233.  
  1234.               break;
  1235.  
  1236.  
  1237.  
  1238.             case 0xff : // application extension
  1239.  
  1240.               readBlock();
  1241.  
  1242.               String app = "";
  1243.  
  1244.               for (int i = 0; i < 11; i++) {
  1245.  
  1246.                 app += (char) block[i];
  1247.  
  1248.               }
  1249.  
  1250.               if (app.equals("NETSCAPE2.0")) {
  1251.  
  1252.                 readNetscapeExt();
  1253.  
  1254.               }
  1255.  
  1256.               else
  1257.  
  1258.                 skip(); // don't care
  1259.  
  1260.               break;
  1261.  
  1262.  
  1263.  
  1264.             default : // uninteresting extension
  1265.  
  1266.               skip();
  1267.  
  1268.           }
  1269.  
  1270.           break;
  1271.  
  1272.  
  1273.  
  1274.         case 0x3b : // terminator
  1275.  
  1276.           done = true;
  1277.  
  1278.           break;
  1279.  
  1280.  
  1281.  
  1282.         case 0x00 : // bad byte, but keep going and see what happens
  1283.  
  1284.           break;
  1285.  
  1286.  
  1287.  
  1288.         default :
  1289.  
  1290.           status = STATUS_FORMAT_ERROR;
  1291.  
  1292.       }
  1293.  
  1294.     }
  1295.  
  1296.   }
  1297.  
  1298.  
  1299.  
  1300.   /**
  1301.  
  1302.    * Reads Graphics Control Extension values
  1303.  
  1304.    */
  1305.  
  1306.   protected void readGraphicControlExt() {
  1307.  
  1308.     read(); // block size
  1309.  
  1310.     int packed = read(); // packed fields
  1311.  
  1312.     dispose = (packed & 0x1c) >> 2; // disposal method
  1313.  
  1314.     if (dispose == 0) {
  1315.  
  1316.       dispose = 1; // elect to keep old image if discretionary
  1317.  
  1318.     }
  1319.  
  1320.     transparency = (packed & 1) != 0;
  1321.  
  1322.     delay = readShort() * 10; // delay in milliseconds
  1323.  
  1324.     transIndex = read(); // transparent color index
  1325.  
  1326.     read(); // block terminator
  1327.  
  1328.   }
  1329.  
  1330.  
  1331.  
  1332.   /**
  1333.  
  1334.    * Reads GIF file header information.
  1335.  
  1336.    */
  1337.  
  1338.   protected void readHeader() {
  1339.  
  1340.     String id = "";
  1341.  
  1342.     for (int i = 0; i < 6; i++) {
  1343.  
  1344.       id += (char) read();
  1345.  
  1346.     }
  1347.  
  1348.     System.out.println("GIF >> "+id);
  1349.  
  1350.     if (!id.startsWith("GIF")) {
  1351.  
  1352.       status = STATUS_FORMAT_ERROR;
  1353.  
  1354.       return;
  1355.  
  1356.     }    
  1357.  
  1358.     readLSD();
  1359.  
  1360.     if (gctFlag && !err()) {
  1361.  
  1362.       gct = readColorTable(gctSize);
  1363.  
  1364.       bgColor = gct[bgIndex];
  1365.  
  1366.     }
  1367.  
  1368.   }
  1369.  
  1370.  
  1371.  
  1372.   /**
  1373.  
  1374.    * Reads next frame image
  1375.  
  1376.    */
  1377.  
  1378.   protected void readImage() {
  1379.  
  1380.     ix = readShort(); // (sub)image position & size
  1381.  
  1382.     iy = readShort();
  1383.  
  1384.     iw = readShort();
  1385.  
  1386.     ih = readShort();
  1387.  
  1388.  
  1389.  
  1390.     int packed = read();
  1391.  
  1392.     lctFlag = (packed & 0x80) != 0; // 1 - local color table flag
  1393.  
  1394.     interlace = (packed & 0x40) != 0; // 2 - interlace flag
  1395.  
  1396.     // 3 - sort flag
  1397.  
  1398.     // 4-5 - reserved
  1399.  
  1400.     lctSize = 2 << (packed & 7); // 6-8 - local color table size
  1401.  
  1402.  
  1403.  
  1404.     if (lctFlag) {
  1405.  
  1406.       lct = readColorTable(lctSize); // read table
  1407.  
  1408.       act = lct; // make local table active
  1409.  
  1410.     } else {
  1411.  
  1412.       act = gct; // make global table active
  1413.  
  1414.       if (bgIndex == transIndex)
  1415.  
  1416.         bgColor = 0;
  1417.  
  1418.     }
  1419.  
  1420.     int save = 0;
  1421.  
  1422.     if (transparency) {
  1423.  
  1424.       save = act[transIndex];
  1425.  
  1426.       act[transIndex] = 0; // set transparent color if specified
  1427.  
  1428.     }
  1429.  
  1430.  
  1431.  
  1432.     if (act == null) {
  1433.  
  1434.       status = STATUS_FORMAT_ERROR; // no color table defined
  1435.  
  1436.     }
  1437.  
  1438.  
  1439.  
  1440.     if (err()) return;
  1441.  
  1442.  
  1443.  
  1444.     decodeImageData(); // decode pixel data
  1445.  
  1446.     skip();
  1447.  
  1448.  
  1449.  
  1450.     if (err()) return;
  1451.  
  1452.  
  1453.  
  1454.     frameCount++;
  1455.  
  1456.  
  1457.  
  1458.     // create new image to receive frame data
  1459.  
  1460.     image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  1461.  
  1462.       // new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
  1463.  
  1464.  
  1465.  
  1466.     setPixels(); // transfer pixel data to image
  1467.  
  1468.  
  1469.  
  1470.     frames.add(new GifFrame(image, delay)); // add image to frame list
  1471.  
  1472.  
  1473.  
  1474.     if (transparency) {
  1475.  
  1476.       act[transIndex] = save;
  1477.  
  1478.     }
  1479.  
  1480.     resetFrame();
  1481.  
  1482.  
  1483.  
  1484.   }
  1485.  
  1486.  
  1487.  
  1488.   /**
  1489.  
  1490.    * Reads Logical Screen Descriptor
  1491.  
  1492.    */
  1493.  
  1494.   protected void readLSD() {
  1495.  
  1496.  
  1497.  
  1498.     // logical screen size
  1499.  
  1500.     width = readShort();
  1501.  
  1502.     height = readShort();
  1503.  
  1504.  
  1505.  
  1506.     // packed fields
  1507.  
  1508.     int packed = read();
  1509.  
  1510.     gctFlag = (packed & 0x80) != 0; // 1   : global color table flag
  1511.  
  1512.     // 2-4 : color resolution
  1513.  
  1514.     // 5   : gct sort flag
  1515.  
  1516.     gctSize = 2 << (packed & 7); // 6-8 : gct size
  1517.  
  1518.  
  1519.  
  1520.     bgIndex = read(); // background color index
  1521.  
  1522.     pixelAspect = read(); // pixel aspect ratio
  1523.  
  1524.   }
  1525.  
  1526.  
  1527.  
  1528.   /**
  1529.  
  1530.    * Reads Netscape extenstion to obtain iteration count
  1531.  
  1532.    */
  1533.  
  1534.   protected void readNetscapeExt() {
  1535.  
  1536.     do {
  1537.  
  1538.       readBlock();
  1539.  
  1540.       if (block[0] == 1) {
  1541.  
  1542.         // loop count sub-block
  1543.  
  1544.         int b1 = ((int) block[1]) & 0xff;
  1545.  
  1546.         int b2 = ((int) block[2]) & 0xff;
  1547.  
  1548.         loopCount = (b2 << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" /> | b1;
  1549.  
  1550.       }
  1551.  
  1552.     } while ((blockSize > 0) && !err());
  1553.  
  1554.   }
  1555.  
  1556.  
  1557.  
  1558.   /**
  1559.  
  1560.    * Reads next 16-bit value, LSB first
  1561.  
  1562.    */
  1563.  
  1564.   protected int readShort() {
  1565.  
  1566.     // read 16-bit value, LSB first
  1567.  
  1568.     return read() | (read() << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />;
  1569.  
  1570.   }
  1571.  
  1572.  
  1573.  
  1574.   /**
  1575.  
  1576.    * Resets frame state for reading next image.
  1577.  
  1578.    */
  1579.  
  1580.   protected void resetFrame() {
  1581.  
  1582.     lastDispose = dispose;
  1583.  
  1584.     lastRect = new Rect(ix, iy, (ix+iw), (iy+ih)); //(ix, iy, iw, ih);
  1585.  
  1586.     lastImage = image;
  1587.  
  1588.     lastBgColor = bgColor;
  1589.  
  1590.     dispose = 0;
  1591.  
  1592.     transparency = false;
  1593.  
  1594.     delay = 0;
  1595.  
  1596.     lct = null;
  1597.  
  1598.   }
  1599.  
  1600.  
  1601.  
  1602.   /**
  1603.  
  1604.    * Skips variable length blocks up to and including
  1605.  
  1606.    * next zero length block.
  1607.  
  1608.    */
  1609.  
  1610.   protected void skip() {
  1611.  
  1612.     do {
  1613.  
  1614.       readBlock();
  1615.  
  1616.     } while ((blockSize > 0) && !err());
  1617.  
  1618.   }
  1619.  
  1620. }
  1621.  
  1622.  
Parsed in 0.118 seconds, using GeSHi 1.0.8.4


and here is an example of class usage using separated thread when playing :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     [ ..... ]
  3.  
  4.     private boolean isPlayingGif = false; // to control GIF playing
  5.  
  6.     private GifDecoder GD = null;
  7.  
  8.     private ImageView tmpGIF = null;
  9.  
  10.     private Bitmap tmpBitmap = null;
  11.  
  12.     // Need handler for callbacks to the UI thread
  13.  
  14.     final Handler mHandler = new Handler();
  15.  
  16.     // Create runnable for posting
  17.  
  18.     final Runnable mUpdateResults = new Runnable() {
  19.  
  20.         public void run() {
  21.  
  22.             if (tmpGIF != null && tmpBitmap != null) tmpGIF.setImageBitmap(tmpBitmap);
  23.  
  24.         }
  25.  
  26.     };
  27.  
  28.     private void playGIF(ByteArrayInputStream stream) {
  29.  
  30.         GD = new GifDecoder();
  31.  
  32.         GD.read(stream);
  33.  
  34.         playForm = new FormView(_activity); // FormView : extends from ScrollView for some purpose ...
  35.  
  36.         tmpGIF = new ImageView(_activity);
  37.  
  38.         tmpGIF.setImageBitmap(GD.getImage());
  39.  
  40.         playForm.addView(tmpGIF);
  41.  
  42.         _activity.setTitle(title);
  43.  
  44.         _activity.setContentView(playForm);
  45.  
  46.         isPlayingGif = true;
  47.  
  48.         gifPlayingThread = new Thread(new Runnable() {
  49.  
  50.               public void run() {
  51.  
  52.                     int n = GD.getFrameCount();
  53.  
  54.                     int ntimes = GD.getLoopCount();
  55.  
  56.                     do {
  57.  
  58.                          for (int i = 0; i < n; i++) {
  59.  
  60.                                tmpBitmap = GD.getFrame(i);  
  61.  
  62.                                int t = GD.getDelay(i);
  63.  
  64.                                mHandler.post(mUpdateResults);
  65.  
  66.                                try {
  67.  
  68.                                     Thread.sleep(t);
  69.  
  70.                                } catch (InterruptedException ex) {
  71.  
  72.                                     System.out.println(ex.getMessage());
  73.  
  74.                                }
  75.  
  76.                          }
  77.  
  78.                     } while (isPlayingGif && (ntimes == 0));
  79.  
  80.               }
  81.  
  82.         });
  83.  
  84.         gifPlayingThread.start();
  85.  
  86.         [ ..... ]
  87.  
  88.     }
  89.  
  90.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


hope it can help :D ...
... beginner in android :D
- JavanDeveloper Member -
User avatar
144key
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 16, 2010 2:07 pm

Re: How to play GIF file in Android??

Postby aniket.kulkarni » Mon Dec 13, 2010 8:15 am

hello,
can we set .gif or .swf as a background?and interact with it ?
aniket.kulkarni
Freshman
Freshman
 
Posts: 9
Joined: Fri Nov 12, 2010 6:54 am

Re: How to play GIF file in Android??

Postby shyama » Fri May 27, 2011 11:59 am

its also possible to use animated gifs in android by extracting the frames and defining an android frame-by-frame animation for these frames.

for an introduction into this topic you can visit http://app-solut.com/2011/05/playing-an ... n-android/
shyama
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 18, 2011 2:19 pm

Re: How to play GIF file in Android??

Postby uKL » Mon Aug 22, 2011 8:22 am

@144key, is your code in a public domain license?
uKL
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Aug 22, 2011 8:20 am

Re: How to play GIF file in Android??

Postby xavieralexander » Sat Aug 27, 2011 7:36 am

I recompiled it. should work for you.
Attachments
Gifanimation.zip
(2.39 MiB) Downloaded 523 times
xavieralexander
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Aug 27, 2011 7:32 am

Re: How to play GIF file in Android??

Postby pushigh » Sat Oct 19, 2013 1:16 pm

hello sir,

when i imported above given projects i am getting following error

[2013-10-18 08:33:58 - GIFAnimation] Project has no project.properties file! Edit the project properties to set one.
pushigh
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Oct 19, 2013 12:39 pm

Top
Previous

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests