Glide loader未正确加载图像

时间:2017-10-05 11:59:25

标签: android firebase android-glide

我正在使用Glide加载程序来加载我的图像。我的代码正在运行,但它会不断更改图像。这是我的代码:

Glide.with(ctx).load(image).asBitmap()        
          .dontTransform()
          .placeholder(R.drawable.cart)
          .into(new SimpleTarget < Bitmap > () {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                photoImageView.setImageBitmap(resource);
                               Glide.with(ctx).load(image).into(post_image);
                                post_image.setImageBitmap(resource);
                            }

2 个答案:

答案 0 :(得分:0)

嘿你可以使用glide utill class来更好地理解

public class GlideUtil {
public static void loadImage(String url, ImageView imageView) {
    Context context = imageView.getContext();
    if(context!=null || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)) {
        assert context != null;
        ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(context, R.color.blue_grey_500));

        Glide.with(context)
                .load(url)
                .placeholder(cd)
                .crossFade()
                .centerCrop()
                .into(imageView);
    }
}

public static void loadProfileIcon(String url, ImageView imageView) {
    Context context = imageView.getContext();
    if(context!=null || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)) {
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.ic_person_outline_black)
                .dontAnimate()
                .fitCenter()
                .into(imageView);
    }
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void loadImage(String url, ImageView mPhotoView, final ProgressBar mProgress) {
    Context context = mPhotoView.getContext();
    if(context!=null || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)) {
        assert context != null;
        ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(context, R.color.blue_grey_500));

        mProgress.setEnabled(true);
        Glide.with(context)
                .load(url)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        mProgress.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        mProgress.setVisibility(View.GONE);
                        return false;
                    }
                })
                .placeholder(cd)
                .crossFade()
                .centerCrop()
                .into(mPhotoView);
    }
}

}

答案 1 :(得分:0)

由于滑动中图像的延迟加载,会发生图像更改。当滑动下载新图像时,由于您正在重新使用视图,因此先前的图像已经加载。为了防止这种情况,。在加载图像之前加载图像清除前一个图像。

Glide.clear(post_image);

修改: 此外,在从滑行中获取图像之前,请清除imageView的旧图像。

post_image.setImageDrawable(null);

然后从滑翔

加载新图像
Glide.with(ctx)
     .load(image)
     .asBitmap()
     .dontTransform()
     .placeholder(R.drawable.cart)
     .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            post_image.setImageBitmap(resource);
        }
}