The same ImageView will flash in the placeHolder and the image will flash in the background if the placeHolder is empty, for two reasons:

  1. intoMethod will cancel the last load and will eventually be called[Target.onLoadCleared]Method, if it’s directinto(ImageView), the original graph is empty and setplaceHolderIf it is inheritance[CustomViewTarget]Is then called to[CustomViewTarget.onResourceLoading];
  2. [ImageViewTarget.onLoadStarted]Will empty the original diagram and setplaceHolder.

But there are some scenarios that you don’t want to use placeHolder. So you need to have the ability to select whether or not you’re going to load in the placeHolder, so if the placeHolder is not empty, you’re going to load in the placeHolder, and if the placeHolder is empty, you’re going to load in the placeHolder, and then the ImageView is going to display the last loaded image until the current one is loaded or fails to load.

abstract class SeamlessImageViewTarget<Z>(view: ImageView) : CustomViewTarget<ImageView, Z>(view), ViewAdapter {
    private var animatable: Animatable? = null

    override fun onStart(a){ animatable? .start() }override fun onStop(a){ animatable? .stop() }override fun getCurrentDrawable(a): Drawable? = view.drawable

    override fun setDrawable(drawable: Drawable?). {
        view.setImageDrawable(drawable)
    }

    override fun onLoadFailed(errorDrawable: Drawable?). {
        setResourceInternal(null)
        setDrawable(errorDrawable)
    }

    override fun onResourceLoading(placeholder: Drawable?). {
        super.onResourceLoading(placeholder)
        // placeholder empty, not set, will display the previous mapplaceholder? .let { setDrawable(placeholder) } }override fun onResourceReady(resource: Z, transition: Transition<in Z>? {
        if (transition == null| |! transition.transition(resource,this)) {
            setResourceInternal(resource)
        } else {
            maybeUpdateAnimatable(resource)
        }
    }

    override fun onResourceCleared(placeholder: Drawable?).{ animatable? .stop()// placeholder empty, not set, will display the previous mapplaceholder? .let { setDrawable(placeholder) } }private fun setResourceInternal(resource: Z?). {
        // Order matters here. Set the resource first to make sure that the Drawable has a valid and
        // non-null Callback before starting it.
        setResource(resource)
        maybeUpdateAnimatable(resource)
    }

    private fun maybeUpdateAnimatable(resource: Z?). {
        animatable = if (resource is Animatable) {
            resource.apply { start() }
        } else {
            null}}protected abstract fun setResource(resource: Z?).
}

class SeamlessBitmapImageViewTarget(view: ImageView) : SeamlessImageViewTarget<Bitmap>(view) {
    override fun setResource(resource: Bitmap?). {
        view.setImageBitmap(resource)
    }
}

class SeamlessDrawableImageViewTarget(view: ImageView) : SeamlessImageViewTarget<Drawable>(view) {
    override fun setResource(resource: Drawable?). {
        view.setImageDrawable(resource)
    }
}
Copy the code