Android - 在 ImageView 周围设置边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12572312/
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
Android - Set a border around an ImageView
提问by Andy Res
I have a cell with a fixed width and height, let it be 100x100px. Inside that cell I want to display an ImageView
with a border around.
My first idea was to put a background resource to the ImageView
, and add a padding of 1dp to create the border effect:
我有一个固定宽度和高度的单元格,让它为 100x100px。在那个单元格内,我想显示一个ImageView
带有边框的 。
我的第一个想法是将背景资源放入ImageView
,并添加 1dp 的填充来创建边框效果:
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_border"
android:padding="1dp"
android:src="@drawable/test_image" />
</LinearLayout>
Apparently this should work, but it doesn't, or at least not as expected.
The problem is that the ImageView background fills the entire 100x100px cell's space, thus, if the image's width is less than 100px, then the top and bottom border will appear larger.
显然这应该有效,但它没有,或者至少不像预期的那样。
问题是 ImageView 背景填充了整个 100x100px 单元格的空间,因此,如果图像的宽度小于 100px,则顶部和底部边框会显得更大。
Notice the yellow border, I need it to be exactly 1px around the ImageView:
注意黄色边框,我需要它在 ImageView 周围正好是 1px:
Any help, idea, suggestion of any kind is much, much appreciated.
任何帮助、想法、任何形式的建议都非常感谢。
回答by Christine
If you put the padding=1 and the background color in the LinearLayout, you'll have a 1px yellow border.
如果你把 padding=1 和背景颜色放在 LinearLayout 中,你就会有一个 1px 的黄色边框。
回答by Ray Hunter
Here is what worked for me...
这是对我有用的...
<!-- @drawable/image_border -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"/>
<stroke android:width="1dp" android:color="#000000"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp"/>
</shape>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:padding="1dp"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:background="@drawable/image_border"/>
Here it the result that I get with an viewpager and imageview with a border.
这是我使用带边框的 viewpager 和 imageview 得到的结果。
回答by Paul D'Ambra
If the images are variable in size then you'll always get that effect. I guess you need to set a fixed size for the ImageView and give it a set background colour - from the look of your example black would make sense. Wrap the imageview in a FrameLayout or just a view with a yellow background and 1px padding.
如果图像大小可变,那么您将始终获得这种效果。我想你需要为 ImageView 设置一个固定的大小并给它一个设置的背景颜色 - 从你的示例黑色的外观来看是有道理的。将 imageview 包裹在 FrameLayout 或仅具有黄色背景和 1px 填充的视图中。
EDIT
编辑
I had a think about this and my answer didn't feel right so...
我想过这个问题,但我的答案感觉不对,所以......
If you set each ImageView with a fixed size, padding and margin. and then set the background colour as required you can get the effect you want.
如果您将每个 ImageView 设置为固定大小、填充和边距。然后根据需要设置背景颜色就可以得到想要的效果了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#52D017"
android:padding="1dp"
android:layout_margin="5dp"
android:src="@drawable/test1"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#52D017"
android:padding="1dp"
android:src="@drawable/test2"
tools:ignore="ContentDescription" />
</LinearLayout>
In the screenshot both displayed images are less than 100px in width and different heights.
在屏幕截图中,显示的图像宽度和高度都小于 100 像素。
This doesn't handle images with transparent backgrounds as then the (in this case) yellow green colour shows through. You could solve this by wrapping each ImageView in a FrameLayout. Making the ImageView background black and setting the FrameLayout to WrapContent with the required padding (I think)
这不会处理具有透明背景的图像,因为(在这种情况下)黄绿色会显示出来。您可以通过将每个 ImageView 包装在 FrameLayout 中来解决此问题。将 ImageView 背景设为黑色并将 FrameLayout 设置为 WrapContent 并使用所需的填充(我认为)
回答by Shubham Kamlapuri
you can use the custom imageview, from where you can get the border, here is the code. you can also change the width of padding and stroke width according to your need. It is specify just below the first line of code, thank you
您可以使用自定义图像视图,从那里可以获得边框,这是代码。您还可以根据需要更改填充宽度和笔划宽度。它在第一行代码的正下方指定,谢谢
public class FreeCollage extends ImageView {
private static final int PADDING = 8;
private static final float STROKE_WIDTH = 5.0f;
private Paint mBorderPaint;
public FreeCollage(Context context) {
this(context, null);
}
public FreeCollage(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setPadding(PADDING, PADDING, PADDING, PADDING);
}
public FreeCollage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initBorderPaint();
}
private void initBorderPaint() {
mBorderPaint = new Paint();
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setColor(Color.WHITE);
mBorderPaint.setStrokeWidth(STROKE_WIDTH);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(PADDING, PADDING, getWidth() - PADDING, getHeight() - PADDING, mBorderPaint);
}
}
回答by Goran Horia Mihail
This is what worked for me:
这对我有用:
<ImageView
android:id="@+id/dialpad_phone_country_flag"
android:layout_width="22dp"
android:layout_height="15dp"
android:scaleType="fitXY"
android:background="@color/gen_black"
android:padding="1px"/>
回答by rricarrdo
Just add the property adjustViewBounds to the ImageView.
只需将属性 adjustViewBounds 添加到 ImageView。
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_border"
android:padding="1dp"
android:adjustViewBounds="true"
android:src="@drawable/test_image" />
Kindly note that android:adjustViewBounds="true"
works only with android:layout_width
and android:layout_height
set to "wrap_content".
请注意,android:adjustViewBounds="true"
仅适用于android:layout_width
并android:layout_height
设置为“wrap_content”。
回答by varotariya vajsi
just add bellow line in your ImageView layout
只需在 ImageView 布局中添加波纹管
if layout_width and layout_height of imageview is not match_parent than use,
如果 imageview 的 layout_width 和 layout_height 不是 match_parent 则使用,
android:adjustViewBounds = "true"
or
或者
android:adjustViewBounds = "true"
android:scaleType="fitXY"
or
或者
android:adjustViewBounds = "true"
android:scaleType="centerCrop"
回答by Shubham
You have to add scaletyle in your imageview. After that your image would be fit.
您必须在 imageview 中添加 scalestyle。之后你的形象就会适合。
android:scaleType="fitXY"
android:scaleType="fitXY"
回答by Randika Vishman
Following is the code snippet which I used in my simplest solution.
以下是我在最简单的解决方案中使用的代码片段。
<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>
If you want further explanation please look at the following linkwhere I'd explained it well enough.
如果您需要进一步的解释,请查看以下链接,我已对其进行了充分的解释。
Hope this may be helpful to you anyone out there!
希望这对你那里的任何人都有帮助!
Cheers!
干杯!