如何使用Glide Library按图像加载图像?

时间:2017-08-29 11:24:07

标签: android image android-glide

我有一个应用程序包含使用滑动库链接显示的一系列图像,我的应用程序需要很长时间才能加载所有图像,因此可以逐个图像加载图像(逐个)。

Glide.with(context)
    .load(imageId.get(position))
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .priority(Priority.HIGH)
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String  model, Target<GlideDrawable> target, boolean isFirstResource) {   
            return false;
        }
        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }
    })
    .into(holder.CategoryImage);

2 个答案:

答案 0 :(得分:2)

试试这个

Glide.with(context)
        .load(imageId.get(position))
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .priority(Priority.HIGH)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String  model, Target<GlideDrawable> target, boolean isFirstResource) {   
                   return false;
            }
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .thumbnail(0.1f) // this will do the trick
        .into(holder.CategoryImage);

如果不起作用,请尝试这种方式,

Glide.with(context)
        .load(imageId.get(position))
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .priority(Priority.HIGH)
        .override(100,100)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String  model, Target<GlideDrawable> target, boolean isFirstResource) {   
                   return false;
            }
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                Glide.with(context).load(imageId.get(position))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.HIGH)
                    .into(holder.CategoryImage);
                return false;
            }
        })
        .into(holder.CategoryImage);

答案 1 :(得分:1)

使用此代码:

 Glide.with(this)
            .load(sharePref.getUserImage())
            .asBitmap()
            //.centerCrop()
            .placeholder(R.drawable.ic_account_circle_black_48dp)     // .placeholder(R.drawable.user)
            .into(new BitmapImageViewTarget(user_Image) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    user_Image.setImageDrawable(circularBitmapDrawable);
                }
            });