【问题标题】:Border for an Image view in Android?Android中图像视图的边框?
【发布时间】:2011-03-16 20:53:44
【问题描述】:

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

【问题讨论】:

  • 我有一个 answer 用于更改 ImageView 的颜色,希望能有所帮助。

标签: android border imageview


【解决方案1】:

你可以在Android Studio中使用9个补丁来制作边框!

我正在寻找解决方案,但没有找到任何解决方案,因此我跳过了该部分。

然后我去了 Google 的 Firebase 资产图片,我无意中发现他们使用了 9patch。

这是链接:https://developer.android.com/studio/write/draw9patch

您只需拖动边缘所在的位置即可。

这就像 Unity 中的边界边缘。

【讨论】:

    【解决方案2】:

    将以下代码添加到形状中:

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

    ét voila,你有一个(或多或少)缩进的边框,光源设置为左上角。摆弄位图的大小(相对于图像视图的大小,我在示例中使用了 200dp x 200dp 的图像视图和 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>
    

    并像这样在您的图像视图中调用它:

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

    【讨论】:

      【解决方案3】:

      只需将此代码添加到您的 ImageView 中:

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval">
      
          <solid
              android:color="@color/white"/>
      
          <size
              android:width="20dp"
              android:height="20dp"/>
          <stroke
              android:width="4dp" android:color="@android:color/black"/>
          <padding android:left="1dp" android:top="1dp" android:right="1dp"
              android:bottom="1dp" />
      </shape>
      

      【讨论】:

        【解决方案4】:

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

        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:angle="270"
                android:endColor="#ffffff"
                android:startColor="#ffffff" />
        
            <corners android:radius="0dp" />
        
            <stroke
                android:width="0.7dp"
                android:color="#b4b4b4" />
        </shape>
        

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

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

        【讨论】:

        • 只是在此说明一下,如果图像是动态设置的,则需要在代码中再次设置边框,否则图像超出边框。
        • 我在使用 `ImageView.setImageBitmap(photoBmp)' 时没有看到任何奇怪的地方
        【解决方案5】:

        我几乎放弃了。

        这是我使用滑行加载图像的条件,见detailed glide issue here about rounded corner transformationshere

        我的ImageView 也有相同的属性,每个人都回答here 1here 2here 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"
        

        但还是没有成功。

        研究了一段时间后,使用this SO answer here中的foreground属性得到android:foreground="@drawable/all_round_border_white"的结果

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

        【讨论】:

          【解决方案6】:

          上面已经使用过,但没有专门提到。

          setCropToPadding(boolean);
          

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

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

          通过 XML 可以如下完成-

          android:cropToPadding="true"
          

          【讨论】:

            【解决方案7】:

            创建边框

            在您的可绘制文件夹中创建一个包含以下内容的 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 替换为您想要的任何内容或添加相应的尺寸/颜色。

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

            改变边框颜色

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

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

            ImageView img = findViewById(R.id.my_image_view);
            img.setBackgroundTintList(getColorStateList(R.color.newColor));
            

            别忘了定义你的newColor

            【讨论】:

            • 描边看起来不错 - 但是,框架和图像之间存在透明间隙(顶部和底部)。
            【解决方案8】:

            首先添加您想要作为边框颜色的背景颜色,然后

            将cropToPadding更改为true,然后添加填充。

            然后你将拥有你的 imageView 的边框。

            【讨论】:

              【解决方案9】:

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

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

              并在您的布局中使用。

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

              【讨论】:

                【解决方案10】:

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

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

                【讨论】:

                • 是的.. 它看起来像一个边框。但是,当您使用具有透明背景的可绘制图像时,它将无法正确显示带边框的图像。只要你有透明的,它就会显示黑色。所以你的答案不是最好的方法。
                • 是的!但是你有一个边框没有创建另一个 xml 文件。
                • 当您使用android:scaleType="centerCrop"调整图像大小时,这不起作用。
                • 这很糟糕,因为它会造成不必要的透支
                • @Silox for scaleType="centerCrop",确保同时添加cropToPadding="true"
                【解决方案11】:

                我将以下 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>
                

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

                【讨论】:

                • 然后将android:background="@drawable/yourXmlFileName"添加到ImageView
                • 边框在左右两边都有效,但对于顶部和底部,它会将父级填充到顶部。
                • 哦,太好了,这也是我想要的。请记住为您的 ImageView 设置填充。
                • @Maurice 您可以将cropToPadding 设置为true。
                • 别忘了设置cropToPadding="true"
                【解决方案12】:

                添加像 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>
                

                更新 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" />
                ...
                

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

                【讨论】:

                  【解决方案13】:

                  在我接下来使用的同一个 xml 中:

                      <RelativeLayout
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:background="#ffffff" <!-- border color -->
                          android:padding="3dp"> <!-- border width -->
                  
                          <ImageView
                              android:layout_width="160dp"
                              android:layout_height="120dp"
                              android:layout_centerHorizontal="true"
                              android:layout_centerVertical="true"
                              android:scaleType="centerCrop" />
                      </RelativeLayout>
                  

                  【讨论】:

                    【解决方案14】:

                    以下是我解决这个冗长问题的最简单方法。

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

                    下面的answer我已经解释的很好了,你也看看吧!

                    我希望这对其他人有帮助!

                    【讨论】:

                      【解决方案15】:

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

                      【讨论】:

                        【解决方案16】:

                        我发现这样做要容易得多:

                        1) 编辑框架以包含内容(使用 9patch 工具)。

                        2) 将ImageView 放在Linearlayout 中,并将您想要的框架背景或颜色设置为Linearlayout 的背景。当您将框架设置为在其内部包含内容时,您的 ImageView 将位于框架内(就在您使用 9patch 工具设置内容的位置)。

                        【讨论】:

                          【解决方案17】:

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

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

                          【讨论】:

                          • 我也喜欢这个,但是如果你把外边做得太小,边角就会是空白的。它适用于 2dp 及以上的任何东西。
                          • 这种方法的好处是它允许圆角。如果您不希望上一个答案中的边框重叠,请在 ImageView 标记中添加填充。这种方法的缺点是如果使用半透明背景,边框会渗入图像区域。所以我选择了之前的方法,因为比起圆角,我更喜欢半透明的背景。
                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2021-03-12
                          • 2015-01-29
                          • 2014-08-03
                          相关资源
                          最近更新 更多