ericn
ericn

Reputation: 13113

Glide centerCrop() not working with CustomTarget

I'm getting "center inside" instead of "center crop" enter image description here

activity_main.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>

MainActivity

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

        val imageView = findViewById<ImageView>(R.id.image)

        Glide.with(this)
                .asBitmap()
                .load("https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-1600x2400.jpg")
                .centerCrop()
                .into(object: CustomTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        imageView.setImageBitmap(resource)
                    }

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

                })
    }
}

What am I missing?

Requirements:

Upvotes: 2

Views: 3272

Answers (2)

Jason Ng
Jason Ng

Reputation: 1

I was able to get around this by adding set dimensions for the generated bitmap, then center crop worked. I needed to set the image for a NavigationView drawer header background which doesn't allow you to set scale like in the prev example, so this was my workaround.

Glide.with(this)
                            .asBitmap()
                            .load(currentSet.getVideo())
                            .centerCrop()
                            .apply(new RequestOptions().override(1280, 720))
                            .into(new CustomTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                    headerLinearLayout.setBackground(new BitmapDrawable(getResources(), resource));
                                }

                                @Override
                                public void onLoadCleared(@Nullable Drawable placeholder) {
                                }
                            });

Upvotes: 0

SlothCoding
SlothCoding

Reputation: 1706

For some unknown reason to me there is an issue when you use CustomTarget<Bitmap> inside into(). If you just use it like this:

ImageView imageView = findViewById(R.id.image);
Glide.with(this)
            .asBitmap()
            .load("https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-1600x2400.jpg")
            .centerCrop()
            .into(imageView);

This works and it shows image as centerCrop(). But when you use CustomTarget<Bitmap> it ignores that method. What you can do is this:

ImageView imageView = findViewById(R.id.image);
Glide.with(this)
            .asBitmap()
            .load("https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-1600x2400.jpg")
            .centerCrop()
            .into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    imageView.setImageBitmap(resource);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });

And then you get this:

enter image description here

Upvotes: 5

Related Questions