【问题标题】:Padding used in background drawable affects the ImageView's main drawable背景可绘制对象中使用的填充会影响 ImageView 的主要可绘制对象
【发布时间】:2015-01-05 22:15:14
【问题描述】:

我有一个可绘制的图层列表,通过对每个图层应用填充和描边来实现类似于下图的效果。

drawable的代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#99ff0000"
                android:width="0.5dp" />
            <corners
                android:radius="2dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#88ff0000"
                android:width="0.5dp" />
            <corners
                android:radius="1dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#77ff0000"
                android:width="0.5dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#66ff0000"
                android:width="0.5dp" />
        </shape>
    </item>

    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#55ff0000"
                android:width="0.5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#44ff0000"
                android:width="0.5dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#33ff0000"
                android:width="0.5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#22ff0000"
                android:width="0.5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#11ff0000"
                android:width="0.5dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="0.5dp"
                android:left="0.5dp"
                android:right="0.5dp"
                android:top="0.5dp" />
            <stroke android:color="#00ff0000"
                android:width="0.5dp"/>
        </shape>
    </item>
</layer-list>

但是用于创建效果的填充也会影响 ImageView 的主要可绘制对象,使图像比应有的要小。我尝试使用 LayerDrawable 代替,但填充仍然影响所有图层,它们位于可绘制对象之上。

将 android:cropToPadding 设置为 true/false 什么也没做。我将 android:scaleType 设置为 centerCrop。

如何防止背景可绘制对象的填充影响主图像可绘制对象?

这是一个视觉示例: 如果我这样写:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:src="@drawable/rectangle_green"/>

我得到下面的结果。我想禁用应用于绿色矩形的填充。

【问题讨论】:

  • 去掉最后一层的内边距?
  • 嗯,应用到主drawable的内边距大于0.5dp,但确实不需要最后一层的内边距。
  • 你能贴一张生效的填充截图,以及初始化ImageView的XML/代码吗?
  • 我找到了setPaddingMode,但它只被添加到了 Lollipop 中,并且不知何故真的弄乱了图像视图(边框很大)
  • 好的,也许您可​​以通过使用插入而不是填充来删除填充。或者,您可以使用没有填充的自定义 PNG 作为图层。或者您可以创建两个重叠的图像视图。

标签: android imageview android-drawable


【解决方案1】:

试试这个imageview

我认为你是这样做的..

android:background="@drawable/padding" // ur drawable xml code
android:src="@drawable/ic_launcher"  // ur image

这样做

android:background="@drawable/ic_launcher" // ur image
android:src="@drawable/padding"  // ur drawable xml code

编辑:

也试试这个

setImageResource(R.drawable.padding);  // ur drawable xml code

setBackgroundDrawable  // ur image

【讨论】:

  • 好吧,我实际上是使用 setImageDrawable 和 setBackgroundDrawable 动态设置可绘制对象,因为我使用的是列表视图,但这在这里并没有什么不同。如果我切换两个图像,drawable 会覆盖图像的边缘,我不希望这样。
  • 再次阅读我的评论。我不想切换两个可绘制对象,因为带有填充的可绘制对象会覆盖主要可绘制对象的某些部分。
  • 如果没有其他解决方案,我会使用这个解决方案
  • 我认为没有填充无法实现这一点...尝试使用嵌套矩形..希望有效...