gpt4 book ai didi

android - 动态更改背景颜色 (Android)

转载 作者:太空狗 更新时间:2023-10-29 15:06:01 27 4
gpt4 key购买 nike

我有以下代码:

public class MainActivity extends Activity {

private BufferedReader br;
private Socket s;
private View v,v1;
private RelativeLayout rl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
s = new Socket("192.168.1.36",50000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){e.printStackTrace();}
color();
}


private void color(){

rl = (RelativeLayout) this.findViewById(R.id.rellay);
while(true){
try{

String received = br.readLine();
if(received != null){
// System.out.println(received);
String[] color = received.split(",");
setColor(color);

}
}catch(Exception e){e.printStackTrace();}
}
}
private void setColor(String[] color){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
int red = Integer.parseInt(color[0]);
int green = Integer.parseInt(color[1]);
int blue = Integer.parseInt(color[2]);
int a = Integer.parseInt(color[3]);
rl.setBackgroundColor(Color.argb(a, red, green, blue));

我想要做的是接收 4 个以逗号分隔的值(这可行),我希望 0-255 范围内的值是 RGB 颜色。

我想更改 android Activity 的背景。我可以从 onCreate 方法更改一次颜色,但是当我尝试进一步更改它时,我得到默认的白色屏幕。值永远不会超过 255。

如何做到这一点?谢谢!!

最佳答案

确保 alpha 值 != 0。alpha 值表示颜色的透明度,因此 0 是完全透明的。尝试从固定值开始,例如:

rl.setBackgroundColor(Color.argb(255, 0, 0, 0));

并确保它有效。之后,从服务器中提取您的值并将它们解析为一种颜色。

顺便说一句,听听 Nick Cardoso 的评论,感觉到网络延迟是一种糟糕的用户体验。

关于android - 动态更改背景颜色 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962099/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com