Android custom GIF View display GIF Animation

Source: Internet
Author: User

Android custom GIF View display GIF Animation

Gif animation is widely used in web development. Using gif, many animations do not need to be programmed. Currently, many apps have used gif animation, however, the android sdk does not provide the gif View for us, so we can only customize one View to implement the gif effect.

Although android does not provide us with a ready-made GIF view, it provides us with a Movie class. This class is the key class used to implement GIF view. it mainly has two most important methods: one is to set the currently displayed frame based on the animation playback time, and the other is to draw the currently displayed frame into the canvas. let's take a look:

Click pause to pause GIF animation.

The following describes how to implement it:

1. First, define the required attributes: the following code:

 
     
                              
  
 
The three attributes indicate the gif resource, whether to use stream encoding, and the default animation interval.

2. Customize the GifView code.

Package view; import java. io. byteArrayOutputStream; import java. io. inputStream; import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. movie; import android. util. attributeSet; import android. view. view; import com. example. drawbaledemo. r;/*** @ author rzq **/public class GifView extends View {private Movie mMovie;/*** animation start time/private Long mMovieStart; private boolean mPaused = false;/*** current frame animation time/private int mCurrentAnimationTime;/*** custom three attributes/private boolean decode_STREAM; private int src_ID; private int default_time; public GifView (Context context) {super (context); init (context, null);} public GifView (Context context Context, AttributeSet set) {super (context, set); init (context, set);} private void init (Context context, AttributeSet Set) {TypedArray a = commoncommon); src_ID = required Common_gif_src,-1); decode_STREAM = required Common_decode_stream, true); default_time = required common_default_animation time, 1000);. recycle (); setFocusable (true); java. io. inputStream is; is = context. getResources (). openRawResource (src_ID); if (Decode_STREAM) {mMovie = Movie. decodeStream (is); // create a Movier Drawing Object Based on the file stream} else {byte [] array = streamToBytes (is); mMovie = Movie. decodeByteArray (array, 0, array. length) ;}@ Overrideprotected void onDraw (Canvas canvas) {if (mMovie! = Null) {if (! MPaused) {// update the frame time updateAnimationTime (); drawMovieFrame (canvas); invalidate () ;}else {// when paused, the frame time is not updated, draw only the current frame drawMovieFrame (canvas) ;}} private void updateAnimationTime () {long now = android. OS. systemClock. uptimeMillis (); // if the first frame is set, record the start time if (mMovieStart = 0) {mMovieStart = now;} // retrieves the animation duration int dur = mMovie. duration (); if (dur = 0) {dur = default_time;} // calculate the number of mCurrentAnimationTime = (int) (now-mMovie Start) % dur);} private void drawMovieFrame (Canvas canvas) {// you can draw mMovie by setting the frame to be displayed. setTime (mCurrentAnimationTime); canvas. save (Canvas. MATRIX_SAVE_FLAG); mMovie. draw (canvas, 0, 0); canvas. restore ();}/*** set pause ** @ param paused */public void setPaused (boolean paused) {this. mPaused = paused; if (! Paused) {/*** update animation start time */mMovieStart = android. OS. systemClock. uptimeMillis ()-mCurrentAnimationTime;} invalidate ();}/*** determines whether the gif image has stopped ** @ return */public boolean isPaused () {return this. mPaused;}/*** rewrite this method to enable wrap_content */@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureSpec. getMode (widthMeasureSpec); int widthSize = MeasureSpec. getSize (widthMeasureSpec); int heightMode = MeasureSpec. getMode (heightMeasureSpec); int heightSize = MeasureSpec. getSize (heightMeasureSpec); int width; int height; if (widthMode = MeasureSpec. EXACTLY) {width = widthSize;} else {int desired = (int) (getPaddingLeft () + mMovie. width () + getPaddingRight (); width = Math. min (desired, widthSize);} if (heightMode = MeasureSpec. EXACTLY) {height = heightSize;} else {int desired = (int) (getPaddingTop () + mMovie. height () + getPaddingBottom (); height = Math. min (desired, heightSize);} setMeasuredDimension (width, height );} /*** convert the stream into a byte array ** @ param is * @ return */private static byte [] streamToBytes (InputStream is) {ByteArrayOutputStream OS = new ByteArrayOutputStream (1024 ); byte [] buffer = new byte [1024]; int len; try {while (len = is. read (buffer)> = 0) {OS. write (buffer, 0, len) ;}} catch (java. io. IOException e) {} return OS. toByteArray ();}}
3. Use GifView in the layout file.

     
      
  
 
Just like using a common View, you can pass in the required resources.


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.