用特定颜色填充可绘制对象的透明部分

Fill the transparent part of the drawable with a specific color

我有这张透明图像,我想在透明部分内部填充特定颜色。

我已经能够用我想要的特定颜色填充此图像,但问题是颜色也填充了 ImageButton 的外部部分。

示例如下:

如何去除 drawable 外部多余的红色?

这是我现在的 xml:

<ImageButton
    android:id="@+id/button_scan"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dp"
    android:layout_weight="0.25"
    android:src="@drawable/ic_scan"
    android:background="@color/aub_red"
    android:backgroundTintMode="screen"
    android:textColor="@color/edittext_text" />

尝试通过添加论文来构建

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

如果您的图像比例受到上述更改的影响,您可以使用

android:scaleType="fitCenter"

看起来parent是一个LinearLayout加上一些'weightsum',这也是ImageButton宽度不确定的原因

您可以创建一个框架布局,其中将包含此图像按钮和图像按钮下方的另一个布局(与图像按钮具有相同的宽度和高度)。像这样:

    <FrameLayout
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="wrap_content">

    <View
        android:layout_width="match_parent"
        android:background="@color/aub_red"
        android:layout_height="match_parent"/>

    <ImageButton
        android:id="@+id/button_scan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="4dp"
        android:backgroundTintMode="screen" />
</FrameLayout>