Android中的图片视图的边框?

Pra*_*een 318 android border imageview

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

Pra*_*een 545

我将下面的xml设置为Image View的背景为Drawable.有用.

<?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>
Run Code Online (Sandbox Code Playgroud)

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

  • 然后将`android:background ="@ drawable/yourXmlFileName"添加到`ImageView` (108认同)
  • 边框同时适用于左侧和右侧,但是对于顶部和底部,它将父级填充到顶部. (10认同)
  • @Maurice您可以将cropToPadding设置为true. (4认同)
  • 哦,好,这也是我想要的.请记住为ImageView设置填充. (3认同)
  • 别忘了设置cropToPadding ="true" (3认同)

use*_*239 160

以下是我曾经有黑色边框的代码.请注意,我没有使用额外的xml文件的边框.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/red_minus_icon"
    android:background="#000000"
    android:padding="1dp"/>
Run Code Online (Sandbox Code Playgroud)

  • 是的..它看起来像一个边界.但是当您使用具有透明背景的可绘制图像时,它将无法正确显示带有边框的图像.它显示黑色,无论你有透明.所以你的答案不是最好的方法. (28认同)
  • 是啊!但你有一个边框而没有创建另一个xml文件. (18认同)
  • +1表示没有透明度的图像的简单解决方案. (15认同)
  • 这很糟糕,因为它会造成不必要的透支 (7认同)
  • 使用`android:scaleType ="centerCrop"`调整图像大小时,这不起作用. (6认同)

mdu*_*pls 22

这是我认识的一篇旧帖子,但我认为这可能会帮助那些人.

如果要模拟不与形状的"实心"颜色重叠的半透明边框,请在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>
Run Code Online (Sandbox Code Playgroud)


Ash*_*ini 20

ImageView 在xml文件中

<ImageView
            android:id="@+id/myImage"
            android:layout_width="100dp"
            android:layout_height="100dp"

            android:padding="1dp"
            android:scaleType="centerCrop"
            android:cropToPadding="true"
            android:background="@drawable/border_image"

            android:src="@drawable/ic_launcher" />
Run Code Online (Sandbox Code Playgroud)

保存下面的代码名为border_image.xml,它应该在drawable文件夹中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<gradient android:startColor="#ffffff" 
android:endColor="#ffffff"
android:angle="270" />
<corners android:radius="0dp" />
<stroke android:width="0.7dp" android:color="#b4b4b4" />
</shape>
Run Code Online (Sandbox Code Playgroud)

如果要将圆角设置为图像边框,则可以更改border.xml文件中的一行

   <corners android:radius="4dp" />
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果图像是动态设置的,则需要在代码中再次设置边框,否则图像会超出边框。 (2认同)

Fra*_*s S 6

借助 Material Design 和新的ShapeableImageViewstrokeColor小部件,您可以使用和属性轻松创建具有边框的图像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" />
Run Code Online (Sandbox Code Playgroud)

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

In cases where you might want to add a stroke to rounded image, you can use the shapeAppearanceOverlay attribute. This allows you to draw the bitmap with the provided shape (round in this case). Check the below code for more details:

<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" />
Run Code Online (Sandbox Code Playgroud)

Add the below code to your styles.xml file:

<!-- Circle Shape -->
<style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>
Run Code Online (Sandbox Code Playgroud)

Make sure your app extends the material design theme in order to use the ShapeableImageView.

  • android:padding="2dp" 对于避免截断笔画至关重要 (3认同)

Sup*_*rld 5

创建边框

在 drawable 文件夹中创建一个包含以下内容的 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>
Run Code Online (Sandbox Code Playgroud)

用你想要的任何东西替换@dimen/borderThickness@color/borderColor或添加相应的尺寸/颜色。

将 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" />
Run Code Online (Sandbox Code Playgroud)

您必须使用android:cropToPadding="true",否则定义的填充无效。或者android:padding="@dimen/borderThickness"在您的 ImageView 中使用以实现相同的目的。如果边框框住父级而不是 ImageView,请尝试使用android:adjustViewBounds="true".

更改边框颜色

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

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

或者

ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(getColorStateList(R.color.newColor));
Run Code Online (Sandbox Code Playgroud)

不要忘记定义您的newColor.