Android 中图像视图的边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:21:52  来源:igfitidea点击:

Border for an Image view in Android?

androidborderimageview

提问by Praveen

How can I set a border for an ImageViewand change its color in Android?

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

回答by Praveen

I set the below xml to the background of the Image View as Drawable. It works.

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

And then add android:background="@drawable/yourXmlFileName"to your ImageView

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

回答by user609239

Following is the code that i used to have black border. Note that i have not used extra xml file for border.

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

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

回答by mdupls

This is an old post I know, but I thought this might possibly help someone out there.

这是我知道的旧帖子,但我认为这可能会帮助那里的人。

If you want to simulate a translucent border that doesn't overlap the shape's "solid" color, then use this in your xml. Note that I don't use the "stroke" tag here at all as it seems to always overlap the actual drawn shape.

如果要模拟不与形状的“纯”色重叠的半透明边框,请在 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>

回答by Ashish Saini

ImageViewin xml file

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

save below code with the name of border_image.xmland it should be in drawable folder

使用名称保存下面的代码,border_image.xml它应该在可绘制文件夹中

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

    <gradient
        android:angle="270"
        android:endColor="#ffffff"
        android:startColor="#ffffff" />

    <corners android:radius="0dp" />

    <stroke
        android:width="0.7dp"
        android:color="#b4b4b4" />
</shape>

if you want to give rounded corner to the border of image then you may change a line in border.xml file

如果你想给图像的边框赋予圆角,那么你可以在 border.xml 文件中更改一行

<corners android:radius="4dp" />

回答by SuperSmartWorld

Create Border

创建边框

Create an xml file (e.g. "frame_image_view.xml") with the following content in your drawable folder:

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

Replace @dimen/borderThicknessand @color/borderColorwith whatever you want or add corresponding dimen / color.

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

Add the Drawable as background to your ImageView:

将 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" />

You have to use android:cropToPadding="true", otherwise the defined padding has no effect. Alternatively use android:padding="@dimen/borderThickness"in your ImageView to achieve the same. If the border frames the parent instead of your ImageView, try to use android:adjustViewBounds="true".

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

Change Color of Border

更改边框颜色

The easiest way to change your border color in code is using the tintBackgound attribute.

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

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

Don't forget to define your newColor.

不要忘记定义您的newColor.

回答by Stephen Niedzielski

Add a background Drawable like res/drawables/background.xml:

添加一个像 res/drawables/background.xml 这样的背景 Drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@android:color/white" />
  <stroke android:width="1dp" android:color="@android:color/black" />
</shape>

Update the ImageView background in res/layout/foo.xml:

更新 res/layout/foo.xml 中的 ImageView 背景:

...
<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="1dp"
  android:background="@drawable/background"
  android:src="@drawable/bar" />
...

Exclude the ImageView padding if you want the src to draw over the background.

如果您希望 src 在背景上绘制,请排除 ImageView 填充。

回答by Atul O Holic

This has been used above but not mentioned exclusively.

这已在上文中使用,但并未专门提及。

setCropToPadding(boolean);

If true, the image will be cropped to fit within its padding.

如果为true,图像将被裁剪以适合其填充。

This will make the ImageViewsource to fit within the padding's added to its background.

这将使ImageView源适合添加到其背景的填充。

Via XML it can be done as below-

通过 XML,它可以完成如下 -

android:cropToPadding="true"

回答by Cabezas

you must create a background.xml in res/drawable this code

您必须在 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>

回答by Khemraj

For those who are searching custom border and shape of ImageView. You can use android-shape-imageview

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

image

图片

Just add compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'to your build.gradle.

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

And use in your layout.

并在您的布局中使用。

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

回答by mochadwi

I almost gave up about this.

我几乎放弃了这件事。

This is my condition using glide to load image, see detailed glide issue here about rounded corner transformationsand here

这是我使用 glide 加载图像的情况,请在此处查看有关圆角转换的详细滑动问题此处

I've also the same attributes for my ImageView, for everyone answer here 1, here 2& here 3

我的 也有相同的属性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"

But still no success.

但仍然没有成功。

After researching for awhile, using a foregroundattributes from this SO answer heregive a result android:foreground="@drawable/all_round_border_white"

经过一段时间的研究,使用这个 SO 答案中foreground属性给出了一个结果android:foreground="@drawable/all_round_border_white"

unfortunately it giving me the "not nice" border corner like below image:

不幸的是,它给了我“不好”的边框角,如下图所示:

enter image description here

在此处输入图片说明