Display GIF images in basic Android

Source: Internet
Author: User

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>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.