如何在Android中使用渐变创建平行线图像?

时间:2014-05-15 06:30:22

标签: android android-layout android-drawable

我试图仅使用android drawables创建以下设计。

enter image description here

我想我可以循环创建线然后覆盖某种渐变层。

我如何有效地进行这种绘图?

1 个答案:

答案 0 :(得分:1)

尝试这个自定义Drawable:

class D extends Drawable {
    private Paint mPaint;

    public D() {
        mPaint = new Paint();
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        Bitmap lines = BitmapFactory.decodeResource(getResources(), R.drawable.lines);
        Shader shaderA = new BitmapShader(lines, TileMode.REPEAT, TileMode.REPEAT);
        Shader shaderB = new LinearGradient(bounds.left, bounds.top, bounds.right, bounds.bottom, Color.BLUE, Color.RED, TileMode.REPEAT);
        ComposeShader cs = new ComposeShader(shaderA, shaderB, Mode.SRC_IN);
        mPaint.setShader(cs);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawPaint(mPaint);
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

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

其中res / drawable / lines.png如下:enter image description here