Android display GIF images

Source: Internet
Author: User

Android display GIF images

Today, we will study how to display GIF dynamic images on Android phones.
First, create a custom View under the src directory. The Code is as follows:

 
 
 
 

 

Import android. content. context; import android. graphics. canvas; import android. graphics. movie; import android. util. attributeSet; import android. view. view; public class MyGifView extends View {// indicates the absolute time for starting to play the GIF image private long movieStart = 0; // manage multiple frames in the GIF image using the movie object private Movie movie; public MyGifView (Context context, AttributeSet attrs) {super (context, attrs); movie = Movie. decodeStream (context. getResources () . OpenRawResource (R. drawable. horse) ;}@ Overrideprotected void onDraw (Canvas canvas) {long currentTime = System. currentTimeMillis (); // if (movieStart = 0) {movieStart = currentTime;} // loop playback if (movie! = Null) {int duration = movie. duration (); int relTime = (int) (currentTime-movieStart) % duration); movie. setTime (relTime); movie. draw (canvas, 0, 0); // force re-paint invalidate ();} // if you only want to play the video once, you only need to judge that the value of currentTime-movieStart is greater than duration and super will not be re-painted. onDraw (canvas );}}

Then write an Activity to display the GIF image:

 

 

import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

The XML layout file is:

 

 

     
  
 

As follows:

 

 

 

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.