Android 要在app上播放gif的方式有很多種, 可以使用ImageView輪撥, WebView來播放等等。
在網路上收尋到第三方Library : GifImageView搭配Glide來播放gif檔。Android上的Glide主要是處理圖片與影片的技術,讓原本的加載流程更加順暢。
Step 1: 引用Library
在app的build.gradle中引用 pl.droidsonroids.gif:android-gif-drawable:1.1.17
build.gradle1 2 3 4 5
| dependencies { ... implementation 'pl.droidsonroids.gif:android-gif-drawable:1.1.17' }
|
Step 2: 使用GifImageView
GifImageView繼承ImageView,使用方式與一般使用ImageView一樣。
在xml檔案中使用pl.droidsonroids.gif.GifImageView即可。
activity_main.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<pl.droidsonroids.gif.GifImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/gif_image_view" /> </RelativeLayout>
|
執行結果