【发布时间】:2021-10-27 19:41:42
【问题描述】:
我正在尝试使用此代码从 URL 加载图像:
@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val executor = Executors.newSingleThreadExecutor()
val handler = Handler(Looper.getMainLooper())
var image: Bitmap?
executor.execute {
try {
val `in` = URL(imgUri).openStream()
image = BitmapFactory.decodeStream(`in`)
handler.post {
this.setImageBitmap(image)
executor.shutdown()
}
} catch (e: Exception) {
Log.e("Tag", "onCreate: ${e.message}")
}
}
}
}
这是xml
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_album_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:imageUrl="@{albumsObject.albumImage}"
android:src="@drawable/img_album_cover" />
它工作得很好..但是有问题 1-当我运行模拟器时,笔记本电脑正在升温并且风扇变得非常响亮 2-如果我在回收站列表上滚动查看缓慢绑定的图像,我可以在下一张照片上看到上一张照片
这段代码有问题吗?或者有任何人有另一种方式从 URL 加载图像而不使用任何第三方 .. 只是 Android SDK
【问题讨论】:
-
I can see the previous photo on the upcoming photo然后先使用setImageBitmap(null);。
标签: android image kotlin url data-binding