我可以使用Glide将.gif文件更改为位图吗?

问题描述 投票:0回答:2

我可以使用Glide将.gif文件更改为位图吗?

我尝试将gif加载到位图,但它显示静态图片。

Glide.with(context)
            .asBitmap()
            .load(R.drawable.image)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(bitmap: Bitmap,transition: Transition<in Bitmap>?) {
                    remoteViews.setImageViewBitmap(R.id.testImage, bitmap)
                    appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // this is called when
                    // imageView is cleared on lifecycle call or for
                    // some other reason.
                    // if you are referencing the bitmap somewhere else too other than this imageView
                    // clear it here as you can no longer have the bitmap
                }
            })
android bitmap gif
2个回答
0
投票

此代码不正确,因为您在Glide中使用asBitmap()。您应该使用asGif()而不是asBitmap()

例如-

Glide.with(itemView.getContext())    
    .asGif()
    .load(thumbPath)                             
    .placeholder(ResourcesCompat.getDrawable(context.getResources(), 
                     R.drawable.image_loading_placeholder, null))
    .centerCrop()
    .into(new ImageViewTarget<GifDrawable>(imageView) {
         @Override
         protected void setResource(@Nullable GifDrawable resource) {
             imageView.setImageDrawable(resource);
         }
     });

0
投票

Glide可以将gif动画加载到Imageview。如果您滑行4+,只需将asBitmap()更改为asGif()。但是请记住,在GIF动画中会消耗大量内存。因此,请明智地使用它。

© www.soinside.com 2019 - 2024. All rights reserved.