如何创建由两种颜色并排组成的 android 可绘制对象?

How to create android drawable consisting of two colors side by side?

Uisng XML 是否可以创建一个 drawable,其中一半是 color1,另一半是 color2?当我将该可绘制对象设置为视图的背景时,它应该如下图所示。

您可以尝试创建 2 个具有各自颜色的形状,然后使用它们。

您可以通过为它们写上 xml 并给出尺寸和颜色来制作这些形状。

有可能。你可以:

1) 创建形状

<shape xmlns:android="http://schemas.android.com/apk/res/android"

  android:shape="rectangle" >

  <stroke
    android:width="1dp"
    android:color="@color/gray_light" />

  <gradient
    android:type="linear"
    android:centerX="0"
    android:centerY="1"
    android:startColor="#bff54a"
    android:endColor="#88c010" />

  <corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

</shape>

2) 扩展可绘制对象class

public class ColorBarDrawable extends Drawable {

    private int[] themeColors;

    public ColorBarDrawable(int[] themeColors) {
        this.themeColors = themeColors;
    }

    @Override
    public void draw(Canvas canvas) {

        // get drawable dimensions
        Rect bounds = getBounds();

        int width = bounds.right - bounds.left;
        int height = bounds.bottom - bounds.top;

        // draw background gradient
        Paint backgroundPaint = new Paint();
        int barWidth = width / themeColors.length;
        int barWidthRemainder = width % themeColors.length;
        for (int i = 0; i < themeColors.length; i++) {
            backgroundPaint.setColor(themeColors[i]);
            canvas.drawRect(i * barWidth, 0, (i + 1) * barWidth, height, backgroundPaint);
        }

        // draw remainder, if exists
        if (barWidthRemainder > 0) {
            canvas.drawRect(themeColors.length * barWidth, 0, themeColors.length * barWidth + barWidthRemainder, height, backgroundPaint);
        }

    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }

}

来源:手性代码 - Whosebug

由xml完成:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="100dp">
    <shape android:shape="rectangle">
        <size android:height="100dp" android:width="100dp"/>
        <solid android:color="@android:color/black"/>
    </shape>
</item>

<item android:left="100dp">
    <shape android:shape="rectangle">
        <size android:height="100dp" android:width="100dp"/>
        <solid android:color="@android:color/holo_green_light"/>
    </shape>
</item>
</layer-list>

将其放入 res/drawable 文件夹并指定为 android:background 图像

来自 xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90">
<shape>
    <gradient
        android:centerX="-10.0"
        android:endColor="@android:color/holo_blue_dark"
        android:startColor="@android:color/holo_green_light"
        android:type="sweep" />
</shape>
适合任何视图大小。

您也可以使用 9-Patch 文件:

https://developer.android.com/studio/write/draw9patch