Hello stackoverflow
I\'m trying to develop an android application to play my own GIF
, here is the code snippet
MainActivity.java
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
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:
Changing image shape
and much more