fresco webp動圖只播放一次

概述

本文適合類似於以下這些需求:

  1. 要求webp動圖播放一遍後就暫停
  2. 要求webp動圖播放一遍後就消失
  3. 要求webp動圖播放一遍後做其他邏輯

具體實現

  1. 在ControllerListener中將Animatable對象轉化成AnimatedDrawable2
  2. 通過AnimatedDrawable2獲取到webp的總幀數
  3. 每執行一幀記錄一次,當記錄的幀數等於總幀數的時候認爲動畫播放了一遍

依賴

  implementation 'com.facebook.fresco:fresco:1.12.0'
  implementation 'com.facebook.fresco:animated-drawable:1.12.0'//支持AnimatedDrawable2
  implementation 'com.facebook.fresco:animated-webp:1.12.0'//支持webp動圖

權限

<uses-permission android:name="android.permission.INTERNET" />

初始化

public class MyApplication extends Application {
  @Override public void onCreate() {
    super.onCreate();
    Fresco.initialize(this);
  }
}

源碼

MainActivity.java

public class MainActivity extends AppCompatActivity {

  //constants
  private static final String URI_TEST_WEBP =
      "https://isparta.github.io/compare-webp/image/gif_webp/webp/1.webp";
  //ui
  private SimpleDraweeView dvMain;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
  }

  private void initView() {
    dvMain = findViewById(R.id.dv_main);

    BaseControllerListener baseControllerListener = new BaseControllerListener<ImageInfo>() {
      @Override
      public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
        if (animatable != null && AnimatedDrawable2.class.isInstance(animatable)) {
          final AnimatedDrawable2 animatedDrawable2 = (AnimatedDrawable2) animatable;
          final int totalCnt = animatedDrawable2.getFrameCount();
          animatedDrawable2.setAnimationListener(new BaseAnimationListener() {
            private int lastFrame; //防止無限循環 適時退出動畫

            @Override
            public void onAnimationFrame(AnimatedDrawable2 drawable, int frameNumber) {

              if (!(lastFrame == 0 && totalCnt <= 1) && lastFrame <= frameNumber) {
                lastFrame = frameNumber;
              } else {
                animatedDrawable2.stop();
              }
            }

            @Override
            public void onAnimationStart(AnimatedDrawable2 drawable) {
              lastFrame = -1;
            }

            @Override
            public void onAnimationStop(AnimatedDrawable2 drawable) {

            }
          });
        }
      }

      @Override
      public void onFailure(String id, Throwable throwable) {
        Log.e("test", throwable.getMessage());
      }
    };

    PipelineDraweeControllerBuilder builder = Fresco.newDraweeControllerBuilder()
        .setUri(Uri.parse(URI_TEST_WEBP))
        .setOldController(dvMain.getController())
        .setAutoPlayAnimations(true)
        .setControllerListener(baseControllerListener);
    dvMain.setController(builder.build());
  }
}

activity_main.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:gravity="center"
    tools:context=".MainActivity"
    >

  <com.facebook.drawee.view.SimpleDraweeView
      android:id="@+id/dv_main"
      android:layout_width="300dp"
      android:layout_height="300dp"
      app:actualImageScaleType="fitXY"
      />

</LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章