android - How to display .gif file in imageview? -
my layout have lots of imageviews. want show .gif animated file in 1 imageview. tried yash method (adding gif image in imageview in android) .gif file shown layout. other views dissappeared.
i want show all.
any idea?
put .gif file res/raw folder, in fragment or activity , can set gif in imageview like
gifanimationdrawable gif; try { gif = new gifanimationdrawable(getresources().openrawresource(r.raw.download)); gif.setoneshot(false); } catch (notfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } ivgif.setimagedrawable(gif); gif.setvisible(true, true); }
1)gifanimationdrawable .java
public class gifanimationdrawable extends animationdrawable { private boolean decoded; private gifdecoder mgifdecoder; private bitmap mtmpbitmap; private int height, width; public gifanimationdrawable(file f) throws ioexception { this(f, false); } public gifanimationdrawable(inputstream is) throws ioexception { this(is, false); } public gifanimationdrawable(file f, boolean inline) throws ioexception { this(new bufferedinputstream(new fileinputstream(f), 32768), inline); } public gifanimationdrawable(inputstream is, boolean inline) throws ioexception { super(); inputstream bis = is; if(!bufferedinputstream.class.isinstance(bis)) bis = new bufferedinputstream(is, 32768); decoded = false; mgifdecoder = new gifdecoder(); mgifdecoder.read(bis); mtmpbitmap = mgifdecoder.getframe(0); android.util.log.v("gifanimationdrawable", "===>lead frame: ["+width+"x"+height+"; "+mgifdecoder.getdelay(0)+";"+mgifdecoder.getloopcount()+"]"); height = mtmpbitmap.getheight(); width = mtmpbitmap.getwidth(); addframe(new bitmapdrawable(mtmpbitmap), mgifdecoder.getdelay(0)); setoneshot(mgifdecoder.getloopcount() != 0); setvisible(true, true); if(inline){ loader.run(); }else{ new thread(loader).start(); } } public boolean isdecoded(){ return decoded; } private runnable loader = new runnable(){ public void run() { mgifdecoder.complete(); int i, n = mgifdecoder.getframecount(), t; for(i=1;i<n;i++){ mtmpbitmap = mgifdecoder.getframe(i); t = mgifdecoder.getdelay(i); android.util.log.v("gifanimationdrawable", "===>frame "+i+": "+t+"]"); addframe(new bitmapdrawable(mtmpbitmap), t); } decoded = true; mgifdecoder = null; } }; public int getminimumheight(){ return height; } public int getminimumwidth(){ return width; } public int getintrinsicheight(){ return height; } public int getintrinsicwidth(){ return width; } }
2) gifdecoder.java
public class gifdecoder { public static final int status_ok = 0; public static final int status_format_error = 1; public static final int status_open_error = 2; protected static final int max_stack_size = 4096; public static final int min_delay = 100; public static final int min_delay_enforce_threshold = 20; protected inputstream in; protected int status; protected int width; // full image width protected int height; // full image height protected boolean gctflag; // global color table used protected int gctsize; // size of global color table protected int loopcount = 1; // iterations; 0 = repeat forever protected int[] gct; // global color table protected int[] lct; // local color table protected int[] act; // active color table protected int bgindex; // background color index protected int bgcolor; // background color protected int lastbgcolor; // previous bg color protected int pixelaspect; // pixel aspect ratio protected boolean lctflag; // local color table flag protected boolean interlace; // interlace flag protected int lctsize; // local color table size protected int ix, iy, iw, ih; // current image rectangle protected int lrx, lry, lrw, lrh; protected bitmap image; // current frame protected bitmap lastbitmap; // previous frame protected byte[] block = new byte[256]; // current data block protected int blocksize = 0; // block size last graphic control extension info protected int dispose = 0; // 0=no action; 1=leave in place; 2=restore bg; 3=restore prev protected int lastdispose = 0; protected boolean transparency = false; // use transparent color protected int delay = 0; // delay in milliseconds protected int transindex; // transparent color index // lzw decoder working arrays protected short[] prefix; protected byte[] suffix; protected byte[] pixelstack; protected byte[] pixels; protected vector<gifframe> frames; // frames read current file protected int framecount; private boolean readcomplete; public gifdecoder() { readcomplete = false; } private static class gifframe { public gifframe(bitmap im, int del) { image = im; delay = del; } public bitmap image; public int delay; } /** * gets display duration specified frame. * * @param n * int index of frame * @return delay in milliseconds */ public int getdelay(int n) { delay = -1; if ((n >= 0) && (n < framecount)) { delay = frames.elementat(n).delay; //meets browser compatibility standards if (delay < min_delay_enforce_threshold) delay = min_delay; } return delay; } /** * gets number of frames read file. * * @return frame count */ public int getframecount() { return framecount; } /** * gets first (or only) image read. * * @return bufferedbitmap containing first frame, or null if none. */ public bitmap getbitmap() { return getframe(0); } /** * gets "netscape" iteration count, if any. count of 0 means repeat indefinitiely. * * @return iteration count if 1 specified, else 1. */ public int getloopcount() { return loopcount; } /** * creates new frame image current data (and previous frames specified disposition codes). */ protected void setpixels() { // expose destination image's pixels int array int[] dest = new int[width * height]; // fill in starting image contents based on last image's dispose code if (lastdispose > 0) { if (lastdispose == 3) { // use image before last int n = framecount - 2; if (n > 0) { lastbitmap = getframe(n - 1); } else { lastbitmap = null; } } if (lastbitmap != null) { lastbitmap.getpixels(dest, 0, width, 0, 0, width, height); // copy pixels if (lastdispose == 2) { // fill last image rect area background color int c = 0; if (!transparency) { c = lastbgcolor; } (int = 0; < lrh; i++) { int n1 = (lry + i) * width + lrx; int n2 = n1 + lrw; (int k = n1; k < n2; k++) { dest[k] = c; } } } } } // copy each source line appropriate place in destination int pass = 1; int inc = 8; int iline = 0; (int = 0; < ih; i++) { int line = i; if (interlace) { if (iline >= ih) { pass++; switch (pass) { case 2: iline = 4; break; case 3: iline = 2; inc = 4; break; case 4: iline = 1; inc = 2; break; default: break; } } line = iline; iline += inc; } line += iy; if (line < height) { int k = line * width; int dx = k + ix; // start of line in dest int dlim = dx + iw; // end of dest line if ((k + width) < dlim) { dlim = k + width; // past dest edge } int sx = * iw; // start of line in source while (dx < dlim) { // map color , insert in destination int index = ((int) pixels[sx++]) & 0xff; int c = act[index]; if (c != 0) { dest[dx] = c; } dx++; } } } image = bitmap.createbitmap(dest, width, height, config.argb_4444); } /** * gets image contents of frame n. * * @return bufferedbitmap representation of frame, or null if n invalid. */ public bitmap getframe(int n) { if (framecount <= 0) return null; n = n % framecount; return ((gifframe) frames.elementat(n)).image; } /** * reads gif image stream * * @param * containing gif file. * @return read status code (0 = no errors) */ public int read(inputstream is) { init(); if (is != null) { in = is; readheader(); if (!err()) { readcontents(); if (framecount < 0) { status = status_format_error; } } } else { status = status_open_error; } readcomplete = true; return status; } public void complete() { readcontents(); try { in.close(); } catch (exception e) { } } /** * decodes lzw image data pixel array. adapted john cristy's bitmapmagick. */ protected void decodebitmapdata() { int nullcode = -1; int npix = iw * ih; int available, clear, code_mask, code_size, end_of_information, in_code, old_code, bits, code, count, i, datum, data_size, first, top, bi, pi; if ((pixels == null) || (pixels.length < npix)) { pixels = new byte[npix]; // allocate new pixel array } if (prefix == null) { prefix = new short[max_stack_size]; } if (suffix == null) { suffix = new byte[max_stack_size]; } if (pixelstack == null) { pixelstack = new byte[max_stack_size + 1]; } // initialize gif data stream decoder. data_size = read(); clear = 1 << data_size; end_of_information = clear + 1; available = clear + 2; old_code = nullcode; code_size = data_size + 1; code_mask = (1 << code_size) - 1; (code = 0; code < clear; code++) { prefix[code] = 0; // xxx arrayindexoutofboundsexception suffix[code] = (byte) code; } // decode gif pixel stream. datum = bits = count = first = top = pi = bi = 0; (i = 0; < npix;) { if (top == 0) { if (bits < code_size) { // load bytes until there enough bits code. if (count == 0) { // read new data block. count = readblock(); if (count <= 0) { break; } bi = 0; } datum += (((int) block[bi]) & 0xff) << bits; bits += 8; bi++; count--; continue; } // next code. code = datum & code_mask; datum >>= code_size; bits -= code_size; // interpret code if ((code > available) || (code == end_of_information)) { break; } if (code == clear) { // reset decoder. code_size = data_size + 1; code_mask = (1 << code_size) - 1; available = clear + 2; old_code = nullcode; continue; } if (old_code == nullcode) { pixelstack[top++] = suffix[code]; old_code = code; first = code; continue; } in_code = code; if (code == available) { pixelstack[top++] = (byte) first; code = old_code; } while (code > clear) { pixelstack[top++] = suffix[code]; code = prefix[code]; } first = ((int) suffix[code]) & 0xff; // add new string string table, if (available >= max_stack_size) { break; } pixelstack[top++] = (byte) first; prefix[available] = (short) old_code; suffix[available] = (byte) first; available++; if (((available & code_mask) == 0) && (available < max_stack_size)) { code_size++; code_mask += available; } old_code = in_code; } // pop pixel off pixel stack. top--; pixels[pi++] = pixelstack[top]; i++; } (i = pi; < npix; i++) { pixels[i] = 0; // clear missing pixels } } /** * returns true if error encountered during reading/decoding */ protected boolean err() { return status != status_ok; } /** * initializes or re-initializes reader */ protected void init() { status = status_ok; framecount = 0; frames = new vector<gifframe>(); gct = null; lct = null; } /** * reads single byte input stream. */ protected int read() { int curbyte = 0; try { curbyte = in.read(); } catch (exception e) { status = status_format_error; } return curbyte; } /** * reads next variable length block input. * * @return number of bytes stored in "buffer" */ protected int readblock() { blocksize = read(); int n = 0; if (blocksize > 0) { try { int count = 0; while (n < blocksize) { count = in.read(block, n, blocksize - n); if (count == -1) { break; } n += count; } } catch (exception e) { e.printstacktrace(); } if (n < blocksize) { status = status_format_error; } } return n; } /** * reads color table 256 rgb integer values * * @param ncolors * int number of colors read * @return int array containing 256 colors (packed argb full alpha) */ protected int[] readcolortable(int ncolors) { int nbytes = 3 * ncolors; int[] tab = null; byte[] c = new byte[nbytes]; int n = 0; try { n = in.read(c); } catch (exception e) { e.printstacktrace(); } if (n < nbytes) { status = status_format_error; } else { tab = new int[256]; // max size avoid bounds checks int = 0; int j = 0; while (i < ncolors) { int r = ((int) c[j++]) & 0xff; int g = ((int) c[j++]) & 0xff; int b = ((int) c[j++]) & 0xff; tab[i++] = 0xff000000 | (r << 16) | (g << 8) | b; } } return tab; } /** * main file parser. reads gif content blocks. */ protected void readcontents() { // read gif file content blocks boolean done = false; while (!(done || err())) { int code = read(); switch (code) { case 0x2c: // image separator readbitmap(); if(!readcomplete) return; break; case 0x21: // extension code = read(); switch (code) { case 0xf9: // graphics control extension readgraphiccontrolext(); break; case 0xff: // application extension readblock(); string app = ""; (int = 0; < 11; i++) { app += (char) block[i]; } if (app.equals("netscape2.0")) { readnetscapeext(); } else { skip(); // don't care } break; case 0xfe:// comment extension skip(); break; case 0x01:// plain text extension skip(); break; default: // uninteresting extension skip(); } break; case 0x3b: // terminator done = true; break; case 0x00: // bad byte, keep going , see happens break; default: status = status_format_error; } } } /** * reads graphics control extension values */ protected void readgraphiccontrolext() { read(); // block size int packed = read(); // packed fields dispose = (packed & 0x1c) >> 2; // disposal method if (dispose == 0) { dispose = 1; // elect keep old image if discretionary } transparency = (packed & 1) != 0; delay = readshort() * 10; // delay in milliseconds transindex = read(); // transparent color index read(); // block terminator } /** * reads gif file header information. */ protected void readheader() { string id = ""; (int = 0; < 6; i++) { id += (char) read(); } if (!id.startswith("gif")) { status = status_format_error; return; } readlsd(); if (gctflag && !err()) { gct = readcolortable(gctsize); bgcolor = gct[bgindex]; } } /** * reads next frame image */ protected void readbitmap() { ix = readshort(); // (sub)image position & size iy = readshort(); iw = readshort(); ih = readshort(); int packed = read(); lctflag = (packed & 0x80) != 0; // 1 - local color table flag interlace lctsize = (int) math.pow(2, (packed & 0x07) + 1); // 3 - sort flag // 4-5 - reserved lctsize = 2 << (packed & 7); // 6-8 - local color // table size interlace = (packed & 0x40) != 0; if (lctflag) { lct = readcolortable(lctsize); // read table act = lct; // make local table active } else { act = gct; // make global table active if (bgindex == transindex) { bgcolor = 0; } } int save = 0; if (transparency) { save = act[transindex]; act[transindex] = 0; // set transparent color if specified } if (act == null) { status = status_format_error; // no color table defined } if (err()) { return; } decodebitmapdata(); // decode pixel data skip(); if (err()) { return; } framecount++; // create new image receive frame data image = bitmap.createbitmap(width, height, config.argb_4444); setpixels(); // transfer pixel data image frames.addelement(new gifframe(image, delay)); // add image frame // list if (transparency) { act[transindex] = save; } resetframe(); } /** * reads logical screen descriptor */ protected void readlsd() { // logical screen size width = readshort(); height = readshort(); // packed fields int packed = read(); gctflag = (packed & 0x80) != 0; // 1 : global color table flag // 2-4 : color resolution // 5 : gct sort flag gctsize = 2 << (packed & 7); // 6-8 : gct size bgindex = read(); // background color index pixelaspect = read(); // pixel aspect ratio } /** * reads netscape extenstion obtain iteration count */ protected void readnetscapeext() { { readblock(); if (block[0] == 1) { // loop count sub-block int b1 = ((int) block[1]) & 0xff; int b2 = ((int) block[2]) & 0xff; loopcount = (b2 << 8) | b1; } } while ((blocksize > 0) && !err()); } /** * reads next 16-bit value, lsb first */ protected int readshort() { // read 16-bit value, lsb first return read() | (read() << 8); } /** * resets frame state reading next image. */ protected void resetframe() { lastdispose = dispose; lrx = ix; lry = iy; lrw = iw; lrh = ih; lastbitmap = image; lastbgcolor = bgcolor; dispose = 0; transparency = false; delay = 0; lct = null; } /** * skips variable length blocks , including next 0 length block. */ protected void skip() { { readblock(); } while ((blocksize > 0) && !err()); } }
you can download class https://github.com/hipmob/gifanimateddrawable/blob/master/src/com/hipmob/gifanimationdrawable/gifanimationdrawable.java 404 not found https://gist.github.com/devunwired/4479231 404 not found
if above link not open can find class below link
hope save someones time lot because admire https://stackoverflow.com/a/11736861/3514144 want show in imageview
as if link unavailable can find class github too.
Comments
Post a Comment