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


当前回答

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

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

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

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

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

这就像Unity中的border edge。

其他回答

我发现这样做容易得多:

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

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

创建边界

创建一个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。你可以使用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"/>

使用Material design和新的ShapeableImageView小部件,您可以使用strokeColor和strokeWidth属性轻松创建具有边框的图像。这很简单,不需要创建任何额外的XML文件。

<com.google.android.material.imageview.ShapeableImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:strokeColor="@color/black"
        app:strokeWidth="2dp"
        app:srcCompat="@drawable/sample_image" />

上面的代码创建了一个宽度为2dp的黑色边框。

如果你想给圆形图像添加描边,你可以使用shapeAppearanceOverlay属性。这允许您使用所提供的形状(在本例中为圆形)绘制位图。查看下面的代码了解更多细节:

<com.google.android.material.imageview.ShapeableImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:shapeAppearanceOverlay="@style/circleImageView"
        app:srcCompat="@drawable/sample_image"
        app:strokeColor="@color/black"
        app:strokeWidth="2dp" />

将下面的代码添加到你的styles.xml文件中:

<!-- Circle Shape -->
<style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

确保你的应用为了使用ShapeableImageView扩展了材质设计主题。

你必须在res/drawable这段代码中创建一个background.xml

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