Glide学习笔记之回调监听

回调:将imageView的实例传到into()中,当Glide将图片加载完后以后,图片就能显示到ImageView上去了

public Target<TranscodeType> into(ImageView view) {
        Util.assertMainThread();
        if (view == null) {
            throw new IllegalArgumentException("You must pass in a non null View");
        }


        if (!isTransformationSet && view.getScaleType() != null) {
            switch (view.getScaleType()) {
                case CENTER_CROP:
                    applyCenterCrop();
                    break;
                case FIT_CENTER:
                case FIT_START:
                case FIT_END:
                    applyFitCenter();
                    break;
                //$CASES-OMITTED$
                default:
                    // Do nothing.
            }
        }


		// 构建target
        return into(glide.buildImageViewTarget(view, transcodeClass));
    }
public class ImageViewTargetFactory {

	// 最终构建出来的target(一般情况下回事GlideDrawableIamgeViewTarget,当使用asBitmap的时候回构建出BitmapImageViewTarget对象)
		@SuppressWarnings("unchecked")
		public <Z> Target<Z> buildTarget(ImageView view, Class<Z> clazz) {
			if (GlideDrawable.class.isAssignableFrom(clazz)) {
				return (Target<Z>) new GlideDrawableImageViewTarget(view);
			} else if (Bitmap.class.equals(clazz)) {
				return (Target<Z>) new BitmapImageViewTarget(view);
			} else if (Drawable.class.isAssignableFrom(clazz)) {
				return (Target<Z>) new DrawableImageViewTarget(view);
			} else {
				throw new IllegalArgumentException("Unhandled class: " + clazz
						+ ", try .as*(Class).transcode(ResourceTranscoder)");
			}
		}
	}

回到into()方法中最终会通过buildRequest(target)把这个target传到GenericRqeust中,所以在GenericRequest中的target.onResourceReady,就是调用的GlideDrawableIamgeViewTarget的onResourceReady。

@Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
        if (!resource.isAnimated()) {
            //TODO: Try to generalize this to other sizes/shapes.
            // This is a dirty hack that tries to make loading square thumbnails and then square full images less costly
            // by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions.
            // If a drawable is replaced in an ImageView by another drawable with different intrinsic dimensions,
            // the ImageView requests a layout. Scrolling rapidly while replacing thumbs with larger images triggers
            // lots of these calls and causes significant amounts of jank.
            float viewRatio = view.getWidth() / (float) view.getHeight();
            float drawableRatio = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
            if (Math.abs(viewRatio - 1f) <= SQUARE_RATIO_MARGIN
                    && Math.abs(drawableRatio - 1f) <= SQUARE_RATIO_MARGIN) {
                resource = new SquaringDrawable(resource, view.getWidth());
            }
        }
        super.onResourceReady(resource, animation);
        this.resource = resource;
        resource.setLoopCount(maxLoopCount);
        resource.start();
    }
@Override
    protected void setResource(GlideDrawable resource) {
        view.setImageDrawable(resource);
    }
在这个方法中处理了图片的展示和GIF播放的逻辑,一张图片就
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值