在edittext上加入gif表情

来源:互联网 发布:推理100软件 编辑:程序博客网 时间:2024/04/28 21:32

在网上找的代码,先贴这里看看

package com.ginkgoavenue.util;import com.ginkgoavenue.R;public class FaceDate {/*private static String[] FaceNames = {"No",                      "OK",                      "爱你",                      "爱情",                      "爱心",                      "傲慢",                      "白眼",                      "抱拳",                      "鄙视",                      "闭嘴",                      "便便",                      "擦汗",                      "菜刀",                      "差劲",                      "龇牙大笑",                      "大兵",                      "大哭",                      "蛋糕",                      "刀",                      "得意",                      "凋谢",                      "发呆",                      "发抖",                      "发怒",                      "饭",                      "飞吻",                      "奋斗",                      "尴尬",                      "勾引",                      "鼓掌",                      "哈欠",                      "害羞",                      "憨笑",                      "汗",                      "坏笑",                      "挥手",                      "回头",                      "饥饿",                      "激动",                      "街舞",                      "惊恐",                      "惊讶",                      "咖啡",                      "磕头",                      "可爱",                      "可怜",                      "抠鼻",                      "骷髅",                      "酷",                      "快哭了",                      "困",                      "篮球",                      "冷汗",                      "礼物",                      "流泪",                      "玫瑰",                      "难过",                      "怄火",                      "啤酒",                      "瓢虫",                      "撇嘴",                      "乒乓",                      "强",                      "敲打",                      "亲亲",                      "溴大了",                      "拳头",                      "弱",                      "色",                      "闪电",                      "胜利",                      "示爱",                      "衰",                      "睡",                      "太阳",                      "调皮",                      "跳绳",                      "跳跳",                      "偷笑",                      "吐",                      "微笑",                      "委屈",                      "握手",                      "西瓜",                      "吓",                      "献吻",                      "心碎",                      "嘘",                      "疑问",                      "阴险",                      "拥抱",                      "右哼哼",                      "右太极",                      "月亮",                      "晕",                      "再见",                      "炸弹",                      "折磨",                      "咒骂",                      "猪头",                      "抓狂",                      "转圈",                      "足球",                      "左哼哼",                      "左太极"};private static Integer[] FaceIds = {R.drawable.face1,R.drawable.face2,R.drawable.face3,R.drawable.face4,R.drawable.face5,R.drawable.face6,R.drawable.face7,R.drawable.face8,R.drawable.face9,R.drawable.face10,R.drawable.face11,R.drawable.face12,R.drawable.face13,R.drawable.face14,R.drawable.face15,R.drawable.face16,R.drawable.face17,R.drawable.face18,R.drawable.face19,R.drawable.face20,R.drawable.face21,R.drawable.face22,R.drawable.face23,R.drawable.face24,R.drawable.face25,R.drawable.face26,R.drawable.face27,R.drawable.face28,R.drawable.face29,R.drawable.face30,R.drawable.face31,R.drawable.face32,R.drawable.face33,R.drawable.face34,R.drawable.face35,R.drawable.face36,R.drawable.face37,R.drawable.face38,R.drawable.face39,R.drawable.face40,R.drawable.face41,R.drawable.face42,R.drawable.face43,R.drawable.face44,R.drawable.face45,R.drawable.face46,R.drawable.face47,R.drawable.face48,R.drawable.face49,R.drawable.face50,R.drawable.face51,R.drawable.face52,R.drawable.face53,R.drawable.face54,R.drawable.face55,R.drawable.face56,R.drawable.face57,R.drawable.face58,R.drawable.face59,R.drawable.face60,R.drawable.face61,R.drawable.face62,R.drawable.face63,R.drawable.face64,R.drawable.face65,R.drawable.face66,R.drawable.face67,R.drawable.face68,R.drawable.face69,R.drawable.face70,R.drawable.face71,R.drawable.face72,R.drawable.face73,R.drawable.face74,R.drawable.face75,R.drawable.face76,R.drawable.face77,R.drawable.face78,R.drawable.face79,R.drawable.face80,R.drawable.face81,R.drawable.face82,R.drawable.face83,R.drawable.face84,R.drawable.face85,R.drawable.face86,R.drawable.face87,R.drawable.face88,R.drawable.face89,R.drawable.face90,R.drawable.face91,R.drawable.face92,R.drawable.face93,R.drawable.face94,R.drawable.face95,R.drawable.face96,R.drawable.face97,R.drawable.face98,R.drawable.face99,R.drawable.face100,R.drawable.face101,R.drawable.face102,R.drawable.face103,R.drawable.face104,R.drawable.face105,};*/private static String[] FaceNames = {"Bye",                                       "HI",                                       "Love",                                       "LoveU",                                       "NO",                                       "WC",                                       "YES",                                       "YoYo",                                       "啊啊",                                       "爱",                                       "拜",                                       "拜佛",                                       "扮可爱",                                       "扮酷",                                       "包",                                       "奔跑",                                       "鼻血",                                       "闭嘴",                                       "表白",                                       "冰冻",                                       "不",                                       "不管",                                       "不嘛",                                       "彩花",                                       "扯脸",                                       "膛目",                                       "抽烟",                                       "愁",                                       "吹泡泡",                                       "捶胸",                                       "搓背",                                       "打架",                                       "打拳",                                       "大吃",                                       "大吼",                                       "大哭",                                       "大盘跳水",                                       "带头造反",                                       "电话",                                       "笃笃",                                       "跺脚",                                       "放鞭炮",                                       "飞",                                       "奋斗",                                       "感冒",                                       "高歌",                                       "恭喜发财",                                       "鬼脸",                                       "害羞1",                                       "害羞2",                                       "汗",                                       "呵呵",                                       "黑线",                                       "嘿嘿",                                       "哼",                                       "回眸",                                       "娇羞",                                       "狡黠",                                       "囧",                                       "聚光",                                       "可爱",                                       "可怜1",                                       "可怜2",                                       "啃玉米",                                       "空",                                       "抠鼻",                                       "哭",                                       "哭泣",                                       "苦想",                                       "狂奔",                                       "困",                                       "喇叭",                                       "两眼汪汪",                                       "路过",                                       "麦霸",                                       "冒金星",                                       "美女",                                       "萌",                                       "蒙头",                                       "喵喵",                                       "摸脸",                                       "抹脖子",                                       "木",                                       "木鱼",                                       "难过",                                       "挠背",                                       "牛",                                       "牛股",                                       "努力",                                       "怒",                                       "怒火",                                       "怒气1",                                       "怒气2",                                       "哦也",                                       "拍屁屁",                                       "拍手",                                       "拍照",                                       "喷火",                                       "屁屁",                                       "骑摩托",                                       "乞求",                                       "潜水",                                       "枪毙",                                       "亲亲",                                       "溴",                                       "热",                                       "蠕动",                                       "上天",                                       "圣诞老人",                                       "石化",                                       "刷牙",                                       "耍酷",                                       "帅哥",                                       "思考",                                       "太极",                                       "叹气",                                       "跳绳",                                       "偷笑",                                       "吐",                                       "吐舌头",                                       "吐水",                                       "哇哦",                                       "晚安",                                       "望远",                                       "委屈",                                       "我不",                                       "无语",                                       "吓",                                       "想入非非",                                       "向前进",                                       "小看",                                       "小样",                                       "笑抽",                                       "休息",                                       "巡山",                                       "疑问",                                       "阴险",                                       "游戏",                                       "抓狂",                                       "转圈",                                       "装狗",                                       "撞墙",                                       "自虐",                                       "做操"};private static Integer[] FaceIds = {R.drawable.monkeyface1,R.drawable.monkeyface2,R.drawable.monkeyface3,R.drawable.monkeyface4,R.drawable.monkeyface5,R.drawable.monkeyface6,R.drawable.monkeyface7,R.drawable.monkeyface8,R.drawable.monkeyface9,R.drawable.monkeyface10,R.drawable.monkeyface11,R.drawable.monkeyface12,R.drawable.monkeyface13,R.drawable.monkeyface14,R.drawable.monkeyface15,R.drawable.monkeyface16,R.drawable.monkeyface17,R.drawable.monkeyface18,R.drawable.monkeyface19,R.drawable.monkeyface20,R.drawable.monkeyface21,R.drawable.monkeyface22,R.drawable.monkeyface23,R.drawable.monkeyface24,R.drawable.monkeyface25,R.drawable.monkeyface26,R.drawable.monkeyface27,R.drawable.monkeyface28,R.drawable.monkeyface29,R.drawable.monkeyface30,R.drawable.monkeyface31,R.drawable.monkeyface32,R.drawable.monkeyface33,R.drawable.monkeyface34,R.drawable.monkeyface35,R.drawable.monkeyface36,R.drawable.monkeyface37,R.drawable.monkeyface38,R.drawable.monkeyface39,R.drawable.monkeyface40,R.drawable.monkeyface41,R.drawable.monkeyface42,R.drawable.monkeyface43,R.drawable.monkeyface44,R.drawable.monkeyface45,R.drawable.monkeyface46,R.drawable.monkeyface47,R.drawable.monkeyface48,R.drawable.monkeyface49,R.drawable.monkeyface50,R.drawable.monkeyface51,R.drawable.monkeyface52,R.drawable.monkeyface53,R.drawable.monkeyface54,R.drawable.monkeyface55,R.drawable.monkeyface56,R.drawable.monkeyface57,R.drawable.monkeyface58,R.drawable.monkeyface59,R.drawable.monkeyface60,R.drawable.monkeyface61,R.drawable.monkeyface62,R.drawable.monkeyface63,R.drawable.monkeyface64,R.drawable.monkeyface65,R.drawable.monkeyface66,R.drawable.monkeyface67,R.drawable.monkeyface68,R.drawable.monkeyface69,R.drawable.monkeyface70,R.drawable.monkeyface71,R.drawable.monkeyface72,R.drawable.monkeyface73,R.drawable.monkeyface74,R.drawable.monkeyface75,R.drawable.monkeyface76,R.drawable.monkeyface77,R.drawable.monkeyface78,R.drawable.monkeyface79,R.drawable.monkeyface80,R.drawable.monkeyface81,R.drawable.monkeyface82,R.drawable.monkeyface83,R.drawable.monkeyface84,R.drawable.monkeyface85,R.drawable.monkeyface86,R.drawable.monkeyface87,R.drawable.monkeyface88,R.drawable.monkeyface89,R.drawable.monkeyface90,R.drawable.monkeyface91,R.drawable.monkeyface92,R.drawable.monkeyface93,R.drawable.monkeyface94,R.drawable.monkeyface95,R.drawable.monkeyface96,R.drawable.monkeyface97,R.drawable.monkeyface98,R.drawable.monkeyface99,R.drawable.monkeyface100,R.drawable.monkeyface101,R.drawable.monkeyface102,R.drawable.monkeyface103,R.drawable.monkeyface104,R.drawable.monkeyface105,R.drawable.monkeyface106,R.drawable.monkeyface107,R.drawable.monkeyface108,R.drawable.monkeyface109,R.drawable.monkeyface110,R.drawable.monkeyface111,R.drawable.monkeyface112,R.drawable.monkeyface113,R.drawable.monkeyface114,R.drawable.monkeyface115,R.drawable.monkeyface116,R.drawable.monkeyface118,R.drawable.monkeyface119,R.drawable.monkeyface120,R.drawable.monkeyface121,R.drawable.monkeyface122,R.drawable.monkeyface123,R.drawable.monkeyface124,R.drawable.monkeyface125,R.drawable.monkeyface126,R.drawable.monkeyface127,R.drawable.monkeyface128,R.drawable.monkeyface129,R.drawable.monkeyface130,R.drawable.monkeyface131,R.drawable.monkeyface132,R.drawable.monkeyface133,R.drawable.monkeyface134,R.drawable.monkeyface135,R.drawable.monkeyface136,R.drawable.monkeyface137,R.drawable.monkeyface138,R.drawable.monkeyface139,R.drawable.monkeyface140,R.drawable.monkeyface141,R.drawable.monkeyface142,R.drawable.monkeyface143,R.drawable.monkeyface144,R.drawable.monkeyface145,};public static String[] getFaceNames(){return FaceNames;}public static Integer[] getFaceIds(){return FaceIds;}}


