【问题标题】:Android: DrawText with background contrastAndroid:具有背景对比度的 DrawText
【发布时间】:2012-04-24 22:21:09
【问题描述】:

如何“设置”一个绘画来完成上面的“第二个”图像?

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

第一张图片:由于上面的代码,文本全部为黑色。

第二张图片:更好地对比背景颜色(使用图形编辑器进行编辑,仅在此处进行说明)

请注意,“31”部分是黑色,部分是白色(但它可以是与红色对比更好的任何其他颜色,因为“36”可能是蓝色)。

【问题讨论】:

  • 据我所知,没有办法做到这一点,因为您不能只将样式应用于一半数字,我建议您使用与红色矩形、蓝色圆圈和画布的背景色。

标签: android graphics canvas drawtext contrast


【解决方案1】:

你可以用PixelXorXfermode画画。

【讨论】:

  • 已解决:paint.setXfermode(new PixelXorXfermode(Color.WHITE));
【解决方案2】:

我能想到的唯一解决方案是,首先在你的 onDraw 上你有一个变量 Canvas,你等于实际的,然后你画你的数字,

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

然后你画红色的矩形

canvas.drawRect(myRect, redPaint);

然后你画你的线

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

在你的 onDraw 之外的最后,你调用一个像这样的方法:

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);
}

注意:这将要求您在 onDraw 上验证全局 Canvas 不为空 如果是这样,那么你等于你的全局到实际。 我不确定这是否真的有效,但这是我能想到的唯一解决方案。

【讨论】:

    【解决方案3】:

    设置 AntiAlias 时,PixelXorXfermode 不是好方法。

    如果你能得到红色矩形,我认为使用 canvas.clipRect 更好。像这样

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多