如何创建由两种颜色组成的Android Drawable,这两种颜色并排显示?

13
使用 XML,可以创建一个可绘制对象,其中一半为 color1,另一半为 color2。将该 drawable 设置为视图的背景时,应该像下面的图片一样显示。

enter image description here

4个回答

19

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


谢谢。这个解决方案最接近我想要实现的目标。 - Egis
如果您将此可绘制对象设置为布局的背景,则它将是静态的(高度为200dp)。 - G. Ciardini

4

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


不要忘记在结尾处添加</rotate>。 - A.I.Shakil

0

你可以尝试创建两个具有不同颜色的形状,然后使用它们。

你可以通过编写 XML 代码为它们指定尺寸和颜色来创建这些形状。


0

这是可能的。你可以:

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) 扩展可绘制类

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

}

来源:Chiral Code - StackOverflow


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接