Pag動畫框架作為一個第三方框架,它的優缺點與Lottie是相似,此處不過多贅述。如果你們的項目中打算用了,肯定是經過了一定的調研的。Pag動畫框架分幾個版本,有免費的有收費的。我們目前用的社區免費版,只用來展示Pag動畫。高級的版本可以播放視頻、聲音等,酌情選取。
github文檔:
https://github.com/Tencent/libpag
引入依賴
// pag庫的庫
implementation 'com.tencent.tav:libpag:4.2.100'
/// pag的依賴庫 必須添加
implementation "androidx.exifinterface:exifinterface:1.3.3"
混淆配置
-keep class org.libpag.** {*;}
-keep class androidx.exifinterface.** {*;}
使用PagView
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"> <org.libpag.PAGView android:id="@+id/pag" android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
assets中添加文件
?
Java/Kotlin代碼
// 加載Pag文件并開始動畫
findViewById<PAGView>(R.id.pag).apply {
// 把0.pag放在 assets目錄下 composition = PAGFile.Load(assets, "pag/0.pag")
// 無限循環
setRepeatCount(-1)
play()
}