Question

How to "set up" a paint to accomplish the "second" image above?

paint.setColor(Color.BLACK);
canvas.drawText(strValue, x, y, paint);

enter image description here

First Image: text all in black as result of that code above.

Second Image: better constrat to backgroud colors (edited with graphic editor just to illustrate here)

Note that "31" is partially black and partially white (but it could be any other color with a better contrast to red, as "36" could be to blue).

Was it helpful?

Solution

You could draw with PixelXorXfermode.

OTHER TIPS

the only solution I could think of is that first on your onDraw you have a variable Canvas that you equals to the actual one and then you draw your number,

paint.setColor(Color.BLACK);
canvas.drawText(strValue, x, y, paint);

then you draw the red Rect

canvas.drawRect(myRect, redPaint);

then you draw your line

canvas.drawline(mStartX,mStartY, mFinishX, mFinishY, myLinePaint);

and at the very end outside your onDraw, you call a method like this one:

public void myMethod(){
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    this.canvas.drawText(strValue, x, y, paint);
    //here you will define the area that you will mark as dirty 
    //(wich can have the same values as your red Rect)
    Rect myRect = new Rect();
    myRect.set(x0,y0,x1,y1);
    //and finally here you invalidate ONLY the red area
    this.canvas.invalidate(myRect);
}

Note: this will require that on your onDraw you verify that the global Canvas is not null and if so, then you equals your global to the actual. I'm not sure if this will actually work, however is the only solution I could think of doing so.

PixelXorXfermode is not good method when AntiAlias is set.

if you can get the red rectangle, I think use canvas.clipRect is better. like this

textpaint.setColor(black);
canvas.drawText(str,x,y,textpaint);

Rect oldClipRect = canvas.getClipBounds();
canvas.clipRect(rcRed,Op.REPLACE);
textpaint.setColor(white);
canvas.drawText(str,x,y,textpaint);
canvas.clipRect(oldclipRect,Op.REPLACE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top