This example for you to share the Android through the movie display GIF format pictures of the code, for your reference, the specific content as follows
public class Commongifview extends View {private resources mresources;
Private Movie Mmovie;
Private long starttime = 0;
private float WidthRatio;
private float HeightRatio;
Public Commongifview {This (context, NULL);
Public Commongifview (context, AttributeSet attrs) {This (context, attrs, 0); Commongifview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr)
;
Mresources = Context.getresources ();
TypedArray ta = context.obtainstyledattributes (attrs, R.styleable.custom_gif_view);
int src_id = Ta.getresourceid (r.styleable.custom_gif_view_gif_src,-1);
SETGIFVIEWBG (src_id);
Ta.recycle (); /** * Set GIF format picture background for view * @description: * @author LDM * @date 2016-2-18 a.m. 9:21:16/private void Set
GIFVIEWBG (int src_id) {if (src_id = = 1) {return;}
Gets the input stream for the corresponding resource file InputStream is = Mresources.openrawresource (src_id); Mmovie = MovIe.decodestream (IS);//decoding input stream is movie Object Requestlayout ();
* * * This method is available for use in the activity/public void Setgifstream (InputStream are) {Mmovie = Movie.decodestream (IS);
Requestlayout ();
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
Long now = Systemclock.uptimemillis ();
if (StartTime = = 0) {//If the first frame, the recording start time starttime = Now; } if (Mmovie!= null) {//If the return value is not equal to NULL, this is a gif picture int duration = Mmovie.duration ();//The time length of the animation is removed if (durati
on = = 0) {duration = 1000;
int currenttime = (int) ((now-starttime)% duration);//Work out the need to display the first few frames Mmovie.settime (currenttime);
Mmovie.draw (Canvas, getwidth ()-Mmovie.width (), GetHeight ()-mmovie.height ());
Float scale = Math.min (WidthRatio, HeightRatio);
Canvas.scale (scale, scale);
Mmovie.draw (canvas, 0, 0);
Invalidate ();
} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) { if (Mmovie!= null) {//If the return value is not equal to NULL, it is a gif picture int w = mmovie.width ();/width int h = mmovie.height ();/height
if (w <= 0) {w = 1;
} if (h <= 0) {h = 1;
int left = Getpaddingleft ();
int right = Getpaddingright ();
int top = Getpaddingtop ();
int bottom = Getpaddingbottom ();
int widthsize, heightsize;
W + left + right;
H + + top + bottom;
W = Math.max (W, Getsuggestedminimumwidth ());
h = Math.max (H, Getsuggestedminimumheight ()); Widthsize = Resolvesizeandstate (w, widthmeasurespec, 0);//depending on the size and measurespec you provide, return the size value you want heightsize = Resolvesizeand
State (H, Heightmeasurespec, 0);
WidthRatio = (float) widthsize/w;
HeightRatio = (float) heightsize/h;
Setmeasureddimension (Widthsize, heightsize);
else {super.onmeasure (widthmeasurespec, Heightmeasurespec);
}
}
}
Custom attribute Res/values/attrs.xml file:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "custom_gif_ View ">
<attr name=" gif_src "format=" reference "></attr>
</declare-styleable>
</resources>
Used in activity:
public class Mainactivity extends activity {
private commongifview view;
Private InputStream is;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
View = (Commongifview) Findviewbyid (r.id.gif_test);
try {is
= Getassets (). Open ("Test01.gif");
View.setgifstream (IS);
}
catch (IOException e) {
e.printstacktrace ();}}}
The above is the entire content of this article, I hope to help you learn.