Android-Glide 4.0+ 加载GIF并控制播放次数

Android-Glide 4.0+ 加载GIF并控制播放次数,第1张

当前版本4.8.0

实现方式:添加listener,回调方法 onResourceReady 中得到 GifDrawable ,然后设置播放次数

GIF本身是可以设置播放次数的,可参考: 用PS修改GIF动图循环播放次数

平时打开GIF图默认是循环播放的,那是因为看图软件实现了循环播放的原因。可使用浏览器打开GIF图即可。

<pl.droidsonroids.gif.GifImageView    

   android:id="@+id/gif_view"

    android:layout_height="match_parent"

    android:layout_width="match_parent"

    android:scaleType="centerCrop"

    tools:src="@mipmap/app_splash"/>

//使用三方库加载Gif

  private  void  gifDrawableFun() {

   try {

           gifDrawable =new GifDrawable(getResources(),           R.mipmap.app_splash)

         }catch (IOException e) {

    e.printStackTrace()

       }

            gif_view.setImageDrawable(gifDrawable)

}

//使用Glide加载Gif

private void GlideFun(@NonNull View imageView) {

      Glide.with(SplashActivity.this)

               .load(R.mipmap.app_splash)

                .listener(new RequestListener() {

   @Override

              public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target,

                                    boolean isFirstResource) {

                                return false

                             }

     @Override

        public    boolean    onResourceReady(Drawable resource, Object model, Target target,

                                      DataSource dataSource, boolean isFirstResource) {

                        if (resourceinstanceof GifDrawable){

                        //Glide默认会加载多次这里设置只加载1次

                            ((GifDrawable) resource).setLoopCount(1)

          }

                          return false

        }

                         }).into((ImageView) imageView)

}

android:theme="@style/libSplashTheme">

<style name="libSplashTheme" parent="Theme.AppCompat.DayNight.NoActionBar">

    <item name="android:windowBackground">@color/color_splash

</style>

<color name="color_splash">#151c2e</color>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/7893726.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存