【问题标题】:Android - Set a border around an ImageViewAndroid - 在 ImageView 周围设置边框
【发布时间】:2012-09-16 08:13:19
【问题描述】:

我有一个固定宽度和高度的单元格,设置为 100x100px。在该单元格内,我想显示一个带有边框的ImageView
我的第一个想法是给ImageView放一个背景资源,并添加一个1dp的padding来创建边框效果:

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

显然这应该有效,但它没有,或者至少不像预期的那样。
问题是 ImageView 背景填充了整个 100x100px 单元格的空间,因此,如果图像的宽度小于 100px,则顶部和底部边框会显得更大。

注意黄色边框,我需要它在 ImageView 周围正好 1px:

非常感谢任何帮助、想法、建议。

【问题讨论】:

  • 把内边距放在LinearLayout上,而不是imageview上。将lineaylayout的背景设置为黄色
  • 这种情况下没有效果。
  • 您是否尝试将android:adjustViewBounds = "true" 添加到您的ImageView。如果你想让它居中,你还需要添加android:layoutGravity = "center"

标签: android android-layout android-emulator android-widget android-imageview


【解决方案1】:

如果您将 padding=1 和背景颜色放在 LinearLayout 中,您将有一个 1px 的黄色边框。

【讨论】:

  • 要将 padding=1 放在 LinearLayout 上?如果是这样,这将行不通。请注意,LinearLayout 的固定宽度为 100x100px。因此,将背景设置为它,将制作一个 100x100 的黄色背景,并且它不会坚持 ImageView 尺寸。或者,可能是我没有理解正确,如果能给出一行代码,我将不胜感激。
  • 我假设您的图像会填充线性布局。您还可以将 LinearLayout 设置为“wrap_content”,给它一个 1dp 的填充,然后放入图像。或者你给图像一个 1dp 的 layout_margin,在包裹它的黄色 LinearLayout 内。
  • 如果您想使用默认颜色保持填充并在图像周围添加边框怎么办。例如,在 GridView 中,您希望突出显示选择而不扭曲单元格之间的填充。
  • 简单优雅的答案
【解决方案2】:

这对我有用...

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

这是我使用带有边框的 viewpager 和 imageview 得到的结果。

【讨论】:

  • 我想念 android:cropToPadding="true"。谢谢现在为我工作。
【解决方案3】:

如果图像的大小是可变的,那么您总能得到这种效果。我想你需要为 ImageView 设置一个固定的大小并给它一个设置的背景颜色 - 从你的示例的外观来看黑色是有意义的。将 imageview 包裹在 FrameLayout 或仅带有黄色背景和 1px 填充的视图中。

编辑


我考虑过这个问题,但我的答案感觉不对,所以......

如果您将每个 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>

在屏幕截图中,两个显示的图像宽度都小于 100 像素,高度不同。

这不会处理具有透明背景的图像,因为(在这种情况下)黄绿色会显示出来。您可以通过将每个 ImageView 包装在 FrameLayout 中来解决此问题。将 ImageView 背景设置为黑色并将 FrameLayout 设置为 WrapContent 并使用所需的填充(我认为)

【讨论】:

    【解决方案4】:

    你可以使用自定义的imageview,从那里你可以得到边框,这里是代码。您还可以根据需要更改填充宽度和笔划宽度。它在第一行代码下方指定,谢谢

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

    【讨论】:

      【解决方案5】:

      这对我有用:

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

      【讨论】:

        【解决方案6】:

        只需将属性 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" />
        

        请注意android:adjustViewBounds="true" 仅适用于设置为“wrap_content”的android:layout_widthandroid:layout_height

        【讨论】:

          【解决方案7】:

          只需在您的 ImageView 布局中添加波纹管

          如果imageview的layout_width和layout_height不是match_parent比使用,

          android:adjustViewBounds = "true"
          

          android:adjustViewBounds = "true"
          android:scaleType="fitXY"
          

          android:adjustViewBounds = "true"
          android:scaleType="centerCrop"
          

          【讨论】:

            【解决方案8】:

            以下是我在最简单的解决方案中使用的代码 sn-p。

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

            如果您想进一步解释,请查看以下link,我已经解释得很好了。

            希望这对你有帮助!

            干杯!

            【讨论】:

              【解决方案9】:

              你必须在你的 imageview 中添加 scaletyle。之后,您的图像将适合。

              android:scaleType="fitXY"

              【讨论】:

              • scaletyle?有参考吗?
              猜你喜欢
              • 1970-01-01
              • 2015-06-19
              • 1970-01-01
              • 1970-01-01
              • 2020-05-30
              • 2013-02-04
              • 1970-01-01
              • 2015-06-26
              • 1970-01-01
              相关资源
              最近更新 更多