簡體   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