Android enables GIF and text blending

Source: Internet
Author: User

We are in the QQ chat when send expression, but these expressions are not static, more dynamic map, GIF, then how to display animated GIF in the Android client.

found a way to do it on GitHub, GitHub Address Https://github.com/TracyZhangLei/android-gif-demo

Because I am, so do not see the dynamic effect, you can download their own to see.

let's start by looking at the code for this open source project. The Open source project is mainly by customizing a adapter-------Chatadapter, A custom method is used in the SetText attribute of each chatadapter convertnormalstringtospannablestring

The return value of the Convertnormalstringtospannablestring method is spannablestring

  Let's start with a look at what spannablestring is.

TextView are typically used to display plain text, but sometimes you need to set some of the text in style and event settings. The Android system uses the Spannablestring class to process the specified text, meaning that we want to implement the text plus the dynamic expression by spannablestring this class.

Private spannablestring convertnormalstringtospannablestring (String message, final TextView TV) {spannablestring        Value = spannablestring.valueof (message);        Matcher Localmatcher = emotion_url.matcher (value);            while (Localmatcher.find ()) {String str2 = localmatcher.group (0);            int k = Localmatcher.start ();            int m = Localmatcher.end ();                if (M-k < 8) {int face = Fm.getfaceid (STR2); if ( -1!=face) {//wrapping with weakreference weakreference<animatedimagespan> localimagespanref = n EW weakreference<animatedimagespan> (New Animatedimagespan (New Animatedgifdrawable) (Cxt.getresources ().                        Openrawresource (face), new Animatedgifdrawable.updatelistener () {@Override                        public void Update () {//update the TextView tv.postinvalidate ();                    }                    }))); Value.Setspan (Localimagespanref.get (), K, M, spanned.span_inclusive_inclusive);    }}} return value; }

First convert our incoming message into the Spannablestring class and see if the incoming value conforms to the regular expression we wrote at the beginning Emotion_url

Private Pattern Emotion_url = Pattern.compile ("\\[(\\s+?) \\]");

We take group (0) if it fits.

Attached: Groupis for (), group (0) refers to the entire string, group (1) refers to the first parenthesis of the Thing, Group (2) refers to the second parenthesis in the thing.

The difference between the sub-expression and the starting and ending positions is less than 8, which is exactly what we want. Call the Getfaceid method in Facemanager

public int Getfaceid (String facestr) {        if (Mfacemap.containskey (FACESTR)) {            return mfacemap.get (FACESTR);        }        return-1;    }

Find the expression we store with a map

If the expression exists, use a weak reference (WeakReference) to handle the custom Animatedimagespan so that Animatedimagespan consumes less memory. Refresh the interface with Postinvalidate in Updatelistener. Finally, the Spannablestring Setspan method, three parameters are to be put in the span, start position, end position, flag flag.

  About flag:

Spanned.span_exclusive_exclusive, this is the flag that needs to be specified at Setspan, which is used to identify whether or not to apply this effect when entering new characters before and after text in the span range. Spanned.span_exclusive_exclusive (not included), Spanned.span_inclusive_exclusive (previously included, not included), Spanned.span_ Exclusive_inclusive (not included in front, included later), Spanned.span_inclusive_inclusive (included before and after).

Finally, the spannablestring is returned, which realizes the dynamic graphics and text blending.

about customizing The Animatedimagespan are as follows:

public class Animatedimagespan extends Dynamicdrawablespan {private drawable mdrawable;        Public Animatedimagespan (drawable D) {super ();        mdrawable = D;        Use handler for ' ticks ' to proceed to next frame final handler Mhandler = new Handler ();                 Mhandler.post (New Runnable () {public void Run () {((animatedgifdrawable) mdrawable). NextFrame (); Set next with a delay depending on the duration for this frame mhandler.postdelayed (thi            S, ((animatedgifdrawable) mdrawable). Getframeduration ());    }        });    } @Override Public drawable getdrawable () {return ((animatedgifdrawable) mdrawable). Getdrawable ();        } @Override public int getsize (paint paint, charsequence text, int start, int end, Paint.fontmetricsint FM) {        drawable d = getdrawable ();        Rect rect = D.getbounds ();             IF (FM! = NULL) {fm.ascent =-rect.bottom; Fm.descent = 0;            Fm.top = fm.ascent;        Fm.bottom = 0;    } return rect.right; } @Override public void draw (canvas canvas, charsequence text, int start, int end, float x, int top, int y, int bott        OM, paint paint) {drawable b = getdrawable ();        Canvas.save ();        int transy = Bottom-b.getbounds (). Bottom;        if (mverticalalignment = = align_baseline) {Transy-= Paint.getfontmetricsint (). Descent;        } canvas.translate (x, Transy);        B.draw (canvas);    Canvas.restore (); }}

If you have questions, welcome to join QQ Group: (452379712), with Jerry Education senior engineer Online interaction

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Android enables GIF and text blending

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.