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"