2012-03-14 75 views
2

我正在試圖在上面有背景圖像的動態壁紙上繪製文本。我希望在透明畫布上繪製顏色,這樣它就不會掩蓋背景圖像,但由於某種原因,它不會將文本繪製到畫布上。Canvas.drawText()不能正常工作

關於我在做什麼的錯誤?

paintText = new Paint(); 
paintText.setColor(Color.WHITE); 
paintText.setStyle(Style.FILL); 
paintText.setAntiAlias(true); 
paintText.setTextSize(20); 

canvas.drawColor(Color.TRANSPARENT); 
canvas.drawText(String.valueOf(Points) + " Points", 50, 50, paintText); 
+0

嗯,怎麼樣Color.WHITE代替Color.TRANSPARENT? – Coffee 2012-03-14 22:07:43

+1

好吧,我嘗試了Color.WHITE,並將文本顏色更改爲Color.BLACK,但我得到的是一個沒有文本的完整白色屏幕。 – Gatekeeper 2012-03-14 22:10:48

+0

@Gatekeeper把'canvas.restore();'放在'drawText()'後面' – 2012-03-14 22:33:43

回答

2

我會說你不繪製文本之前需要

canvas.drawColor(Color.TRANSPARENT); 

。你沒有試過嗎?

+0

就這樣試了沒有,仍然沒有。如果我使用canvas.drawBitmap(bmp,0,0,paint);在這之前呢?我見過一些教程,指出您需要在添加文本之前在畫布上繪製純色,但它會掩蓋我的位圖 – Gatekeeper 2012-03-14 22:18:13

+0

您的畫布在圖像視圖中是正確的?你可以在drawtext方法後在imageview上調用.invalidate()嗎? – Vossi 2012-03-14 22:26:04

+0

我在一個動態壁紙,所以我的畫布是我相信的畫布。 canvas = new Canvas(); – Gatekeeper 2012-03-14 22:28:56

1

更新您的代碼:

paintText = new Paint(); 
paintText.setColor(Color.WHITE); 
paintText.setStyle(Style.FILL); 
paintText.setAntiAlias(true); 
paintText.setTextSize(20); 
canvas.save(); // 
canvas.drawColor(Color.TRANSPARENT); 
canvas.drawText(String.valueOf(Points) + " Points", 50, 50, paintText); 
canvas.restore(); //