package com.ginkgoavenue.util;import java.util.List;public class GifObj {private String gifID;private List<GifTextDrawable> drawable; public GifObj(String gifID,List<GifTextDrawable>  drawableList){this.gifID = gifID;this.drawable = drawableList;}public String getGifId(){return this.gifID;}public List<GifTextDrawable> getGifTextDrawableList(){return this.drawable;}}

package com.ginkgoavenue.util;import java.io.InputStream;import java.util.Vector;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;//Handler for read & extract Bitmap from *.gifpublic class gifOpenHelper {// to store *.gif data, Bitmap & delayclass GifFrame {// to access image & delay w/o interfacepublic Bitmap image;public int delay;public GifFrame(Bitmap im, int del) {image = im;delay = del;}}// to define some error typepublic static final int STATUS_OK = 0;public static final int STATUS_FORMAT_ERROR = 1;public static final int STATUS_OPEN_ERROR = 2;protected int status;protected InputStream in;protected int width; // full image widthprotected int height; // full image heightprotected boolean gctFlag; // global color table usedprotected int gctSize; // size of global color tableprotected int loopCount = 1; // iterations; 0 = repeat foreverprotected int[] gct; // global color tableprotected int[] lct; // local color tableprotected int[] act; // active color tableprotected int bgIndex; // background color indexprotected int bgColor; // background colorprotected int lastBgColor; // previous bg colorprotected int pixelAspect; // pixel aspect ratioprotected boolean lctFlag; // local color table flagprotected boolean interlace; // interlace flagprotected int lctSize; // local color table sizeprotected int ix, iy, iw, ih; // current image rectangleprotected int lrx, lry, lrw, lrh;protected Bitmap image; // current frameprotected Bitmap lastImage; // previous frameprotected int frameindex = 0;public int getFrameindex() {return frameindex;}public void setFrameindex(int frameindex) {this.frameindex = frameindex;if (frameindex > frames.size() - 1) {frameindex = 0;}}protected byte[] block = new byte[256]; // current data blockprotected int blockSize = 0; // block size// last graphic control extension infoprotected int dispose = 0;// 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prevprotected int lastDispose = 0;protected boolean transparency = false; // use transparent colorprotected int delay = 0; // delay in millisecondsprotected int transIndex; // transparent color indexprotected static final int MaxStackSize = 4096;// max decoder pixel stack size// LZW decoder working arraysprotected short[] prefix;protected byte[] suffix;protected byte[] pixelStack;protected byte[] pixels;protected Vector<GifFrame> frames; // frames read from current fileprotected int frameCount;// to get its Width / Heightpublic int getWidth() {return width;}public int getHeigh() {return height;}/** * Gets display duration for 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 = ((GifFrame) frames.elementAt(n)).delay;}return delay;}public int getFrameCount() {return frameCount;}public Bitmap getImage() {return getFrame(0);}public int getLoopCount() {return loopCount;}protected void setPixels() {int[] dest = new int[width * height];// fill in starting image contents based on last image's dispose codeif (lastDispose > 0) {if (lastDispose == 3) {// use image before lastint n = frameCount - 2;if (n > 0) {lastImage = getFrame(n - 1);} else {lastImage = null;}}if (lastImage != null) {lastImage.getPixels(dest, 0, width, 0, 0, width, height);// copy pixelsif (lastDispose == 2) {// fill last image rect area with background colorint c = 0;if (!transparency) {c = lastBgColor;}for (int i = 0; i < lrh; i++) {int n1 = (lry + i) * width + lrx;int n2 = n1 + lrw;for (int k = n1; k < n2; k++) {dest[k] = c;}}}}}// copy each source line to the appropriate place in the destinationint pass = 1;int inc = 8;int iline = 0;for (int i = 0; i < 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;}}line = iline;iline += inc;}line += iy;if (line < height) {int k = line * width;int dx = k + ix; // start of line in destint dlim = dx + iw; // end of dest lineif ((k + width) < dlim) {dlim = k + width; // past dest edge}int sx = i * iw; // start of line in sourcewhile (dx < dlim) {// map color and insert in destinationint 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);}public Bitmap getFrame(int n) {Bitmap im = null;if ((n >= 0) && (n < frameCount)) {im = ((GifFrame) frames.elementAt(n)).image;}return im;}public Bitmap nextBitmap() {frameindex++;if (frameindex > frames.size() - 1) {frameindex = 0;}return ((GifFrame) frames.elementAt(frameindex)).image;}public int nextDelay() {return ((GifFrame) frames.elementAt(frameindex)).delay;}// to read & parse all *.gif streampublic 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;}try {is.close();} catch (Exception e) {e.printStackTrace();}return status;}protected void decodeImageData() {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[MaxStackSize];}if (suffix == null) {suffix = new byte[MaxStackSize];}if (pixelStack == null) {pixelStack = new byte[MaxStackSize + 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;for (code = 0; code < clear; code++) {prefix[code] = 0;suffix[code] = (byte) code;}// Decode GIF pixel stream.datum = bits = count = first = top = pi = bi = 0;for (i = 0; i < npix;) {if (top == 0) {if (bits < code_size) {// Load bytes until there are enough bits for a code.if (count == 0) {// Read a new data block.count = readBlock();if (count <= 0) {break;}bi = 0;}datum += (((int) block[bi]) & 0xff) << bits;bits += 8;bi++;count--;continue;}// Get the next code.code = datum & code_mask;datum >>= code_size;bits -= code_size;// Interpret the codeif ((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 a new string to the string table,if (available >= MaxStackSize) {break;}pixelStack[top++] = (byte) first;prefix[available] = (short) old_code;suffix[available] = (byte) first;available++;if (((available & code_mask) == 0)&& (available < MaxStackSize)) {code_size++;code_mask += available;}old_code = in_code;}// Pop a pixel off the pixel stack.top--;pixels[pi++] = pixelStack[top];i++;}for (i = pi; i < npix; i++) {pixels[i] = 0; // clear missing pixels}}protected boolean err() {return status != STATUS_OK;}// to initia variablepublic void init() {status = STATUS_OK;frameCount = 0;frames = new Vector<GifFrame>();gct = null;lct = null;}protected int read() {int curByte = 0;try {curByte = in.read();} catch (Exception e) {status = STATUS_FORMAT_ERROR;}return curByte;}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;}// Global Color Tableprotected 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 to avoid bounds checksint i = 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;}// Image Descriptorprotected void readContents() {// read GIF file content blocksboolean done = false;while (!(done || err())) {int code = read();switch (code) {case 0x2C: // image separatorreadImage();break;case 0x21: // extensioncode = read();switch (code) {case 0xf9: // graphics control extensionreadGraphicControlExt();break;case 0xff: // application extensionreadBlock();String app = "";for (int i = 0; i < 11; i++) {app += (char) block[i];}if (app.equals("NETSCAPE2.0")) {readNetscapeExt();} else {skip(); // don't care}break;default: // uninteresting extensionskip();}break;case 0x3b: // terminatordone = true;break;case 0x00: // bad byte, but keep going and see what happensbreak;default:status = STATUS_FORMAT_ERROR;}}}protected void readGraphicControlExt() {read(); // block sizeint packed = read(); // packed fieldsdispose = (packed & 0x1c) >> 2; // disposal methodif (dispose == 0) {dispose = 1; // elect to keep old image if discretionary}transparency = (packed & 1) != 0;delay = readShort() * 10; // delay in millisecondstransIndex = read(); // transparent color indexread(); // block terminator}// to get Stream - Headprotected void readHeader() {String id = "";for (int i = 0; i < 6; i++) {id += (char) read();}if (!id.startsWith("GIF")) {status = STATUS_FORMAT_ERROR;return;}readLSD();if (gctFlag && !err()) {gct = readColorTable(gctSize);bgColor = gct[bgIndex];}}protected void readImage() {// offset of Xix = readShort(); // (sub)image position & size// offset of Yiy = readShort();// width of bitmapiw = readShort();// height of bitmapih = readShort();// Local Color Table Flagint packed = read();lctFlag = (packed & 0x80) != 0; // 1 - local color table flag// Interlace Flag, to array with interwoven if ENABLE, with order// otherwiseinterlace = (packed & 0x40) != 0; // 2 - interlace flag// 3 - sort flag// 4-5 - reservedlctSize = 2 << (packed & 7); // 6-8 - local color table sizeif (lctFlag) {lct = readColorTable(lctSize); // read tableact = lct; // make local table active} else {act = gct; // make global table activeif (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;}decodeImageData(); // decode pixel dataskip();if (err()) {return;}frameCount++;// create new image to receive frame dataimage = Bitmap.createBitmap(width, height, Config.ARGB_4444);// createImage(width, height);setPixels(); // transfer pixel data to imageframes.addElement(new GifFrame(image, delay)); // add image to frame// listif (transparency) {act[transIndex] = save;}resetFrame();}// Logical Screen Descriptorprotected void readLSD() {// logical screen sizewidth = readShort();height = readShort();// packed fieldsint packed = read();gctFlag = (packed & 0x80) != 0; // 1 : global color table flag// 2-4 : color resolution// 5 : gct sort flaggctSize = 2 << (packed & 7); // 6-8 : gct sizebgIndex = read(); // background color indexpixelAspect = read(); // pixel aspect ratio}protected void readNetscapeExt() {do {readBlock();if (block[0] == 1) {// loop count sub-blockint b1 = ((int) block[1]) & 0xff;int b2 = ((int) block[2]) & 0xff;loopCount = (b2 << 8) | b1;}} while ((blockSize > 0) && !err());}// read 8 bit dataprotected int readShort() {// read 16-bit value, LSB firstreturn read() | (read() << 8);}protected void resetFrame() {lastDispose = dispose;lrx = ix;lry = iy;lrw = iw;lrh = ih;lastImage = image;lastBgColor = bgColor;dispose = 0;transparency = false;delay = 0;lct = null;}/** * Skips variable length blocks up to and including next zero length block. */protected void skip() {do {readBlock();} while ((blockSize > 0) && !err());}}

