【发布时间】:2020-07-03 15:09:01
【问题描述】:
我的视图模型:
init{
updateWallPaper()
}
private var _wallpaper = MutableLiveData<Bitmap>()
val wallpaper: LiveData<Bitmap>
get() = _wallpaper
fun updateWallPaper() {
val file = appCtx.getWallpaperFile()
if(file.exists()) {
_wallpaper.value = BitmapFactory.decodeFile(file.absolutePath)
}
}
还有我家Activity.xml:
<ImageView
android:id="@+id/imageview_main_home_img"
android:layout_width="match_parent"
android:layout_height="324dp"
android:scaleType="fitXY"
android:src="@drawable/sample_image"
app:layout_constraintTop_toTopOf="parent"
app:load="@{homeViewModel.wallpaper }" />
我要做的就是把这张图片换到别的地方,图片src实时变化。
我尝试了很多方法,但都失败了,我想知道如何将实时数据应用到 src。
操作onresume是正常的,但是每次回老家都运行这个方法,所以觉得很浪费内存,所以打算改成绑定live data。
【问题讨论】:
标签: android kotlin mvvm imageview android-livedata