1. 程式人生 > >android gif動畫解決 fresco方法 Android Studio 3.0環境

android gif動畫解決 fresco方法 Android Studio 3.0環境

載入和播放順滑流暢,推薦使用

1、引入fresco

編輯 build.gradle 檔案

dependencies {
  // 其他依賴
  compile 'com.facebook.fresco:fresco:0.12.0'

  / 在 API < 14 上的機器支援 WebP 時,需要新增
  compile 'com.facebook.fresco:animated-base-support:0.12.0'

  // 支援 GIF 動圖,需要新增
  compile 'com.facebook.fresco:animated-gif:0.12.0'

  // 支援 WebP (靜態圖+動圖),需要新增
compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0' // 僅支援 WebP 靜態圖,需要新增 compile 'com.facebook.fresco:webpsupport:0.12.0' }

當然,這裡的0.12.0可以修改為其他版本,版本可以到官方 https://github.com/facebook/fresco去檢視一下。


注意,如果出現了引入無法合併的問題,如下

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'
. > java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

我用了笨方法,依次去掉後邊的compile,挨個測試,雖然笨但是有效

2、在xml佈局檔案中, 加入名稱空間:

<!-- 其他元素-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco
="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent">

加入SimpleDraweeView:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="130dp"
    android:layout_height="130dp"
    fresco:placeholderImage="@drawable/my_drawable"/>

3、java程式碼新增,開始載入圖片:

Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
/**載入靜態圖
draweeView.setImageURI(uri);  
*/

/**載入gif圖
*/
DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setUri(uri)
                        .setAutoPlayAnimations(true)
                        .build();
draweeView.setController(controller);