android gif动画解决 fresco方法 Android Studio 3.0环境

版权声明:本文为博主原创文章,转载请保留地址。 https://blog.csdn.net/xuz0917/article/details/79468703

加载和播放顺滑流畅,推荐使用

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);

猜你喜欢

转载自blog.csdn.net/xuz0917/article/details/79468703
今日推荐