Ya tengo un adaptador de enlace para cargar la imagen en ImageView proveniente de la URL. Ahora, necesito cargar la URL de la imagen de fondo como fondo de la Vista de imagen y estoy usando el enlace de datos, me deslizo para cargar imágenes y escribirlo en Kotlin.
¿Cómo puedo escribir un adaptador vinculante para el mismo?
Aquí está mi XML
<androidx.appcompat.widget.AppCompatImageView android:id="@+id/ImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:scaleType="centerInside" app:backgroundImageUrl="@{item.backgroundImageUrl}" app:mainImageUrl="@{item.mainImageUrl}"/>
Usé el siguiente adaptador de enlace y funciona bien.
@BindingAdapter("backgroundImageUrl") fun loadBackgroundImage(view: ImageView, imageUrl:String?) { Glide.with(view.context).load(imageUrl).into(object:SimpleTarget<Drawable>() { override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.background = resource} } }) }
Puedes probar de la siguiente manera
try { ImageView i = (ImageView)findViewById(R.id.image); Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); i.setImageBitmap(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Prueba este XML:
<android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_view_background" android:layout_width="250dp" android:layout_height="250dp" android:background="@color/colorPrimary" android:alpha="0.4" app:mainImageUrl="@{item.backgroundImageUrl}" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <ImageView android:id="@+id/image_view_main" android:layout_width="230dp" android:layout_height="230dp" app:mainImageUrl="@{item.mainImageUrl}" android:background="@color/colorPrimaryDark" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </android.support.constraint.ConstraintLayout>
Para verificar, acabo de configurar el alfa para la imagen de fondo.
Espero que este truco te ayude.