多个对角线背景颜色

时间:2017-02-15 06:58:03

标签: android android-drawable

我希望有一个背景可绘制如下图。 我应该如何创建它? enter image description here

2 个答案:

答案 0 :(得分:2)

您可以扩展目标Drawable类(可能是ColorDrawable)。然后在Path方法中绘制一个梯形draw(Canvas)。调整Paint的alpha和颜色以更改效果。代码可能如下:

public class SDrawable extends ColorDrawable {

    private Path mPath = new Path();
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public SDrawable() {
        init();
    }

    public SDrawable(int color) {
        super(color);
        init();
    }

    private void init(){
        mPaint.setColor(0x33ffffff);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mPath.reset();
        mPath.moveTo(0,0);
        mPath.lineTo(canvas.getWidth()/2+50, 0);
        mPath.lineTo(canvas.getWidth()/2, canvas.getHeight());
        mPath.lineTo(0, canvas.getHeight());
        mPath.lineTo(0, 0);
        canvas.drawPath(mPath, mPaint);
    }

}

答案 1 :(得分:0)

在布局背景下使用drawable XML下方,根据应用需要更改drawable XML下面的颜色。

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="@color/application_background"
                android:endColor="@color/bright_red"
                android:angle="45"/>
        </shape>
    </item>
</selector>

请参阅以下链接,了解如何更改和使用上述代码。

http://www.singhajit.com/gradient-color-in-android/