How to use GIF pictures on Android (Android Open Source Library android-gif-drawabl)

Source: Internet
Author: User

Use of Android Open Source Library android-gif-drawable

Android's Open Source Library is used to display GIF images on Android. I checked on the internet, everyone said this framework is good, loading large GIF images will not be memory overflow, so I would like to try this open source Library, I under the author's source code and examples, but I do not get up. I do not know why, I went to the Internet to find the use of this open Source Library example found that there is a, I also downloaded down, found or not run up. I decided to try this source code, and finally I can use it now. Now I'm going to teach you how to use this library. people don't want to see what to do. You can download the demo code later.




Source code for 1.android-gif-drawable: https://github.com/koral--/android-gif-drawable


2. Tap it, as shown


3. Click on the download, we can see the following interface PS: is the download. aar file I wrote it wrong.

4. After downloading this file, we right-click the open mode to

5. Then unzip the file into an empty folder, and the copy can also

6. Then get the following

7. Click to open the Jni folder to get the following

8. Copy the jar package (Classes.jar) of these 4 folders and open Source Library to your Android code location as shown in

9. Here is how the authors teach you how to use

PS: Want to see the original, please come here to see: https://github.com/koral--/android-gif-drawable

From XML

The simplest-to-use GifImageView (or)-like- GifImageButton a normal ImageView :

<pl.droidsonroids.gif.gifimageview android:layout_width= "match_parent" android:layout_height= "Match_parent" an droid:src= "@drawable/src_anim" android:background= "@drawable/bg_anim"/>

If Drawables declared by android:src and/or is android:background GIF files then they would be automatically recognized as GifDrawable s and animate D. If given drawable is not a GIF then mentioned-like plain and ImageView ImageButton .

GifTextViewAllows use GIFs as compound drawables and background.

<pl.droidsonroids.gif.giftextview android:layout_width= "match_parent" android:layout_height= "Match_parent" and roid:drawabletop= "@drawable/left_anim" android:drawablestart= "@drawable/left_anim" android:background= "@drawable /bg_anim "/>

From Java code

GifImageView, and has GifImageButton GifTextView also hooks for setters implemented. So animated GIFs can is set by calling and setImageResource(int resId)setBackgroundResource(int resId)

GifDrawableCan is constructed directly from various sources:

        //asset file         gifdrawable giffromassets = new gifdrawable ( getassets (),  "Anim.gif"   );        //resource  (Drawable or raw)          gifdrawable giffromresource = new gifdrawable (  Getresources (), r.drawable.anim );         //byte array         byte[] rawGifBytes = ...         gifdrawable giffrombytes = new gifdrawable ( rawGifBytes  );        //filedescriptor         filedescriptor fd = new randomaccessfile (  "/path/anim.gif",  "R"  ). GETFD ();          gifdrawable giffromfd = new gifdrawable ( fd );         //file path         gifdrawable giffrompath = new gifdrawable (  "/path/anim.gif"  );         //file        file giffile  = new file (Getfilesdir (), "Anim.gif");         Gifdrawable giffromfile = new gifdrawable (Giffile);         //assetfiledescriptor        assetfiledescriptor afd  = getassets (). OPENFD (  "anim.gif"  );         Gifdrawable giffromafd = new gifdrawable ( afd );         //InputStream  (it must support marking)         inputstream sourceis  = ...        bufferedinputstream bis = new  bufferedinputstream ( sourceIs, GIF_LENGTH );         gifdrawable giffromstream = new gifdrawable ( bis );         //direct ByteBuffer         bytebuffer rawgifbytes = ...        gifdrawable  Giffrombytes = new gifdrawable ( rawGifBytes );

Inputstreams was closed automatically in finalizer if gifdrawable was no longer needed so you don ' t need to explicitly clos e them. Calling would recycle() also close underlaying input source.

Note that all input sources need to the begining of ability to rewind. It is required to correctly play animated GIFs (where animation is repeatable) since subsequent frames be decoded on Dema nd from source.

Animation Control

GifDrawableImplements an and so can with its methods and more Animatable MediaPlayerControl :

    • stop ()  -stops the animation, can be called from any thread

    • Start ( )  -starts the animation, can be called from any thread

    • isrunning ()  -Retu RNs Whether animation is currently running or not

    • reset ()  -rewinds the animation, Doe s not restart stopped one

    • setspeed (float factor)  -sets new animation speed factor, eg . Passing 2.0f would double the animation speed

    • seekto (int position)  -seeks animation (w Ithin current loop) to given , position   (in milliseconds)  , only seeking forward is Supported

    • getduration ()  -returns duration of one loop of the animation

      /li>
    • getcurrentposition ()  -returns elapsed time from the beginning of a current loop of Animat Ion

Using MediaPlayerControl

Standard controls for a MediaPlayer (as in Videoview) can is used to control GIF animation and show it current progress .

Just Set as GifDrawable MediaPlayer on your mediacontroller like this:

     @Override     protected void onCreate  ( bundle  savedInstanceState )     {         Super.oncreate ( savedInstanceState );         gifimagebutton  gib = new gifimagebutton ( this );         setcontentview ( gib );         gib.setimageresource (  R.drawable.sample );         final mediacontroller  mc = new mediacontroller ( this );         mc.setmediaplayer (  ( GifDrawable )  gib.getdrawable ()  );         mc.setanchorview ( gib );         Gib.setonclicklistener ( new Onclicklistener ()         {              @Override              public void onClick  ( View v )              {                 mc.show ();            }         } );     }

Retrieving GIF metadata
    • getLoopCount()-Returns a loop count as defined in NETSCAPE 2.0 extension

    • getNumberOfFrames()-Returns number of frames (at least 1)

    • getComment()-Returns comment text ( null if GIF has no comment)

    • getFrameByteCount()-Returns minimum number of bytes that can is used to store pixels of the single frame

    • getAllocationByteCount()-Returns size (in bytes) of the allocated memory used to store pixels of given gifdrawable

    • getInputSourceByteCount()-Returns Length (in bytes) of the backing input data

    • toString()-Returns human readable information about image size and number of frames (intended for debugging purpose)

10.demo:http://pan.baidu.com/s/1gdd27v1


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.