【问题标题】:Background color of a textView and seekBar value in androidandroid中textView和seekBar值的背景颜色
【发布时间】:2015-07-01 17:49:52
【问题描述】:

我正在做这个项目,在这个项目中我使用 seekBar 来改变图形的颜色。该图由在 xml 中设置特定背景颜色的 textView 表示。当我改变 seekBar 的值时,颜色应该会有所不同。

假设 textView 有颜色 #xxxxxx。我想得到这个颜色的值,并用 seekBar 的值按比例修改它。

例如,如果:

案例1:seekBar值增加0 -> max => #yyyyyy(seekBar的每个值不同的颜色)

案例2:seekBar值减小max-> 0 => #xxxxxx(同上)

我知道如何获取 textView 的颜色:

ColorDrawable cd = (ColorDrawable) textView.getBackground();
int colorCode = cd.getColor();`

以及如何使 seekBar 值的“增加”和“减少”值的逻辑。

我只是想不通如何使用“进度”和颜色代码一起从 x 到 y 以及从 y 到 x。

【问题讨论】:

    标签: android colors textview


    【解决方案1】:

    每一种颜色都是由红绿蓝组成 所以试试这个:

    public int getColor(int progress) {
        int startColor = Color.parseColor("#xxxxxx");
        int endColor = Color.parseColor("#yyyyyy");
    
        int start_red = Color.red(startColor);
        int start_green = Color.green(startColor);
        int start_blue = Color.blue(startColor);
        int end_red = Color.red(endColor);
        int end_green = Color.green(endColor);
        int end_blue = Color.blue(endColor);
    
        int progress_red = start_red + (end_red - start_red) * progress / 100;
        int progress_green = start_green + (end_green - start_green) * progress / 100;
        int progress_blue = start_blue + (end_blue - start_blue) * progress / 100;
    
        int progress_color = Color.rgb(progress_red, progress_green, progress_blue);
        return progress_color;
    }
    

    【讨论】:

    • 这看起来很有希望 :D 但有一件事,我不知道确切的 yyyyyy 值,但我不认为我会保持这个条件不变,我会分配一个整理颜色,就是这样.感谢您的帮助:)
    • 哦,顺便说一句,seekBar 的 maxValue 应该是多少?
    【解决方案2】:

    你必须写方法

    int toColor(int progress)
    

    它将进度值从 seekbar 的 onProgressChanged() 方法转换为 int 颜色,然后将其设置为 textview 的背景:

    textView.setBackgroundColor(convertedColor);
    

    【讨论】:

    • 并且我认为 seekBar 的最大值应该是 255 ?而且我仍然看不出旧的 bg 颜色和新的颜色之间有任何关系。像 newBgColor = progress + oldBgColor
    • 最好使用 seekbar 的 getMax() 方法,因为在创建 seekbar 时可以覆盖它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多