Introduction
Android does not recommend the use of GIF pictures, generally png, for GIF image resolution compared to consume resources, but for some animated GIF pictures of the play, if relatively small, if it is still possible, if large, it is recommended to convert GIF images into a frame of PNG pictures, Then play through animation. For small GIF animated images, you can play them in movie. Note that you can no longer use a custom view in the layout.
Activity
public class mainactivity extends Activity { linearlayout root; @Override Public void onCreate(Bundle savedinstancestate) { Super. OnCreate(savedinstancestate);Setcontentview(r.layout. Main); Root = (linearlayout) Findviewbyid(r.id. Root); root. AddView(new customgifview(This, r.drawable. Loading)); }}
<? XML version="1.0" encoding="Utf-8" ?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:id="@+id/root" android:layout_width="Fill_parent" android:layout_height="Fill_parent" Android:background="#666" android:orientation="vertical" > <TextView android:layout_width="Fill_parent" android:layout_height="Wrap_content" Android:text="Simple display gif dynamic graph" /> </linearlayout>
Custom View
/** * Custom View play GIF animation */public class Customgifview extends View { Private Movie mmovie; Private long mmoviestart; public customgifview(context context, int resId) { Super(context); //read in stream (or byte array or file) to decode Create movie object Mmovie = Movie. decodestream(getresources(). Openrawresource(resId)); }Public void onDraw(canvas canvas) { long now = Android.os.SystemClock. uptimemillis(); //System Current moment //First time playback if (mmoviestart = = 0) mmoviestart = now; //Time of animation start if (mmovie ! = null) { int dur = mmovie. Duration(); //The duration of the animation, that is, the time to complete an animation if (dur = = 0) dur = +; int reltime = (int) ((now- mmoviestart) % dur) /c7>; //Note that this is the remainder operation, which will calculate the time of the first frame of the current replay. mmovie. SetTime(reltime); //Set relative to the first frame time of the play, according to the time to determine the number of frames to display Mmovie. Draw(canvas, 0, 0); //force Redrawinvalidate(); } }}
From for notes (Wiz)
Animated picture gif