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, many other is dynamic map, GIF, then how to display animated GIF in Androidclient.

find such a method on GitHub. GitHub Address Https://github.com/TracyZhangLei/android-gif-demo

Because I am, I can't see the dynamic effect. You can download it yourself and see it.

let's start by looking at the code for this open source project. The Open source project is primarily defined by itself as a adapter-------Chatadapter, The SetText attribute for each of the Chatadapter uses its own defined method convertnormalstringtospannablestring

The return value of the Convertnormalstringtospannablestring method is spannablestring

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

TextView-Pass is often used to display normal text, but sometimes it is necessary to set some of the text in style and event settings. The Android system uses the spannablestring class to handle the specified text. That is to say, we want to achieve the implementation of text plus dynamic expression by spannablestring this class to achieve.

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, we convert our incoming message into the Spannablestring class, and then we see if the incoming value conforms to the normal form we wrote at the beginning Emotion_url

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

)\\]");

We take group (0) If we are to conform.

Attached:group is for (), group (0) refers to the entire string. Group (1) refers to something in the first parenthesis, and group (2) refers to something in the second parenthesis.

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

Assuming that the expression exists, use a weak reference (WeakReference) to process your own defined 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, the starting 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 before and after), 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.

  The animatedimagespan about itself defined 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 (); }}

Suppose you have questions. Welcome to Increase QQ Group: (452379712). Interact online with Jerry Education advanced Project

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article belongs to Yantai Jerry Education Technology Co., Ltd. and CSDN co-owned, welcome reprint, but without the author's permission 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.