Set drawable resource ID in android:src for ImageView using data binding in Android

后端 未结 15 2003
陌清茗
陌清茗 2020-12-04 09:27

I\'m trying to set drawable resource ID to android:src of ImageView using data binding

Here is my object:

public class Recipe implem         


        
相关标签:
15条回答
  • 2020-12-04 09:55

    set image like this,

      <ImageView
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:src="@{model.isActive ? @drawable/white_activated_icon :@drawable/activated_icon}"
            tools:src="@mipmap/white_activated_icon" />
    
    0 讨论(0)
  • 2020-12-04 09:56

    Using Fresco(facebook image library)

     public class YourCustomBindingAdapters {
    
        //app:imageUrl="@{data.imgUri}"
        @BindingAdapter("bind:imageUrl")
        public static void loadImage(SimpleDraweeView imageView, String url) {
            if (url == null) {
                imageView.setImageURI(Uri.EMPTY);
            } else {
                if (url.length() == 0)
                    imageView.setImageURI(Uri.EMPTY);
                else
                    imageView.setImageURI(Uri.parse(url));
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:59

    In your view state or view model class;

     fun getSource(context: Context): Drawable? {
            return ContextCompat.getDrawable(context, R.drawable.your_source)
        }
    

    In your XML;

    <androidx.appcompat.widget.AppCompatImageButton
       .
       .
       .
       android:src="@{viewState.getSource(context)}"
    
    0 讨论(0)
  • 2020-12-04 10:01

    For Kotlin put this to a top level utils file, no static / companion context needed:

    @BindingAdapter("android:src")
    fun setImageViewResource(view: ImageView, resId : Int) {
        view.setImageResource(resId)
    }
    
    0 讨论(0)
  • 2020-12-04 10:03
    public Drawable getImageRes() {
            return mContext.getResources().getDrawable(R.drawable.icon);
        }
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@{viewModel.imageRes}"/>
    
    0 讨论(0)
  • 2020-12-04 10:08

    The more you can do with DataBindingAdapter

    • You can set Image Url, File, Bitmap, Byte Array, Drawable, Drawable Id anything by data binding.
    • You can set Error Image / Placeholder Images too with passing multiple parameters to binding adapter.

    Set any of these types:

    android:src="@{model.profileImage}"
    
    android:src="@{roundIcon ? @drawable/ic_launcher_round : @drawable/ic_launcher_round}"
    
    android:src="@{bitmap}"
    
    android:src="@{model.drawableId}"
    
    android:src="@{@drawable/ic_launcher}"
    
    android:src="@{file}"
    
    android:src="@{`https://placekitten.com/200/200`}"
    

    Set Error image/ Placeholder image

    placeholderImage="@{@drawable/img_placeholder}"
    errorImage="@{@drawable/img_error}"
    
    
    <ImageView
        placeholderImage="@{@drawable/ic_launcher}"
        errorImage="@{@drawable/ic_launcher}"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@{`https://placekitten.com/2000/2000`}"
        />
    

    Tested all the types

    So that becomes possible with single binding adapter. Just copy this method project.

    public class BindingAdapters {
        @BindingAdapter(value = {"android:src", "placeholderImage", "errorImage"}, requireAll = false)
        public static void loadImageWithGlide(ImageView imageView, Object obj, Object placeholder, Object errorImage) {
            RequestOptions options = new RequestOptions();
            if (placeholder instanceof Drawable) options.placeholder((Drawable) placeholder);
            if (placeholder instanceof Integer) options.placeholder((Integer) placeholder);
    
            if (errorImage instanceof Drawable) options.error((Drawable) errorImage);
            if (errorImage instanceof Integer) options.error((Integer) errorImage);
    
            RequestManager manager = Glide.with(App.getInstance()).
                    applyDefaultRequestOptions(options);
            RequestBuilder<Drawable> builder;
    
            if (obj instanceof String) {
                builder = manager.load((String) obj);
            } else if (obj instanceof Uri)
                builder = manager.load((Uri) obj);
            else if (obj instanceof Drawable)
                builder = manager.load((Drawable) obj);
            else if (obj instanceof Bitmap)
                builder = manager.load((Bitmap) obj);
            else if (obj instanceof Integer)
                builder = manager.load((Integer) obj);
            else if (obj instanceof File)
                builder = manager.load((File) obj);
            else if (obj instanceof Byte[])
                builder = manager.load((Byte[]) obj);
            else builder = manager.load(obj);
            builder.into(imageView);
        }
    }
    

    Reason I used Glide to load all objects

    If you ask me why I used Glide to load drawable/ resource id, instead I could use imageView.setImageBitmap(); or imageView.setImageResource();. So the reason is that

    • Glide is an efficient image loading framework that wraps media decoding, memory and disk caching. So you need not to worry about large size images and cache.
    • To make consistency while loading image. Now all types of image resources are loaded by Glide.

    If you use Piccaso, Fresso or any other image loading library, you can make changes in loadImageWithGlide method.

    0 讨论(0)
提交回复
热议问题