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


当前回答

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

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

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

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

其他回答

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

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

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

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

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

这就像Unity中的border edge。

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

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

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

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

我将下面的xml设置为可绘制的图像视图的背景。它的工作原理。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke android:width="1dp" android:color="#000000" />
    <padding android:left="1dp" android:top="1dp" android:right="1dp"
        android:bottom="1dp" />
</shape>

然后添加android:background="@drawable/yourXmlFileName"到你的ImageView

创建边界

创建一个xml文件(例如:"frame_image_view.xml")和以下内容在你的可绘制文件夹:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="@dimen/borderThickness"
        android:color="@color/borderColor" />
    <padding
        android:bottom="@dimen/borderThickness"
        android:left="@dimen/borderThickness"
        android:right="@dimen/borderThickness"
        android:top="@dimen/borderThickness" />
    <corners android:radius="1dp" /> <!-- remove line to get sharp corners -->
</shape>

替换@dimen/borderThickness和@color/borderColor与任何你想要或添加相应的dimen/ color。

添加Drawable作为ImageView的背景:

<ImageView
        android:id="@+id/my_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/frame_image_view"
        android:cropToPadding="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

你必须使用android:cropToPadding="true",否则所定义的填充没有效果。或者在ImageView中使用android:padding="@dimen/borderThickness"来实现相同的效果。 如果边框帧父视图而不是ImageView,尝试使用android:adjustViewBounds="true"。

改变边框颜色

在代码中更改边框颜色的最简单方法是使用tintbackground属性。

ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(ColorStateList.valueOf(Color.RED); // changes border color to red

or

ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(getColorStateList(R.color.newColor));

不要忘记定义你的newColor。

我差点就放弃了。

这是我使用滑动加载图像的情况,请参阅关于圆角转换和这里的详细滑动问题

我的ImageView也有相同的属性,每个人都回答这里1,这里2,这里3

android:cropToPadding="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/path_to_rounded_drawable"

但还是没有成功。

经过一段时间的研究,使用前景属性从这个SO回答这里给出一个结果android:前景="@drawable/all_round_border_white"

不幸的是,它给我“不太好”的边界角如下图: