gpt4 book ai didi

android - Glide centerCrop() 不适用于 CustomTarget

转载 作者:行者123 更新时间:2023-12-05 00:15:35 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com