如果使用.asBitmap(),则Glide侦听器无法正常工作

时间:2016-03-21 10:44:55

标签: java android imageview android-glide

在一个Android项目中,我有这段代码来获取ImageView中的图像

XML

<package.RoundedImageView
                    android:id="@+id/options_photo_icon"
                    android:layout_width="@dimen/registration_icons_height"
                    android:layout_height="@dimen/registration_icons_height"
                    android:maxWidth="@dimen/registration_icons_height"
                    android:maxHeight="@dimen/registration_icons_height"
                    android:src="@drawable/no_image"
                    android:foregroundGravity="center"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1" />

的java

Glide
                    .with(context)
                    .load(url)
                    .asBitmap()
                    .centerCrop()
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            //do something
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            //do somethig else.
                            return false;
                        }
                    })
                    .into(options_photo_icon);

但是我收到了这个错误:

  

错误:(212,21)错误:找不到合适的方法   listener(&gt;)方法   GenericRequestBuilder.listener(RequestListener)   不适用(参数不匹配;&gt;无法转换为   RequestListener)方法   BitmapRequestBuilder.listener(RequestListener)   不适用(参数不匹配;&gt;无法转换为   RequestListener)

如果我删除.asBitmap()我没有得到错误,但我需要.asBitmap()为其他东西。 如果我是asBitmap,我不能使用听众吗?

2 个答案:

答案 0 :(得分:0)

试试这个

SimpleTarget target = new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
          imageView.setImageBitmap(resource);
        }
    };
Glide.with(getApplicationContext()).load(profileUrl)
.asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.profile_placeholder)
.error(R.drawable.profile_placeholder).into(target);

答案 1 :(得分:0)

// Put asBitmap() right after Glide.with(context)   ,,. 4.0+
// And for SubsamplingScaleImageView use SimpleTarget

  Glide.with(context)
                .asBitmap()
                .load(images[position])
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .into(new SimpleTarget<Bitmap>(width, height) {
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                        subsampleImageView.setImage(ImageSource.bitmap(bitmap)); //For SubsampleImage
                    }
                });