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


当前回答

我将下面的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。

我将下面的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文件为边界。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/red_minus_icon"
    android:background="#000000"
    android:padding="1dp"/>

添加以下代码到一个形状:

<gradient
    android:angle="135"
    android:endColor="#FF444444"
    android:centerColor="#FFAAAAAA"
    android:startColor="#FFFFFFFF"/>

ét瞧,你得到了一个(或多或少)缩进的边界,光源设置在左上方。调整位图的大小(相对于imageview的大小,我在示例中使用200dp x 200dp imageview和196dp x 196dp的位图,角的半径为14dp)和填充来获得最好的结果。切换结束和开始颜色的斜面效果。

下面是你在图片中看到的形状的完整代码(保存在res/drawable中,例如border_shape.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="135"
        android:endColor="#FF444444"
        android:centerColor="#FFAAAAAA"
        android:startColor="#FFFFFFFF"/>
    <padding
        android:top="2dp"
        android:left="2dp"
        android:right="2dp"
        android:bottom="2dp"/>
    <corners
        android:radius="30dp"/>
</shape>

然后像这样在imageview中调用它:

android:scaleType="center"    
android:background="@drawable/border_shape"
android:cropToPadding="true"
android:adjustViewBounds="true"

下面是圆角位图的代码:

Bitmap getRoundedRectBitmap(Bitmap bitmap, float radius) {
    Paint paint = new Paint();
    PorterDuffXfermode pdmode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xff424242);
    canvas.drawRoundRect(rectF, radius, radius, paint);
    paint.setXfermode(pdmode);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return bm;
}

使用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扩展了材质设计主题。