TextView作为带有textcolor minipulation的进度条?

Mr.*_*.Me 4 user-interface animation android android-layout android-progressbar

我正在努力改进我的应用程序的用户界面.在我正在使用的设计中,我有一个TextView,它将在某个时间充当进度条.ruslt应如下所示:

在此输入图像描述

问题是文本的一部分会在进度发生变化时改变颜色

我在android中查看了spannablestring,如果我能找到进度所涵盖的字母,它会起作用(但我不认为这很容易/准确)

我现在计划的是使用以下内容:

  • FrameLayout:
  • 背景textView与绿色文本和白色背景.
  • 前面的Linearlayout,在进行中改变宽度.
  • TextView与绿色背景和匹配framelayout宽度的白色文本.

有更好的方法吗?

Zie*_*ony 8

更好的方法需要您覆盖TextView类.您可以使用剪切,拆分TextView并以不同颜色绘制两个部分.

TextView tv = new TextView(this){
    @Override
    public void draw(Canvas canvas) {
        int color1 = Color.WHITE;
        int color2 = Color.GREEN;

        canvas.save();
        setTextColor(color1);
        setBackgroundColor(color2);
        canvas.clipRect(new Rect(0, 0, (int)(getWidth() * percent), getHeight()));
        super.draw(canvas);
        canvas.restore();

        canvas.save();
        setTextColor(color2);
        setBackgroundColor(color1);
        canvas.clipRect(new Rect((int)(getWidth() * percent), 0, getWidth(), getHeight()));
        super.draw(canvas);
        canvas.restore();
    }  
};
Run Code Online (Sandbox Code Playgroud)

我希望你明白这一点