在 ImageView 上设置图片背景

3
我的gridView 显示本地存储的图片(imageItem),使用Picasso展示。当通过点击右上角的选中图标选择图片时,图片的状态会更改,并在选定状态下带有蓝色边框,如下所示: blue border 我尝试了以下代码:
public void onClick(View v) {
            if(!v.isSelected()){
                imageItem.setBackgroundResource(R.drawable.photoborder);
            }else{
                imageItem.setBackgroundDrawable(null);
            }
        }
    });

在图片未加载时,我能看到边框。当Picasso完成图片的加载后,边框被覆盖了:

border has been covered

如何在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
我不是很确定,因为我不使用 Picasso,但似乎有多个层被使用:ImageView 作为背景层,勾选标记指示器作为前置层。你所做的是设置 ImageView 的背景,而不是前置层。如果您能够发布适配器中使用的 XML 布局,可能会更清晰明了。 - Andrew T.
3个回答

3
您的布局有两个视图;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);
    }
}

谢谢,我刚刚也想通了。 - Hoa Vu
很高兴你自己解决了这个问题 :) - Andrew T.

0

你可以将边框可绘制对象作为图像视图的层使用

<?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>

0

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

imageItem.setBackgroundDrawable

你实际上需要做的就是坚持使用其中一个背景实例,然后你就能得到你想要的结果。代码应该像这样:
public void onClick(View v) {
        if(!v.isSelected()){
            imageItem.setBackgroundResource(R.drawable.photoborder);
        }else{
            imageItem.setBackgroundResource(null);
        }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接