作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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")
}
})
}
}
我错过了什么?
要求:
.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) {
}
});
然后你会得到这个:
关于android - Glide centerCrop() 不适用于 CustomTarget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66071616/
我是一名优秀的程序员,十分优秀!