Map bitmap bindings in memory to ImageView

I have a bitmap in memory (it is loaded from the server application through its own TCP / IP protocol), which I want to bind to ImageView. However, I can manually snap the image using setImageBitmap; if I use the data binding library to bind other controls, the image is not displayed. How can I use a data binding library to bind to a property containing a Bitmap object?

+4
source share
2 answers

You should do this using @BindingAdaptersomething like:

@BindingAdapter("bind:imageBitmap")
public static void loadImage(ImageView iv, Bitmap bitmap) {
   iv.setImageBitmap(bitmap);
}

ImageView bind:imageBitmap="@{...}", ... , Bitmap.

+14

android.databinding.adapters.ImageViewBindingAdapter, .

, , , :

@Bindable
public Drawable getDrawable() {
    return new BitmapDrawable(context.getResources(), bitmap);
}

ImageView - :

android:src="@{viewModel.drawable}"

, viewModel โ€‹โ€‹ .

, ImageViewBindingAdapter :

@BindingAdapter("android:src")
public static void setImageDrawable(ImageView view, Drawable drawable) {
    view.setImageDrawable(drawable);
}
0

Source: https://habr.com/ru/post/1628040/


All Articles