【问题标题】:Can I partially change the color of this text in Android?我可以在 Android 中部分更改此文本的颜色吗?
【发布时间】:2016-05-16 09:38:40
【问题描述】:

我有一个如上所述的自定义轮子选择器视图。是否可以将蓝色矩形突出显示的文本着色为白色,即使只是部分?所以如果它在文本上占 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 中为构成我的控件的每个 TextView 使用 Volodymyr 的代码。这是我目前所拥有的:

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

但这只会将所有元素的文本颜色更改为白色。我觉得我在这里很近,任何帮助将不胜感激!

【问题讨论】:

  • 您是否在问是否可以给文本(如 04:00)提供两种颜色(白色和黑色)?我认为只能用不同的颜色为文本的部分/字符着色。您可以尝试设置像suggested here 这样的突出显示颜色,看看是整个文本改变还是只有突出显示的部分。您也可以查看textColorPrimaryInverse
  • 是的,没错。我将研究这些属性,但我不认为我的代码/xml 设置的方式使这成为可能。我想我会编辑我的问题以展示我到目前为止所做的事情。

标签: java c# android xml xamarin.android


【解决方案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 相结合以形成对比。

【讨论】:

  • 我喜欢这个答案,但您能否提供与我上述两种方法相关的更多信息?我仍然没有完全遵循图形概念。
  • 感谢沃洛迪米尔!我快到了,但想弄清楚如何设置我的文本颜色?这一切都发生在我的适配器中的一个名为 getItemText 的方法中,该方法只是设置视图的文本颜色。我还要更改此属性吗?
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 2021-09-15
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多