如何在Android中为ImageView设置边框并更改其颜色?


当前回答

对于那些正在搜索自定义边界和形状的ImageView。你可以使用android-shape-imageview

只需添加compile 'com.github.siyamed:android-shape-imageview:0.9。+@aar'到build.gradle。

并使用在你的布局。

<com.github.siyamed.shapeimageview.BubbleImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/neo"
    app:siArrowPosition="right"
    app:siSquare="true"/>

其他回答

下面是我对这个漫长麻烦的最简单的解决方案。

<FrameLayout
    android:layout_width="112dp"
    android:layout_height="112dp"
    android:layout_marginLeft="16dp" <!-- May vary according to your needs -->
    android:layout_marginRight="16dp" <!-- May vary according to your needs -->
    android:layout_centerVertical="true">
    <!-- following imageView acts as the boarder which sitting in the background of our main container ImageView -->
    <ImageView
        android:layout_width="112dp"
        android:layout_height="112dp"
        android:background="#000"/>
    <!-- following imageView holds the image as the container to our image -->
    <!-- layout_margin defines the width of our boarder, here it's 1dp -->
    <ImageView
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_margin="1dp"
        android:id="@+id/itemImageThumbnailImgVw"
        android:src="@drawable/banana"
        android:background="#FFF"/> </FrameLayout>

在下面的答案中,我已经解释得很好了,请大家也看看!

我希望这将有助于其他人在那里!

我发现这样做容易得多:

1)编辑帧内有内容(用9patch工具)。

2)将ImageView放置在线性布局中,并设置你想要的框架背景或颜色作为线性布局的背景。当你设置帧本身有内容时,你的ImageView将在帧内(就在你用9patch工具设置内容的地方)。

对于那些正在搜索自定义边界和形状的ImageView。你可以使用android-shape-imageview

只需添加compile 'com.github.siyamed:android-shape-imageview:0.9。+@aar'到build.gradle。

并使用在你的布局。

<com.github.siyamed.shapeimageview.BubbleImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/neo"
    app:siArrowPosition="right"
    app:siSquare="true"/>

你可以在Android Studio中使用9个补丁来制作边框!

我一直在寻找解决方案,但我没有找到,所以我跳过了这部分。

然后我去Firebase资产的谷歌图像,我偶然发现他们使用9patch。

这是链接:https://developer.android.com/studio/write/draw9patch

你只需要拖动边缘所在的位置。

这就像Unity中的border edge。

这是我知道的一个老帖子,但我想这可能会帮助到一些人。

如果你想模拟一个半透明的边框,不重叠形状的“纯”色,然后在你的xml中使用这个。注意,我在这里根本没有使用“stroke”标签,因为它似乎总是与实际绘制的形状重叠。

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

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#55111111" />

            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#ff252525" />
        </shape>
    </item>
</layer-list>