Playing GIF Animation 2 in android

Source: Internet
Author: User

In the previous article, the code is used to input the id of the GIF animation to be played through the constructor. This article is further transformed to allow GifView to set the src attribute in the layout file like ImageView. In GifView, we use reflection to dynamically obtain the image resource ID of src and then play it back. This makes it much more flexible to use. The Code is as follows:

GifView:

Package com.home.gif view; import java. io. inputStream; import java. lang. reflect. field; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. movie; import android. OS. systemClock; import android. util. attributeSet; import android. util. typedValue; import android. widget. ima GeView; public class GifView extends ImageView {private Movie movie; private long movieStart; private int imageWidth; private int imageHeight; public GifView (Context context) {super (context );} public GifView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public GifView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); TypedArray a = context. obtainStyledA Ttributes (attrs, R. styleable. GifView); int resourceId = getResourceId (a, context, attrs); if (resourceId! = 0) {InputStream is = getResources (). openRawResource (resourceId); movie = Movie. decodeStream (is); if (movie! = Null) {Bitmap bitmap = BitmapFactory. decodeStream (is); imageWidth = bitmap. getWidth (); imageHeight = bitmap. getHeight (); bitmap. recycle () ;}}@ Overrideprotected void onDraw (Canvas canvas) {if (movie = null) {// a normal image directly calls the onDraw () method super of the parent class. onDraw (canvas) ;}else {playGif (canvas); invalidate () ;}@overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureS Pec, heightMeasureSpec); if (movie! = Null) {// if the image is a GIF image, set the size of the GIF view to setMeasuredDimension (imageWidth, imageHeight );}} /*** play GIF animation ** @ param canvas * @ return */private boolean playGif (Canvas canvas) {long now = SystemClock. uptimeMillis (); if (movieStart = 0) {movieStart = now;} int duration = movie. duration (); if (duration = 0) {duration = 1000;} int relTime = (int) (now-movieStart) % duration); movie. setTime (relTime); movie. draw (can Vas, 0, 0); if (now-movieStart)> = duration) {movieStart = 0; return true;} return false;}/*** use reflection, obtains the id of the image resource specified by src. ** @ Param a * @ param context * @ param attrs * @ return */private int getResourceId (TypedArray a, Context context, AttributeSet attrs) {try {Field field = TypedArray. class. getDeclaredField ("mValue"); field. setAccessible (true); TypedValue typedValueObject = (TypedValue) field. get (a); return typedValueObject. resourceId;} catch (Exception e) {e. printStackTrace ();} finally {if (! = Null) {a. recycle () ;}} return 0 ;}}

Attrs. xml:

 
     
  
 

Main. xml:

     
  
 

Disable hardware acceleration:

android:hardwareAccelerated="false"





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.