2014-04-08 5 views
5

사용자 정의 진행률 막대를 작성하고 있습니다. 나는 검은 색 막대를 잘 진행하는 동안 "50 %"텍스트 색상 변경 동적으로 흰색에배경을 기준으로 페인트 색상을 반전합니다.

enter image description here

와 유사한 효과를 만들 싶습니다. "간단한"솔루션을 사용하여 가능합니까? 나는 PorterDuff, ColorFilters, xFermodes를 찾았지만 아무것도 작동하지 않는 것 같습니다. 어떤 아이디어? ATM 내 코드는 다음과 STH 보이는 '캔버스'아래에 그려 뭐죠에 따라 색상을 변경하는 pBlackTxtM 페인트를 수정하는 방법이

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

있습니까?

답변

3

질문이 꽤 오래된 경우에도이 해결책을 공유하고 싶습니다.

당신은 할 수 없어 사용이 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(); 
: 여기

는 생각을 간략하게 설명하는 몇 가지 코드
관련 문제