BitmapをImageViewにbindingしたい
Bitmap形式の画像をImageViewに表示するには、kotlinプログラム上ではImageView#setImageBitmap()を使いますが、同じことをDataBindingで実現するには、app:imageBitmap属性を使います。
<layout 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"> <data> <variable name="viewModel" type="com.example.testapp.TestViewModel" /> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" app:imageBitmap="@{viewModel.bmp}" /> </LinearLayout></layout>ここで、ViewModelのbmpプロパティは、MutableLiveData<Bitmap>です。実際に画像をセットするときは、bmp.value = XXXのように設定します。