2014-04-08 8 views
5

मैं एक कस्टम प्रगति पट्टी लिख रहा हूं। मैंपृष्ठभूमि पर आधारित पेंट रंग बदलें

enter image description here

जहां "50%" पाठ रंग में परिवर्तन गतिशील रूप से सफेद करने के लिए है, जबकि काले रंग की बार सही प्रगति करने के लिए एक प्रभाव समान बनाना चाहेंगे। क्या यह "सरल" समाधानों का उपयोग कर संभव है? मैंने पोर्टरडफ, कलरफिल्टर, एक्सफर्मोड्स को देखा, कुछ भी काम नहीं करता है। कोई विचार? एटीएम मेरे कोड इस तरह sth दिखता है:

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 रंग क्या 'कैनवास पर' यह नीचे खींचा के आधार पर रंग बदलने के लिए संशोधित करने के लिए कोई तरीका है?

उत्तर

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

यहाँ विचार की रूपरेखा तैयार करने के लिए कुछ कोड है

संबंधित मुद्दे