When an ImageView loads different images, if the width/height is set to WRAP_content, it needs to be called before switching imagessetImageResourse(0)Clean up the previous image, or the image will be too small.

The principle is that Glide will actively perform a calculation logic for the width/height of wrAP_content

There’s logic in the ViewTarget

Void getSize(@nonnull SizeReadyCallback cb) {// currentWidth = getTargetWidth(); int currentHeight = getTargetHeight(); if (isViewStateAndSizeValid(currentWidth, currentHeight)) { cb.onSizeReady(currentWidth, currentHeight); return; } if (! cbs.contains(cb)) { cbs.add(cb); } if (layoutListener == null) { ViewTreeObserver observer = view.getViewTreeObserver(); layoutListener = new SizeDeterminerLayoutListener(this); observer.addOnPreDrawListener(layoutListener); }}Copy the code

Then watch getTargetWidth

private int getTargetWidth() { int horizontalPadding = view.getPaddingLeft() + view.getPaddingRight(); LayoutParams layoutParams = view.getLayoutParams(); int layoutParamSize = layoutParams ! = null ? layoutParams.width : PENDING_SIZE; return getTargetDimen(view.getWidth(), layoutParamSize, horizontalPadding); } private int getTargetDimen(int viewSize, int paramSize, int paddingSize) { int adjustedParamSize = paramSize - paddingSize; if (adjustedParamSize > 0) { return adjustedParamSize; } if (waitForLayout && view.isLayoutRequested()) { return PENDING_SIZE; } // If the imageView already has a width/height, use the current width/height, Int adjustedViewSize = viewSize - paddingSize; if (adjustedViewSize > 0) { return adjustedViewSize; } if (! view.isLayoutRequested() && paramSize == LayoutParams.WRAP_CONTENT) { return getMaxDisplayLength(view.getContext()); } return PENDING_SIZE; }Copy the code

GetWidth ()/getHeight() already has the same width/height as the previous image when the ImageView reloads the image

Note that setImageResourse does not allow you to Glide load an image directly after the view is drawn. Post the loading request to the Handler for processing, and then call the loading option after the View is drawn.