2016-05-16 80 views
1

enter image description here我可以在Android中部分更改此文本的顏色嗎?

我有一個自定義的車輪選取器視圖我正在如上所述。是否可以將藍色矩形突出顯示的文本着色爲白色,即使只是部分?所以如果文字上有50%,它會將其中一半改爲白色,一半改爲黑色?

編輯:

我用Java代碼(移植到C#)從這裏:https://code.google.com/archive/p/android-wheel/

基本上,藍色的矩形是一個xml定義的形狀,我放在代碼中的可滾動文本後面。

然後我繪製矩形,並在上面的項目,使用此代碼:

/** 
    * Draws items 
    * @param canvas the canvas for drawing 
    */ 
    private void drawItems(Canvas canvas) 
    { 
     canvas.Save(); 

     int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - this.Height)/2; 
     canvas.Translate(PADDING, -top + scrollingOffset); 

     itemsLayout.Draw(canvas); 

     canvas.Restore(); 
    } 

    /** 
    * Draws rect for current value 
    * @param canvas the canvas for drawing 
    */ 
    private void drawCenterRect(Canvas canvas) 
    { 
     int center = this.Height/2; 
     int offset = (int)(getItemHeight()/2 * 1.2); 
     centerDrawable.SetBounds(0, center - offset, this.Width, center + offset); 
     centerDrawable.Draw(canvas); 
    } 

從我所知道的,矩形本身不具有文本屬性設置像高亮或ColorPrimaryInverse性質。

編輯2:

從我可以告訴我需要使用弗拉基米爾代碼在被覆蓋的的onDraw爲每個彌補我的控制TextViews的。這是我到目前爲止:

protected override void OnDraw(Canvas canvas) 
    { 
     Rect rect = new Rect(); 
     this.GetDrawingRect(rect); 

     Paint mpaint = new Paint(); 
     mpaint.Color = Color.Black; 
     mpaint.SetStyle(Style.Fill); 

     canvas.Save(); 
     canvas.ClipRect(rect, Region.Op.Difference); 
     this.SetTextColor(Color.Black); 
     base.OnDraw(canvas); 
     canvas.Restore(); 


     mpaint.Color = Color.White; 
     canvas.Save(); 
     canvas.ClipRect(rect, Region.Op.Replace); // lets draw inside center rect only 
     this.SetTextColor(Color.White); 
     base.OnDraw(canvas); 
     canvas.Restore(); 
    } 

但這隻會將所有元素的文本顏色更改爲白色。我覺得我在這附近,任何幫助將不勝感激!

+0

你問是否可以給文本(如04:00)兩種顏色(白色和黑色)?我認爲只能用不同顏色對文本的部分/字符進行着色。您可以嘗試設置突出顯示的顏色,如[這裏建議](http://stackoverflow.com/questions/3451347/change-the-color-of-highlighted-text-in-android),看看整個文本是否改變,或只有突出部分。你也可以檢查[textColorPrimaryInverse](http://developer.android.com/reference/android/R.attr.html#textColorPrimaryInverse)。 – ishmaelMakitla

+0

是的,確切地說。我會研究這些屬性,但我不認爲我的代碼/ xml是以這種方式設置的,這使得可能。我想我會編輯我的問題,以顯示我迄今爲止所做的工作。 – Chucky

回答

1

有幾種方法可以做到這一點或類似的東西:

  1. 可以使用Canvas.clipRect方法,通過選擇矩形和渲染與其他彩色文本。所以,你的代碼將是這樣的:

    private draw(Canvas canvas) 
    { 
        RectF centerRect = new RectF(....); // change to your values 
    
        drawCenterRect(canvas); 
    
        canvas.save(); 
        canvas.clipRect(centerRect, Region.Op.DIFFERENCE); // lets draw everywhere except center rect 
        drawItems(canvas, Color.BLACK); // Pass color outside selection 
        canvas.restore(); 
    
        canvas.save(); 
        canvas.clipRect(centerRect, Region.Op.REPLACE); // lets draw inside center rect only 
        drawItems(canvas, Color.WHITE); // Pass color inside selection 
        canvas.restore(); 
    } 
    
  2. (高級)可以渲染新層/位圖的文字和比ColorFilter結合起來喜歡做對比。

+0

我喜歡這個答案,但是你能夠提供更多的信息相對於我上面的兩種方法嗎?我仍然不太關注圖形概念。 – Chucky

+0

謝謝Volodymyr!我幾乎在那裏,但試圖找出如何設置我的文字顏色?這一切都發生在我的適配器中一個名爲getItemText的方法中,該方法簡單地設置視圖的文本顏色。我還會改變這個屬性嗎? – Chucky