bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.59k stars 6.12k forks source link

Glide centerCrop() not working with CustomTarget ImageView #4499

Open ericntd opened 3 years ago

ericntd commented 3 years ago

Glide Version: 4.12.0

Integration libraries: N/A

Device/Android Version: All

Issue details / Repro steps / Use case background: Run the following code Observe that the result is actually centerInside not centerCrop Actual behaviour glide center crop not working

Expected behaviour glide center crop expected behaviour

Glide load line / GlideModule (if any) / list Adapter code (if any):

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // available in this demo but nullable across my app e.g. in notification or certain screens
        val imageView: ImageView? = findViewById<ImageView>(R.id.image)

        Glide.with(this)
                .asBitmap() // required for image in notification
                .load("https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-1600x2400.jpg")
                .centerCrop()
                .into(object: CustomTarget<Bitmap>() { // again since ImageView not always available, this is the best choice I know of
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        imageView?.setImageBitmap(resource)
                        // SUCCESS CALLBACK here
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                        TODO("Not yet implemented")
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                            // FAILURE CALLBACK here
                        }
                })
    }
}

My requirements:

  1. ImageView are not always available e.g. notifications. But when the imageView is available, I'd like to use "center crop" instead of "center inside"
  2. Callbacks are always necessary

Layout XML:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>```

<!--
What is the error message that you got in the log?
You can find some help on diagnosing issues here: https://github.com/bumptech/glide/wiki/Debugging-and-Error-Handling
-->
**Stack trace / LogCat**: N/A

<!-- Bonus points if you attach a relevant screenshot, screen recording or a small demo project -->
![glide centercrop not working](https://user-images.githubusercontent.com/19919208/107121411-404efe80-6860-11eb-880d-354b4ee67ab5.png)

Cross-posting [here
](https://stackoverflow.com/questions/66071616/glide-centercrop-not-working-with-customtarget-imageview)
ericntd commented 3 years ago

I found a solution for my needs:

Can we please update the documentations to something like below though?

val imageView: ImageView? = ...
Glide.with(this)
                .load(imageSourceUrl)
                .centerCrop()
                .into(object: CustomTarget<Drawable>() {
                    override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                        imageView?.setImageDrawable(resource) // above centerCrop() will not work! use imageView.setScaleType instead
                        // SUCCESS CALLBACK here
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                        TODO("Not yet implemented")
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                            // FAILURE CALLBACK here
                        }
                })