android - Glide centerCrop() 不适用于 CustomTarget

标签 android android-glide android-bitmap

我得到的是“中心内部”而不是“中心裁剪” 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>

主要 Activity

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")
                    }

                })
    }
}

我错过了什么?

要求:

  • 在推送通知的情况下,imageView 不可用,因此 .into(CustomTarget<>)使用而不是 .into(imageView)
  • 成功和失败的回调是必要的

最佳答案

出于某种未知的原因,当您使用 CustomTarget<Bitmap> 时出现问题里面into() 。如果你只是像这样使用它:

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);

这有效,并将图像显示为 centerCrop() 。但是当你使用CustomTarget<Bitmap>时它忽略该方法。你可以做的是:

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) {

                }
            });

然后你会得到这个:

enter image description here

关于android - Glide centerCrop() 不适用于 CustomTarget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66071616/

相关文章:

java - 如何使用 Glide 通过字节数组加载图像?

java - Glide 在我的 Android 应用程序中不显示图像

android - 无法在外部存储上保存文件,即使有用户权限 [Android]

Android libc版本和malloc实现

java - 无法使用 Glide 库加载图像

android - LruCache 返回 null

android - Android 上的多线程问题

android - Fabric crashlytics 到 Firebase 迁移

android - java.lang.NoClassDefFoundError : com. google.firebase.FirebaseOptions 我尝试了所有解决方案但我不知道解决方案

android - 通过 API 获取 Android 订阅的用户历史交易记录