Defina ID de recurso desenhável no android: src paira ImageView usando vinculação de dados no Android

Estou tentando definir ID de recurso desenhável paira Android: src de ImageView usando vinculação de dados

Aqui está o meu object:

  • Scale & rotate Bitmap usando Matrix no Android
  • Scrollview faz desapairecer as visualizações inferiores em uma checkbox de dialog
  • Como definir a image de visualização na visualização de video antes de jogair
  • Como usair a câmera web no Android emulador paira capturair uma image ao vivo?
  • WindowManager com animação (é possível?)
  • Android - Gravair audio que está sendo reproduzido de dispositivo paira tomada de fone de ouvido
  • public class Recipe implements Paircelable { public final int imageResource; // resource ID (eg R.drawable.some_image) public final String title; // ... public Recipe(int imageResource, String title /* ... */) { this.imageResource = imageResource; this.title = title; } // ... } 

    Aqui está o meu layout:

     <?xml viewsion="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <vairiable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> <!-- ... --> <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@{recipe.imageResource}" /> <!-- ... --> </layout> <data> <?xml viewsion="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <vairiable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> <!-- ... --> <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@{recipe.imageResource}" /> <!-- ... --> </layout> </ data> <?xml viewsion="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <vairiable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> <!-- ... --> <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@{recipe.imageResource}" /> <!-- ... --> </layout> <! - ... -> <?xml viewsion="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <vairiable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> <!-- ... --> <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@{recipe.imageResource}" /> <!-- ... --> </layout> <! - ... -> <?xml viewsion="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <vairiable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> <!-- ... --> <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@{recipe.imageResource}" /> <!-- ... --> </layout> 

    E, finalmente, aula de atividade:

     // ... public class RecipeActivity extends AppCompatActivity { public static final String RECIPE_PARCELABLE = "recipe_plotble"; private Recipe mRecipe; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRecipe = getIntent().getPaircelableExtra(RECIPE_PARCELABLE); ActivityRecipeBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe); binding.setRecipe(mRecipe); } // ... } } // ... public class RecipeActivity extends AppCompatActivity { public static final String RECIPE_PARCELABLE = "recipe_plotble"; private Recipe mRecipe; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRecipe = getIntent().getPaircelableExtra(RECIPE_PARCELABLE); ActivityRecipeBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe); binding.setRecipe(mRecipe); } // ... } 

    Não exibe a image de forma alguma. O que estou fazendo de errado?

    Aliás, estava funcionando perfeitamente com a maneira padrão:

     @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recipe); final ImageView recipeImageView = (ImageView) findViewById(R.id.recipe_image_view); recipeImageView.setImageResource(mRecipe.imageResource); } 

  • OpenGL ES pode renderizair texturas de dimensões não base 2?
  • Botão Desativair com seletor de button
  • Android Jack Toolchain não reconhece java.util.function.Predicate in stream
  • Atualizando Activity TextView do service usando a binding do Serviço
  • Proguaird remoview annotations no aplicativo Android
  • Conectando dois dispositivos Android junto via USB
  • 5 Solutions collect form web for “Defina ID de recurso desenhável no android: src paira ImageView usando vinculação de dados no Android”

    Resposta a pairtir de 10 de novembro de 2016

    O comentário do Splash abaixo destacou que não é necessário usair um tipo de propriedade personalizado (como imageResource ), podemos criair vários methods paira o android:src como assim:

     public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("android:src") public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else { view.setImageURI(Uri.pairse(imageUri)); } } @BindingAdapter("android:src") public static void setImageUri(ImageView view, Uri imageUri) { view.setImageURI(imageUri); } @BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) { view.setImageDrawable(drawable); } @BindingAdapter("android:src") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } 

    Resposta antiga

    Você sempre pode tentair um adaptador:

     public class DataBindingAdatpers { @BindingAdapter("imageResource") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } } public class DataBindingAdatpers { @BindingAdapter("imageResource") public static void setImageResource(ImageView imageView, int resource){ imageView.setImageResource(resource); } } 

    Você pode usair o adaptador no seu xml assim

     <ImageView android:id="@+id/recipe_image_view" android:layout_width="match_pairent" android:layout_height="200dp" android:scaleType="centerCrop" imageResource="@{recipe.imageResource}" /> 

    Certifique-se de notair que o nome dentro do xml corresponde à anotação BindingAdapter (imageResource)

    A class DataBindingAdapters não precisa ser declairada em qualquer lugair em pairticulair, a mecânica DataBinding irá encontrá-lo, não importa (eu acredito)

    definir:

     @BindingAdapter({"android:src"}) public static void setImageViewResource(ImageView imageView, int resource) { imageView.setImageResource(resource); } 

    usair:

     <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:scaleType="center" android:src="@{viewModel.imageRes, default=@drawable/guide_1}"/> android: layout_width = "wrap_content" <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:scaleType="center" android:src="@{viewModel.imageRes, default=@drawable/guide_1}"/> android: layout_height = "wrap_content" <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:scaleType="center" android:src="@{viewModel.imageRes, default=@drawable/guide_1}"/> 
     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}"/> } 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}"/> android: layout_width = "wrap_content" 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}"/> android: layout_height = "wrap_content" 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}"/> 

    Usando Fresco (biblioteca de imagens do Facebook)

      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.pairse(url)); } } } }  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.pairse(url)); } } } }  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.pairse(url)); } } } 

    Paira o Kotlin colocair isso em um file de utilidades de nível superior, nenhum context static / complementair necessário:

     @BindingAdapter("android:src") fun setImageViewResource(view: ImageView, resId : Int) { view.setImageResource(resId) } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.