使用值更改Android背景颜色

使用值更改Android背景颜色,android,Android,我试图更改布局的背景色,但这取决于一个值 比如说。100代表阅读,75代表橙色,60代表黄色,20代表蓝色。 另外,在每种颜色之间有一个过渡 我是Android开发新手,我不确定到底要搜索什么。如果你能给我指出正确的方向,课程,网站或其他什么 更新 我目前正在尝试: if(percentage <= 100 && percentage >= 85) { // red } else if(percentage < 85 && pe

我试图更改布局的背景色,但这取决于一个值

比如说。100代表阅读,75代表橙色,60代表黄色,20代表蓝色。 另外,在每种颜色之间有一个过渡

我是Android开发新手,我不确定到底要搜索什么。如果你能给我指出正确的方向,课程,网站或其他什么

更新 我目前正在尝试:

if(percentage <= 100 && percentage >= 85) {
        // red
 } else if(percentage < 85 && percentage >= 70) {

}
...
if(百分比=85){
//红色的
}否则如果(百分比<85&&percentage>=70){
}
...
试试下面的方法- 在布局中,为要更改其背景的视图指定id,例如

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#e5e5e5">

 <!-- Other Views -->
</LinearLayout>

然后在活动中-

LinearLayout ll = findViewById(R.id.linear);
if(percentage <= 100 && percentage >= 85) ll.setBackgroundColor(Color.RED);
else if(percentage < 85 && percentage >= 70) ll.setBackgroundColor(Color.BLUE);
LinearLayout ll=findviewbyd(R.id.linear);
如果(百分比=85)ll.setBackgroundColor(颜色为红色);
否则,如果(百分比<85&&percentage>=70)ll.setBackgroundColor(颜色为蓝色);

我制作了一个演示。假定

  • 金额从0到100
  • 0=rgb(0,0255)-蓝色
  • 50=rgb(255,255,0)-黄色
  • 100=rgb(255,0,0)-红色
这是一个每2秒更改颜色的示例活动:

class BackgroundActivity : Activity() {
    private lateinit var root: LinearLayout
    private val values = arrayOf(0F, 40F, 60F, 100F, 10F)
    private var counter = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_background)

        root = findViewById(R.id.background)

        val timer = Timer()
        val task = object : TimerTask() {
            override fun run() {
                changeBackground(values[counter])
                counter++
                if (counter >= values.size)
                    timer.cancel()
            }
        }
        timer.schedule(task, 0, 2000)
    }


    private fun changeBackground(amount: Float) {
        val r = Math.min(255*amount*2/100, 255F)
        val g = if (50 > amount) 255*amount*2/100 else 255*(100-amount)*2/100
        val b = Math.max(255*(1-amount*2/100), 0F)

        val color = getIntFromColor(r.toInt(), g.toInt(), b.toInt())

        runOnUiThread {
            root.setBackgroundColor(color)
        }
    }

    private fun getIntFromColor(r: Int, g: Int, b: Int): Int {
       val red = r shl 16 and 0x00FF0000 //Shift red 16-bits and mask out other stuff
        val green = g shl 8 and 0x0000FF00 //Shift Green 8-bits and mask out other stuff
        val blue = b and 0x000000FF //Mask out anything not blue.

        return -0x1000000 or red or green or blue //0xFF000000 for 100% Alpha. Bitwise OR everything together.
    }

}
重要的部分是
changeBackground()
方法-基于数量计算最终颜色(int)

我真的不确定这个过渡



(感谢@initramfs)

我最后使用了其他东西

boolean good = false;
double percentage = getPercentage((int) temp);
int screenHeight = getScreenHeight();
int pos1 = 0, pos2 = 0;

Integer[] firstColor = new Integer[]{0, 155, 255}, secondColor = new Integer[]{255, 152, 0};

if (percentage <= 0) {
    firstColor = new Integer[]{0, 155, 255};
    secondColor = new Integer[]{163, 235, 255};
    pos1 = 0;
    pos2 = 50;
    good = true;
}

if (percentage <= 50 && !good) {
    firstColor = new Integer[]{0, 155, 255};
    secondColor = new Integer[]{163, 235, 255};
    pos1 = 0;
    pos2 = 50;
    good = true;
}

if (percentage <= 100 && !good) {
    firstColor = new Integer[]{163, 235, 255};
    secondColor = new Integer[]{255, 152, 0};
    pos1 = 50;
    pos2 = 100;
}

int firstColor_X = screenHeight * (pos1 / 100);
int secondColor_X = (int) (screenHeight * ((double) pos2 / 100) - firstColor_X);
double slider_X = (screenHeight * (percentage / 100) - firstColor_X);
double ratio = slider_X / secondColor_X;

ArrayList<Integer> result = hexColor(secondColor, firstColor, ratio);

int red = result.get(0);
int green = result.get(1);
int blue = result.get(2);

background.setBackgroundColor(Color.rgb(red, green, blue));
boolean good=false;
双百分比=getPercentage((int)temp);
int screenHeight=getScreenHeight();
int pos1=0,pos2=0;
整数[]firstColor=新整数[]{0,155,255},secondColor=新整数[]{255,152,0};

如果(百分比请显示您已尝试的内容。我用示例更新了问题,但我确信一定有更好的方法来完成此操作并添加转换。因此,您有一个从0到100的值,并且希望根据该值更改颜色?您还提到了转换。值是否定期更改?@kristyna用户更改了该值