我正在写一个定制的进度条。我想创造一个类似于

其中,"50%“文本的颜色动态变化为白色,而黑条前进的权利。是否可以使用“简单”的解决方案?我查了一下PorterDuff,ColorFilters,xFermodes,似乎什么都没有用。有什么想法吗?自动提款机我的代码如下所示:
Rect r = new Rect(1, 1, m_width-1, m_height-1);
canvas.drawRect(r, pWhiteFill);
r = new Rect(1, 1, progressWidth, m_height-1);
canvas.drawRect(r, pBlackFill);
canvas.drawText(String.valueOf(progress)+"%", m_width/2, m_height/2, pBlackTxtM);有没有一种方法可以根据在画布下面画的颜色来修改pBlackTxtM涂料的颜色?
发布于 2015-12-27 22:50:39
即使这个问题很老,我也想分享一下这个问题的解决方案。
您不能使用“反转”Paint来实现这一点,但是您可以使用裁剪来实现它。
其想法是两次绘制文本,一次是黑色,一次是白色,同时设置剪裁区域以匹配条形图的各个部分。
下面是一些代码来概述这个想法:
// store the state of the canvas, so we can restore any previous clipping
canvas.save();
// note that it's a bad idea to create the Rect during the drawing operation, better do that only once in advance
// also note that it might be sufficient and faster to draw only the white part of the bar
Rect r = new Rect(1, 1, m_width-1, m_height-1);
canvas.drawRect(r, pWhiteFill);
// this Rect should be created when the progress is set, not on every drawing operation
Rect r_black = new Rect(1, 1, progressWidth, m_height-1);
canvas.drawRect(r_black, pBlackFill);
// set the clipping region to the black part of the bar and draw the text using white ink
String text = String.valueOf(progress)+"%";
canvas.cliprect(r_black);
canvas.drawText(text, m_width/2, m_height/2, pWhiteTxtM);
// draw the same text again using black ink, setting the clipping region to the complementary part of the bar
canvas.clipRect(r, Region.Op.XOR);
canvas.drawText(text, m_width/2, m_height/2, pBlackTxtM);
canvas.restore();https://stackoverflow.com/questions/22943091
复制相似问题