Android platform enables GIF decoding and playback of code and components

Source: Internet
Author: User
Tags stub thread

Android has an article about how to use a GIF file and play it before it plays GIF. Here's a way to learn about playing GIF.

If you know a friend in front of you knows it's a bit of a hassle, again to break down the picture, and write all kinds of trouble XML file, a bit depressed, but also bring the direct result is a more action of GIF pictures may be the size of the project will be larger, this point many friends can not accept. But it's also obvious that the speed of running is faster than you can decode it.

The following method is required to decode, but the speed of the words can not be guaranteed, may be a thread when not handled well, to the following picture for example to do decoding playback:

First, write a GIF decoding help class, this class provides several ways to manipulate GIF pictures, such as the initialization of the picture ah, switch pictures, get the entire number of GIF combinations and so on, because the code is too much, here is not listed, the following will provide the source code for everyone to refer to, you can find the source code within the Help class.

After that, write a component that can display the GIF, inherit from view and implement the Runable code as follows:

Package com.terry.gif;

Import Android.content.Context;

Import Android.content.res.TypedArray;

Import Android.graphics.Bitmap;

Import Android.graphics.Canvas;

Import Android.graphics.Paint;

Import Android.util.AttributeSet;

Import Android.view.View;

public class Typegifview extends View implements Runnable {

Gifopenhelper Ghelper;

Private Boolean isstop = true;

int Delta;

String title;

Bitmap bmp;

Construct-refer for Java

Public Typegifview {

This is (context, NULL);

}

Construct-refer FOR XML

Public Typegifview (context context, AttributeSet Attrs) {

Super (context, attrs);

Add properties

TypedArray ta = context.obtainstyledattributes (Attrs,

R.styleable.gifview);

int n = ta.getindexcount ();

for (int i = 0; i < n; i++) {

int attr = Ta.getindex (i);

Switch (attr) {

Case R.STYLEABLE.GIFVIEW_SRC:

int id = ta.getresourceid (r.styleable.gifview_src, 0);

SETSRC (ID);

Break

Case R.styleable.gifview_delay:

int idelta = Ta.getinteger (R.styleable.gifview_delay, 1);

Setdelta (Idelta);

Break

Case R.styleable.gifview_stop:

Boolean sp = Ta.getboolean (R.styleable.gifview_stop, false);

if (!SP) {

Setstop ();

}

Break

}

}

Ta.recycle ();

}

/**

* Set Stop

*

* @param stop

*/

public void Setstop () {

Isstop = false;

}

/**

* Set Startup

*/

public void Setstart () {

Isstop = true;

Thread Updatetimer = new Thread (this);

Updatetimer.start ();

}

/**

* By the next ticket to set the first few pictures show

* @param ID

*/

public void setsrc (int id) {

Ghelper = new Gifopenhelper ();

Ghelper.read (TypegifView.this.getResources (). Openrawresource (ID));

BMP = Ghelper.getimage ();//Get the first picture

}

public void Setdelta (int is) {

Delta = is;

}

To meaure its Width & Height

@Override

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {

Setmeasureddimension (Measurewidth (Widthmeasurespec),

Measureheight (Heightmeasurespec));

}

private int measurewidth (int measurespec) {

return Ghelper.getwidth ();

}

private int measureheight (int measurespec) {

return Ghelper.getheigh ();

}

protected void OnDraw (Canvas Canvas) {

TODO auto-generated Method Stub

Canvas.drawbitmap (BMP, 0, 0, New Paint ());

BMP = Ghelper.nextbitmap ();

}

public void Run () {

TODO auto-generated Method Stub

while (Isstop) {

try {

This.postinvalidate ();

Thread.Sleep (Ghelper.nextdelay ()/delta);

catch (Exception ex) {

}

}

}

}

After the basic functionality is implemented. You can turn on GIF playback via View.start (), or view.stop () to stop the GIF playback, the code reads as follows:

Setcontentview (R.layout.main);

Button btn = (button) Findviewbyid (R.ID.BUTTON01);

Button btn2 = (button) Findviewbyid (R.ID.BUTTON02);

Final Typegifview view = (Typegifview) Findviewbyid (R.ID.GIFVIEW1);

Btn.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

View.setstop ();

}

});

Btn2.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View v) {

View.setstart ();

}

});

Display effect:

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.