Decoding Gif images and playing code and components on the Android platform

Source: Internet
Author: User

Before playing a GIF on Android, I wrote an article about how to use a GIF file and play it back. You can learn about how to play GIF videos.

If you know something about it, you must know that the above practice is troublesome. You have to break down the image and write all kinds of troublesome XML files, which is a bit depressing, the direct consequence is that a GIF image with many actions may increase the size of the project, which is unacceptable to many friends. However, the advantage is that the running speed is faster than the decoding speed.

The following method requires decoding, but the speed cannot be guaranteed. It may not be handled during thread startup. The following picture is used as an example to decode and play the video:

First, write a help class for GIF Decoding. This class provides several methods for operating GIF images, such as initializing the image, switching the image, and obtaining the total number of GIF combinations, because there are too many codes, we will not list them here. The source code will be provided for your reference. You can find this help class in the source code.

Then, compile a component for displaying Gif, inherit from View and implement Runable. The Code is 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 (Context context ){

This (context, null );

}

// Construct-refer for xml

Public TypegifView (Context context, AttributeSet attrs ){

Super (context, attrs );

// Add attributes

TypedArray ta = context. obtainStyledAttributes (attrs,

R.styleable.gif View );

Int n = ta. getIndexCount ();

For (int I = 0; I <n; I ++ ){

Int attr = ta. getIndex (I );

Switch (attr ){

Case R.styleable.gif View_src:

Int id = ta.getResourceId(R.styleable.gif View_src, 0 );

SetSrc (id );

Break;

Case R.styleable.gif View_delay:

Int idelta = ta.getInteger(R.styleable.gif View_delay, 1 );

SetDelta (idelta );

Break;

Case R.styleable.gif View_stop:

Boolean sp = ta.getBoolean(R.styleable.gif View_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 ();

}

/**

* Set the number of images to be displayed through the next ticket

* @ Param id

*/

Public void setSrc (int id ){

GHelper = new gifOpenHelper ();

GHelper. read (TypegifView. this. getResources (). openRawResource (id ));

Bmp = gHelper. getImage (); // obtain the first image

}

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 basic functions are implemented. You can enable GIF playback through view. start () or view. stop () to stop GIF playback. The Code is 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.gif View1 );

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.