我正在尝试更改布局的背景颜色,但是取决于值。
例如。 100表示阅读,75:橙色60:淡黄色,20则类似蓝色。 另外,在每种颜色之间进行过渡。
我是Android开发的新手,我不确定要搜索的内容。如果您能为我指明正确的方向,课程,网站等等。
我正在尝试:
if(percentage <= 100 && percentage >= 85) {
// red
} else if(percentage < 85 && percentage >= 70) {
}
...
答案 0 :(得分:0)
尝试以下类似方法- 在布局中,将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);
答案 1 :(得分: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用于this answer)
答案 2 :(得分:0)
我最终还是用了其他东西
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));