package com.ginkgoavenue.util;import java.lang.ref.WeakReference;import android.annotation.TargetApi;import android.content.res.Resources;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.graphics.drawable.Drawable.Callback;import android.graphics.drawable.DrawableContainer.DrawableContainerState;import android.os.Build;import android.util.Log;import android.widget.TextView;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class GifTextDrawable extends AnimationDrawable {private TextView tv;// private WeakReference<Callback> mCallback = null; private int curFrame=-1; private boolean Stop=false; public GifTextDrawable(TextView textView){this.tv=textView;}@Overridepublic void invalidateDrawable(Drawable who) {// TODO Auto-generated method stubsuper.invalidateDrawable(who);//Log.v("hwLog","invalidateDrawable");}@Overridepublic boolean selectDrawable(int idx) {// TODO Auto-generated method stub//Log.v("hwLog","selectDrawable,idx="+idx);curFrame = idx;return super.selectDrawable(idx);}@Overridepublic void scheduleSelf(Runnable what, long when) {// TODO Auto-generated method stub//Log.v("hwLog","scheduleSelf,when="+when);//super.scheduleSelf(what, when);if(!Stop){tv.postInvalidate();tv.postDelayed(this, this.getDuration(curFrame));}//Log.v("hwLog","scheduleSelf,"+text);}@Overridepublic void stop() {// TODO Auto-generated method stubsuper.stop();Stop=true;}/*@Overridepublic Callback getCallback() {        Log.v("hwLog","getCallback,Callback="+super.getCallback());//return super.getCallback();        if(mCallback!=null)        {        //Log.v("hwLog", "mCallback.get()="+mCallback.get());          return mCallback.get();        }        return super.getCallback();}*/   }

package com.ginkgoavenue.util;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.BitmapDrawable;import android.os.Handler;import android.os.Message;import android.text.Spannable;import android.text.SpannableString;import android.text.style.ImageSpan;import android.util.Log;import android.widget.TextView;public class SetGifText {private Context context;//表情    private static Integer[] faceIds = FaceDate.getFaceIds();    private static String[] faceNames = FaceDate.getFaceNames();    private final int Face_Text_MaxLength = 7;    private static List<GifObj> gifList = new ArrayList<GifObj>();   //存储正文中表情符号的位置   class facePos   {int s;//表情文字起始int e;//表情文字结束点int i;//该表情文字表示的表情的序号public facePos(int s,int e,int i){this.s=s;this.e=e;this.i=i;}   }       public SetGifText(Context context)   {   this.context = context;      }    /**     *      * @param tv TextView     * @param content 待设置到TextView上的内容     * @param TextId 对该段内容的标识ID,作为GIFID的前缀将本段文本中的GIF与其他文本中的GIF区别开来。如果只有一个TextView可以设为0     */public void setSpannableText(final TextView tv,final String content,final int TextId)    {        //先显示文字    tv.setText(content);    //新线程计算是否需要替换表情,需要替换就替换        new Thread(new Runnable(){public void run() {//用以存储需替换的表情的位置List<facePos> faceList = new ArrayList<facePos>();//查找表情的位置   for(int i=0;i<content.length();i++)    {    if(content.charAt(i)=='[')    {    for(int k=i;k<i+Face_Text_MaxLength;k++)    {    if(content.charAt(k)==']')    {                        for(int j =0;j<faceNames.length;j++)                        {                        if(faceNames[j].equals(content.substring(i+1,k)))                        {                        //保存需替换的表情文字的位置                        facePos fp = new facePos(i,k,j);                        faceList.add(fp);                        }                        }                break;    }    }        }    }                //如果无表情    if(faceList.size()==0)    {    }    else    {       //如果有表情         //计算该gif的标识    final String gifID = TextId+"";            //是否该表情已有动画            int isExist = gifExist(gifID);           // Log.v("hwLog","gifID:"+gifID+"/nisExist="+isExist);            if(isExist!=-1)            {            Log.e("hwLog","终止:"+isExist);            //已有动画,终止旧动画             List<GifTextDrawable> mFaceList = gifList.get(isExist).getGifTextDrawableList();             for(int i=0;i<mFaceList.size();i++){mFaceList.get(i).stop();}            gifList.remove(isExist);            }                   SpannableString ss = new SpannableString(content);     final List<GifTextDrawable> mFaceList = new ArrayList<GifTextDrawable>();     for(int j = 0;j<faceList.size();j++)     {            //开始新动画     GifTextDrawable mFace = new GifTextDrawable(tv);     final com.ginkgoavenue.util.gifOpenHelper gHelper = new com.ginkgoavenue.util.gifOpenHelper();     gHelper.read(context.getResources().openRawResource(faceIds[faceList.get(j).i]));     BitmapDrawable bd = new BitmapDrawable(gHelper.getImage());     mFace.addFrame(bd, gHelper.getDelay(0));     for (int i = 1; i < gHelper.getFrameCount(); i++) {     mFace.addFrame(new BitmapDrawable(gHelper.nextBitmap()), gHelper     .getDelay(i));     }                                       float faceH = bd.getIntrinsicHeight();                    float faceW = bd.getIntrinsicWidth();                                        //视为为dp                    faceH = faceH;                    faceW = faceW;                                     //再转回px                    faceH = UnitTransformUtil.dip2px(context, faceH);                    faceW = UnitTransformUtil.dip2px(context, faceW);                                        /*                    //表情高度固定为文字高度                    float x=faceH/tv.getLineHeight();                    faceW = faceW / x;                    faceH = tv.getLineHeight();                    */     mFace.setBounds(0, 0, (int)faceW,(int)faceH);     mFace.setOneShot(false);                  ImageSpan span = new ImageSpan(mFace, ImageSpan.ALIGN_BOTTOM);               //替换一个表情             ss.setSpan(span,faceList.get(j).s,faceList.get(j).e+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);             mFaceList.add(mFace);     }     gifList.add(new GifObj(TextId+"",mFaceList));          final SpannableString ssForPost = ss;             //显示新的已经替换表情的text             tv.post(new Runnable(){public void run() {// TODO 自动生成的方法存根tv.setText(ssForPost);for(int i=0;i<mFaceList.size();i++){mFaceList.get(i).start();}}                          });                 }}                }).start();                                    }    /**     * 是否已存在该GIF     * @param gifID 计算得到的gifID     * @return 不存在返回-1,存在返回序号     */    private int gifExist(String gifID)    {    for(int i=0;i<gifList.size();i++)    {    if(gifList.get(i).getGifId().equals(gifID))    {    return i;    }    }return -1;        }}

回去好好研究研究,挺有趣的
0 0
原创粉丝点击