How does an Android loaded GIF animation work? I believe we are all very curious, this article will be announced for you, the contents are as follows
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<com.example.gifdemo.gifview
android:id= "@+id/gif1"
android:layout_ Width= "100DP"
android:layout_height= "100DP"
android:layout_gravity= "Center_horizontal"
android: Enabled= "false"/>
</LinearLayout>
<declare-styleable name= "Gifview" >
<attr name= "gif" format= "reference"/> <attr "name="
Paused "format=" boolean "/>
</declare-styleable>
Main interface
Package Com.example.gifdemo;
Import android.app.Activity;
Import Android.os.Bundle;
public class Mainactivity extends activity {
private gifview gif1;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
GIF1 = (Gifview) Findviewbyid (R.ID.GIF1);
Set background gif picture resource
gif1.setmovieresource (r.raw.red);
}
customizing view
Package Com.example.gifdemo;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Movie;
Import Android.os.Build;
Import Android.util.AttributeSet;
Import Android.view.View;
public class Gifview extends View {/** * defaults to 1 seconds * * * private static final int default_movie_duration = 1000;
private int Mmovieresourceid;
Private Movie Mmovie;
Private long Mmoviestart;
private int mcurrentanimationtime = 0;
private float Mleft;
private float mtop;
private float Mscale;
private int mmeasuredmoviewidth;
private int mmeasuredmovieheight;
Private Boolean mvisible = true;
Private volatile Boolean mpaused = false;
/** * Constructor/public Gifview {This (context, NULL);
Public Gifview (context, AttributeSet attrs) {This (context, attrs,0); } public Gifview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Setviewattributes (context, attrs, Defstyle);
SetBackgroundColor (Color.parsecolor ("#FFB6C1")); @SuppressLint ("Newapi") private void setviewattributes (context context, AttributeSet attrs, int defstyle) {if (Build.VERSION.SDK_INT >= build.version_codes.)
Honeycomb) {setlayertype (view.layer_type_software, NULL);
//Read the GIF value from the description file to create a movie instance final TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.gifview);
Mmovieresourceid = Array.getresourceid (R.styleable.gifview_gif,-1);
mpaused = Array.getboolean (r.styleable.gifview_paused, false);
Array.recycle ();
if (Mmovieresourceid!=-1) {Mmovie = Movie.decodestream (Getresources (). Openrawresource (Mmovieresourceid));
}/** * Set GIF map resource */public void Setmovieresource (int movieresid) {This.mmovieresourceid = Movieresid; Mmovie = Movie.decodestream (Getresources (). openrawreSource (Mmovieresourceid));
Requestlayout (); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {if (Mmovie!= null) {//gif move
The width of the painting, the height int moviewidth = mmovie.width ();
int movieheight = Mmovie.height ();
The width of the control int maximumwidth = Measurespec.getsize (Widthmeasurespec);
GIF picture width/control wide float Scalew = (float) moviewidth/(float) maximumwidth;
Mscale = 1f/scalew;
Mmeasuredmoviewidth = MaximumWidth;
Mmeasuredmovieheight = (int) (movieheight * mscale);
Setmeasureddimension (Mmeasuredmoviewidth, mmeasuredmovieheight);
else {setmeasureddimension (Getsuggestedminimumwidth (), Getsuggestedminimumheight ()); }//@Override//protected void OnLayout (Boolean changed, int l, int t, int r, int b) {//Super.onlayout (change
D, L, T, R, b);
Mleft = (getwidth ()-mmeasuredmoviewidth)/2f;
Mtop = (getheight ()-mmeasuredmovieheight)/2f;
mvisible = getvisibility () = = view.visible; } @Override ProtecTed void OnDraw (Canvas Canvas) {if (Mmovie!= null) {if (!mpaused) {updateanimationtime ();
Drawmovieframe (canvas);
Invalidateview ();
else {drawmovieframe (canvas);
}} private void Updateanimationtime () {Long now = Android.os.SystemClock.uptimeMillis ();
If the first frame, record starting time if (Mmoviestart = = 0) {Mmoviestart = now;
///Remove the animation when the length int dur = Mmovie.duration ();
if (dur = = 0) {dur = default_movie_duration;
Mcurrentanimationtime = (int) ((now-mmoviestart)% dur) required to display the first few frames;
private void Drawmovieframe (Canvas Canvas) {//Set the frame to be displayed, Draw Mmovie.settime (mcurrentanimationtime);
Canvas.save (Canvas.matrix_save_flag);
Canvas.scale (Mscale, Mscale);
Mmovie.draw (Canvas, Mleft/mscale, Mtop/mscale);
Canvas.restore (); @SuppressLint ("Newapi") private void Invalidateview () {if (mvisible) {if (Build.VERSION.SDK_INT >= Buil D.version_codes.
Jelly_bean) {postinvalidateonanimation (); else {invalidate ();
The following methods did not call------------------------------------/public void Setmovie (Movie mo
VIE) {This.mmovie = movie;
Requestlayout ();
Public Movie Getmovie () {return mmovie;
public void Setmovietime (int time) {mcurrentanimationtime = time;
Invalidate ();
} public void Setpaused (Boolean paused) {this.mpaused = paused;
if (!paused) {Mmoviestart = Android.os.SystemClock.uptimeMillis ()-mcurrentanimationtime;
} invalidate ();
public Boolean ispaused () {return this.mpaused; @SuppressLint ("Newapi") @Override public void onscreenstatechanged (int screenstate) {Super.onscreenstatechang
Ed (screenstate);
mvisible = Screenstate = = screen_state_on;
Invalidateview (); @SuppressLint ("Newapi") @Override protected void onvisibilitychanged (View changedview, int visibility) {Super
. onvisibilitychanged (Changedview, visibility);
mvisible = visibility = = view.visible; Invalidateview(); @Override protected void onwindowvisibilitychanged (int visibility) {super.onwindowvisibilitychanged (visibility)
;
mvisible = visibility = = view.visible;
Invalidateview (); }
// --------------------------------------------------------/
}
SOURCE Download: Http://xiazai.jb51.net/201610/yuanma/AndroidGifDemo (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.