【问题标题】:How to set image url as background of an ImageView using data binding?如何使用数据绑定将图像 url 设置为 ImageView 的背景?
【发布时间】:2019-02-12 05:43:03
【问题描述】:

我已经有一个绑定适配器来加载来自 URL 的 ImageView 中的图像。现在,我需要加载背景图像 URL 作为图像视图的背景,并且我正在使用数据绑定、滑动加载图像并将其写入 Kotlin。

如何为它编写一个绑定适配器?

这是我的 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}"/>

【问题讨论】:

  • 简单的只是制作以url和imageview为参数的方法并调用该方法。
  • 这里Binding image using bindAdapter 是同样的例子,而不是毕加索使用滑翔为你的工作
  • @AbhayKoradiya 我已经有了将图像加载到图像视图的方法。如果我对背景图像执行相同操作,则不会显示背景图像
  • 检查@PJain 的答案。
  • 这是我用来将来自 url 的图像加载到图像视图的绑定适配器 @BindingAdapter("ImageUrl") fun loadImage(view: ImageView, imageUrl:String?) { Glide.with( view.context) .load(imageUrl) .into(view) }

标签: android kotlin


【解决方案1】:

我使用了以下绑定适配器,它工作正常。

@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}
 }
 })
 }

【讨论】:

    【解决方案2】:

    试试这个 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>
    

    出于检查目的,我只是为背景图像设置了 alpha。

    希望这个技巧能帮到你。

    【讨论】:

    • 我喜欢这个想法,但它仍然不适合我。您知道如何将从 glide 加载的图像转换为绑定适配器中的位图吗?我猜这会奏效
    • 为什么需要获取图像位图?
    • 您提到的解决方案会影响布局性能,因为我们正在创建一个单独的图像视图只是为了设置背景图像。我看到的大多数解决方案都是将滑动加载的图像转换为位图对象以设置背景。我不知道如何为它编写代码
    • 基本上,位图的工作是通过数据流从 URL 下载图像。然后将该位图设置为图像视图。就是这样。
    【解决方案3】:

    你可以尝试如下

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

    【讨论】:

    • 你能帮我用glide然后把它转换成位图吗?
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2019-09-02
    • 2015-11-13
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多