Vidia
Vidia

Reputation: 481

How can I create an image of parallel lines with a gradient in Android?

I am trying to create the following design using only android drawables.

enter image description here

I imagine I would be able to loop the creation of lines then overlay a gradient layer of some sort.

How would I go about this kind of drawing efficiently?

Upvotes: 0

Views: 260

Answers (1)

pskink
pskink

Reputation: 24730

try this custom 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;
    }
}

where res/drawable/lines.png is as follows: enter image description here

Upvotes: 1

Related Questions