will
will

Reputation: 37

Android cannot draw transparent rectangle on canvas

TRY draw with transparent background and draw lines on it. However it draws a black rectangle instead of transparet. If I change the color to red, it works alright. So I guess it's the problem of transparent.

       drawPoints[currentpoint*4+0] = 0; 
       drawPoints[currentpoint*4+1] = 10;
       drawPoints[currentpoint*4+2] = 20; 
       drawPoints[currentpoint*4+3] = 40;

        Canvas mCanvas = sfh.lockCanvas(new Rect(10,20,50,100));
        mCanvas.drawColor(Color.TRANSPARENT);           
        mCanvas.drawLines(drawPoints,mPaint);
        sfh.unlockCanvasAndPost(mCanvas);

Why it draws black?

Upvotes: 1

Views: 5404

Answers (3)

Andres Cárdenas
Andres Cárdenas

Reputation: 738

I find this on the internet, I think that can works

Paint pincel1=new Paint();
paint.setARGB(255,255,0,0);

canvas.drawRect(10,10,ancho-10,40,pincel1);

pincel1.setStyle(Style.STROKE);
canvas.drawRect(10,60,ancho-10,90,pincel1);

pincel1.setStrokeWidth(3);
canvas.drawRect(10,110,ancho-10,140,pincel1);

image http://www.javaya.com.ar/androidya/imagentema/foto137.jpg

thx javaya.com/ar for the code

code

Upvotes: 0

CSAntol
CSAntol

Reputation: 74

You want to set the Paint object's color to Color.TRANSPARENT, rather than the canvas' color. So try

mPaint.setColor(Color.TRANSPARENT);

Upvotes: 0

0gravity
0gravity

Reputation: 2762

If you want transparency, you could try:

mPaint.setAlpha(100);

or some value between 0 and 255.

Hope that helps.

Upvotes: 7

Related Questions