Objective
We all know ImageView is not the perfect loading GIF format picture, if we in ImageView src specified resources are GIF format, we will be pleasantly surprised to find that the picture will stay in the first frame, that is, there will be no animation effect. Of course, after a few modifications, we can make GIF perfectly loaded on the ImageView.
Body
Android provides us with a movie class that allows us to implement the target of loading GIF format resources. We need to import Android.graphics.Movie This package, of course this is also Android comes with. So our main approach is to inherit a ImageView subclass and load the GIF resource by overwriting the OnDraw method. Words are not much to say, through the code we see more clearly, the end of the text with the source Oh.
PS: Read this article to understand the knowledge of custom view.
Attrs resource file:
< resources > < declare-styleable Span style= "color: #ff0000;" >name = "Gifview" > < attr name = "Isgifimage" format =" Boolean " /> </ declare-styleable > </ Span style= "color: #800000;" >resources >
I've set a custom property isgifimage in here, to let the user set the control to show whether it is a GIF format resource, because non-GIF format resources can also display images in movie, but efficiency is certainly not native control loading mode good, of course, The default isgifimage is true, which is also the default GIF format resource.
Constructor for custom Gifview (Gifview class inherits ImageView)
PublicGifview (Context context, AttributeSet attrs) {Super(context, attrs); //Gets the custom attribute IsgifimageTypedArray array =context.obtainstyledattributes (Attrs, R.styleable.gifview); Isgifimage= Array.getboolean (R.styleable.gifview_isgifimage,true); Array.recycle ();//Recycle is required to get the custom properties, otherwise it will affect the next fetch.//gets the default src attribute of ImageViewImage = Attrs.getattributeresourcevalue ("http://schemas.android.com/apk/res/android", "src", 0); Movie=Movie.decodestream (Getresources (). Openrawresource (image));}
In the construction method of Gifview, we mainly obtain the custom attributes of Gifview. You can return a Typedarray object from Context.obtainstyledattributes (Attrs, R.styleable.gifview), and then get the custom properties from that object separately. Here we need to emphasize a little bit of time r.styleable. Gifview_isgifimage, red when attrs the name of the resource file, and blue is its corresponding property name (see Attrs resource file), the middle is separated by an underscore.
We must recycle ()after we have finished getting the custom properties, otherwise it will affect the next time the control gets the custom properties because the Typedarray object is a public resource.
Then we are passing attrs.getattributeresourcevalue ("http://schemas.android.com/apk/res/android", "src", 0) To get the native src attribute of the ImageView and pass it into the movie instance.
OnDraw method for customizing Gifview
@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas);//Execute the Parent class OnDraw method to draw a non-GIF resource if(Isgifimage) {//if it is a GIF file, execute drawgifimage (), default executiondrawgifimage (canvas); }}Private voiddrawgifimage (canvas canvas) {//Get system Current time LongNowtime =Android.os.SystemClock.currentThreadTimeMillis (); if(Moviestart = = 0){ //for the first load, the start time is set to NowtimeMoviestart =Nowtime; } if(Movie! =NULL){//fault-tolerant processing intDuration = Movie.duration ();//Get GIF duration//if the GIF duration is 100, you can think of non-GIF resources and jump out of processing if(Duration = = 0){ //gets the point in time at which the current frame of the GIF is displayed intReltime = (int) ((Nowtime-moviestart)%duration); Movie.settime (Reltime); //Render GIF imagesMovie.draw (canvas, 0, 0); Invalidate (); } }}
In this method, we first determine whether Isgifimage is true, and if the developer specifies that it is false, call Super.ondraw (canvas) to draw directly without having to call drawgifimage () to reduce efficiency. After all, the constant invalidate () is still quite big for performance efficiency.
If we want to draw the GIF resource, we will infer the time frame that the GIF resource should display according to the time of the system, I believe we can see the code easier to understand, the comment is enough detailed, not much to explain.
Call the resource's XML file:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:gifview= "Http://schemas.android.com/apk/res/com.net168.testgifview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Com.net168.gifview.GifViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/image"Gifview:isgifimage= "false" /></LinearLayout>
One thing to be aware of is xmlns:gifview= "http://schemas.android.com/apk/res/com.net168.testgifview", Where the Red section Gifview.java the package name in which this class resides.
The following is the source code (Gifview.rar for the Lib project, Testgifview.rar for commissioning works ): Gif.rar
Enjoy wind chimes
Source: http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced
Android App series: perfect for running GIF format ImageView (attached source)