Display Animated GIF

后端 未结 30 1919
无人及你
无人及你 2020-11-22 02:11

I want to display animated GIF images in my aplication. As I found out the hard way Android doesn\'t support animated GIF natively.

However it can display animations

相关标签:
30条回答
  • 2020-11-22 02:31

    @PointerNull gave good solution, but it is not perfect. It doesn't work on some devices with big files and show buggy Gif animation with delta frames on pre ICS version. I found solution without this bugs. It is library with native decoding to drawable: koral's android-gif-drawable.

    0 讨论(0)
  • 2020-11-22 02:32

    I solved the problem by splitting gif animations into frames before saving it to phone, so I would not have to deal with it in Android.

    Then I download every frame onto phone, create Drawable from it and then create AnimationDrawable - very similar to example from my question

    0 讨论(0)
  • 2020-11-22 02:32

    First of all the Android browser should support Animated GIFs. If it doesn't then it's a bug! Have a look at the issue trackers.

    If you're displaying these animated GIFs outside of a browser it might be a different story. To do what you're asking would require external library that supports the decoding of Animated GIFs.

    The first port of call would be to look at Java2D or JAI (Java Advanced Imaging) API, although I would be very surprised if Android Dalvik would support those libraries in your App.

    0 讨论(0)
  • 2020-11-22 02:33

    Glide

    Image Loader Library for Android, recommended by Google.

    • Glide is quite similar to Picasso but this is much faster than Picasso.
    • Glide consumes less memory than Picasso.

    What that Glide has but Picasso doesn't

    An ability to load GIF Animation to a simple ImageView might be the most interesting feature of Glide. And yes, you can't do that with Picasso. Some important links-

    1. https://github.com/bumptech/glide
    2. http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
    0 讨论(0)
  • 2020-11-22 02:33

    Use ImageViewEx, a library that makes using a gif as easy as using an ImageView.

    0 讨论(0)
  • 2020-11-22 02:33

    For only android API (Android Pie)28 and +

    use AnimatedImageDrawable as

    // ImageView from layout
    val ima : ImageView = findViewById(R.id.img_gif)
    // create AnimatedDrawable 
    val decodedAnimation = ImageDecoder.decodeDrawable(
            // create ImageDecoder.Source object
            ImageDecoder.createSource(resources, R.drawable.tenor))
    // set the drawble as image source of ImageView
    ima.setImageDrawable(decodedAnimation)
    // play the animation
    (decodedAnimation as? AnimatedImageDrawable)?.start()
    

    XML code, add a ImageView

    <ImageView
        android:id="@+id/img_gif"
        android:background="@drawable/ic_launcher_background" <!--Default background-->
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="200dp"
        android:layout_height="200dp" />
    

    AnimatedImageDrawable is a child of Drawable and created by ImageDecoder.decodeDrawable

    ImageDecoder.decodeDrawable which further required the instance of ImageDecoder.Source created by ImageDecoder.createSource.

    ImageDecoder.createSource can only take source as a name, ByteBuffer, File, resourceId, URI, ContentResolver to create source object and uses it to create AnimatedImageDrawable as Drawable (polymorphic call)

    static ImageDecoder.Source  createSource(AssetManager assets, String fileName)
    static ImageDecoder.Source  createSource(ByteBuffer buffer)
    static ImageDecoder.Source  createSource(File file)
    static ImageDecoder.Source  createSource(Resources res, int resId)
    static ImageDecoder.Source  createSource(ContentResolver cr, Uri uri)
    

    Note: You can also create Bitmap using ImageDecoder#decodeBitmap.

    Output:

    AnimatedDrawable also supports resizing, frame and color manipulation

    0 讨论(0)
提交回复
热议问题