How to play GIF in android

后端 未结 8 761
死守一世寂寞
死守一世寂寞 2020-11-30 05:08

Hello stackoverflow I\'m trying to develop an android application to play my own GIF, here is the code snippet

MainActivity.java

相关标签:
8条回答
  • 2020-11-30 05:24

    add this to your dependencies (build.gradle)

     allprojects {
        repositories {
            mavenCentral()
        }
    }
    dependencies {
        implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.15'
    }
    

    and use this in xml to show your gif

     <pl.droidsonroids.gif.GifTextView
                        android:id="@+id/gifTextView2"
                        android:layout_width="230dp"
                        android:layout_height="200dp"
                        android:layout_weight="1"
                        android:src="@drawable/dev_option_gif"
                        />
    

    More info at: https://github.com/koral--/android-gif-drawable

    0 讨论(0)
  • 2020-11-30 05:26

    For devices running API 29(Pie) & above you can use AnimatedImageDrawable to play GIF(s) and WebP animated images.

    Here's a sample:

    ImageView imgGif = findViewById(R.id.imgGif);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            try {
                drawable = ImageDecoder.decodeDrawable(ImageDecoder.createSource(getResources(), R.drawable.giphy));
                imgGif.setImageDrawable(drawable);
    
                if (drawable instanceof AnimatedImageDrawable) {
                    ((AnimatedImageDrawable) drawable).start();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    Using this provides more control & features you can use on the image such as:

    • Checking whether the gif is running
    • Starting & stopping the playback.
    • Applying color filters
    • Changing image shape

      and much more

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