【问题标题】:set background over the image on ImageView在 ImageView 上的图像上设置背景
【发布时间】:2014-09-03 22:24:07
【问题描述】:

我的 gridView 使用 Picasso 显示来自本地存储的图像 (imageItem)。通过单击右上角的选择图标选择图像时,图像的状态会发生变化,并且在选定状态下,图像具有如下蓝色边框:

我试过这样的:

public void onClick(View v) {
            if(!v.isSelected()){
                imageItem.setBackgroundResource(R.drawable.photoborder);
            }else{
                imageItem.setBackgroundDrawable(null);
            }
        }
    });

当图像尚未加载时,我确实看到了边框。 Picasso 完成图片加载后,边框已被覆盖:

如何在 ImageView 中设置图像边框? 非常感谢您的任何建议!

这是每个图像项的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

       <custome.SquaredImageView
        android:id="@+id/image_item_in_gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher"
        android:background="@drawable/attachment_gallery_images_items_background" />

    <ImageView
        android:id="@+id/imageCheckStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:padding="2dp"
        android:src="@drawable/abc_ic_cab_done_holo_dark" />

</RelativeLayout>

【问题讨论】:

  • 我不太确定,因为我不使用毕加索,但似乎使用了不止 1 层:ImageView 作为后层,复选标记指示器作为前层。您正在做的是设置ImageView 的背景,而不是前层。如果您可以发布适配器中使用的 XML 布局,也许会更清楚。

标签: android gridview imageview


【解决方案1】:

您的布局有 2 个视图; SquaredImageView 显示图像,ImageView 作为选择指示符。当您为SquaredImageView 设置背景时,它将放在SquaredImageView 后面(因为它是背景)。

你需要在SquaredImageView前面添加一个View,然后在代码中修改它的背景。

在您的 XML 布局中添加 View

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right">

    <custome.SquaredImageView
        android:id="@+id/image_item_in_gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        android:background="@drawable/attachment_gallery_images_items_background" />

    <View
        android:id="@+id/imageCheckBorder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/photoborder" />

    <ImageView
        android:id="@+id/imageCheckStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_gravity="right"
        android:layout_marginTop="2dp"
        android:padding="2dp"
        android:src="@drawable/abc_ic_cab_done_holo_dark" />

</FrameLayout >

然后修改代码以更改View 的背景,而不是SquaredImageView。 (在这个例子中,它被命名为imageCheckBorder

public void onClick(View v) {
    if(!v.isSelected()){
        imageCheckBorder.setBackgroundResource(R.drawable.photoborder);
    }else{
        imageCheckBorder.setBackgroundDrawable(null);
    }
}

【讨论】:

  • 谢谢,我也弄明白了
  • 很高兴你自己想出来了 :)
【解决方案2】:

您可以使用可绘制边框作为图像视图的图层

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="@dimen/drawable_selector_border_thickness"
        android:color="#0099CC" />

    <solid android:color="#500099CC" />

</shape>

【讨论】:

    【解决方案3】:

    根据您的代码,您同时使用 2 个不同的实例,即imageItem.setBackgroundResource

    imageItem.setBackgroundDrawable
    

    您真正需要做的只是简单地坚持使用其中一个背景实例,您就会得到您想要的结果。代码应该是这样的

    public void onClick(View v) {
            if(!v.isSelected()){
                imageItem.setBackgroundResource(R.drawable.photoborder);
            }else{
                imageItem.setBackgroundResource(null);
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多