Android Glide:在加载实际图像之前显示模糊图像

问题描述 投票:11回答:6

我正在开发一个Android应用程序,向用户显示全屏图像。从服务器获取图像。我正在使用Glide来显示图像。但是我希望在显示实际图像之前显示非常小的模糊图像。缓存图像后,应显示直接完整大小的图像。

图像显示流程如下: - 如果是第一次下载图像,请首先下载小尺寸图像,然后下载全分辨率图像。 - 如果之前已下载图像,则直接显示全尺寸图像。

我在Glide库中找不到任何方法,它告诉我缓存中是否存在文件。

任何想法,如何做到这一点。

android android-glide
6个回答
9
投票
Glide.with(context.getApplicationContext())
                        .load(Your Path)   //passing your url to load image.
                        .override(18, 18)  //just set override like this
                        .error(R.drawable.placeholder)
                        .listener(glide_callback)
                        .animate(R.anim.rotate_backward)
                        .centerCrop()
                        .into(image.score);

5
投票

更好的是你可以从这个库中获得想法或使用这个库。

在Android中使用Blur和Original图像的两种方法。

1)最初加载模糊图像服务器URL路径并获得成功位图缓存机制以加载原始(大)图像服务器URL路径。

点1是可能的,我得到了你的答案(要求你的服务器团队获得模糊图像或低质量图像使模糊,然后加载大图像)。 APK Link

Glide.with(mContext)
    .load(image.getMedium())
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                    // Do something with bitmap here.
                    holder.thumbnail.setImageBitmap(bitmap);
                    Log.e("GalleryAdapter","Glide getMedium ");

                    Glide.with(mContext)
                            .load(image.getLarge())
                            .asBitmap()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                                    // Do something with bitmap here.
                                    holder.thumbnail.setImageBitmap(bitmap);
                                    Log.e("GalleryAdapter","Glide getLarge ");
                                }
                            });
                }
            });

2)Facebook的Fresco是一个新的Android图像库。这是介绍图书馆的官方博客文章。它支持通过网络传输渐进式JPEG图像,这对于通过慢速连接显示大图像非常有用。人们可以看到图像质量逐渐提高。

Progressive JPEGs with Facebook Fresco


5
投票

与Glide v4:

Glide.with(context)
    .load(URL)
    .thumbnail(0.1f)
    .into(image_view)

0
投票

使用BitmapFactory.Options.inSampleSize生成图像的缩减采样版本,并上载两个版本。加载样本图像(这应该花费更少的时间),当下载更大的版本时,切换到该图像。您还可以使用TransitionDrawable进行淡入淡出过渡。


0
投票

这不是一个完美的方式,但它是最简单的方法。

将placeHolder图像设置为模糊并将其设置为滑动。然后它总是显示模糊图像,然后加载实际图像后它会出现。

你可以参考这个代码来使用滑行。

//crete this method into your Utils class and call this method wherever you want to use.
//you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
    if (context == null || context.isDestroyed()) return;

    //placeHolderUrl=R.drawable.ic_user;
    //errorImageUrl=R.drawable.ic_error;
        Glide.with(context) //passing context 
                .load(getFullUrl(url)) //passing your url to load image.
                .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                .fitCenter()//this method help to fit image into center of your ImageView 
                .into(imageView); //pass imageView reference to appear the image.
}

-1
投票

使用BitmapFactory.Options.inSamleSize加载图像的缩减采样版本。然后加载较大的图像并使用TransitionDrawable进行淡入淡出过渡

希望这会帮助你!干杯!

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