繁体   English   中英

如何创建由两种颜色并排组成的android drawable?

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

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

在此处输入图片说明

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

您可以尝试创建具有相应颜色的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)扩展drawable类

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

}

来源:手性代码 - StackOverflow

通过 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>
适合任何视图尺寸。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM