首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于背景的涂料颜色倒置

基于背景的涂料颜色倒置
EN

Stack Overflow用户
提问于 2014-04-08 16:36:26
回答 1查看 1.6K关注 0票数 7

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

其中,"50%“文本的颜色动态变化为白色,而黑条前进的权利。是否可以使用“简单”的解决方案?我查了一下PorterDuff,ColorFilters,xFermodes,似乎什么都没有用。有什么想法吗?自动提款机我的代码如下所示:

代码语言:javascript
复制
    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涂料的颜色?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-27 22:50:39

即使这个问题很老,我也想分享一下这个问题的解决方案。

您不能使用“反转”Paint来实现这一点,但是您可以使用裁剪来实现它。

其想法是两次绘制文本,一次是黑色,一次是白色,同时设置剪裁区域以匹配条形图的各个部分。

下面是一些代码来概述这个想法:

代码语言:javascript
复制
// 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();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22943091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档