Display GIF images in basic Android
23:20:17
As we all know, if we use imageview in Android to directly display GIF images, the images will only be parked on the first frame and will not continue. In this case, only a static image is displayed, without the GIF effect. It is easier to use movie to display images;
1. Add imageview to main. XML to specify a GIF image as the background;
2. mainactivity. Java
Package COM. yin. displaygif; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. movie; <br/> Import android. OS. bundle; <br/> Import android. view. view; </P> <p> public class mainactivity extends activity {<br/> private mycustomview mview; <br/> private movie mmovie; <br/> private long mmoviestart; </P> <p> Public void oncre Ate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> mview = new mycustomview (this); </P> <p> setcontentview (mview ); <br/>}</P> <p> // customize a class, inherit view <br/> class mycustomview extends view {</P> <p> Public mycustomview (context) {<br/> super (context ); <br/> // read the file as a file stream <br/> mmovie = movie. decodestream (<br/> getresources (). openrawresource (R. drawable. my_picture); <br/>}< /P> <p> @ override <br/> protected void ondraw (canvas) {</P> <p> long curtime = android. OS. systemclock. uptimemillis (); <br/> // The first playback <br/> If (mmoviestart = 0) {<br/> mmoviestart = curtime; <br/>}</P> <p> If (mmovie! = NULL) {<br/> int duration = mmovie. duration (); </P> <p> int reltime = (INT) (curtime-mmoviestart) % duration); <br/> mmovie. settime (reltime); <br/> mmovie. draw (canvas, 0, 0); </P> <p> // force redraw <br/> invalidate (); </P> <p >}</P> <p> super. ondraw (canvas); <br/>}</P> <p>}