I have the following code:
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活动的背景。我可以从onCreate()方法中更改一次颜色,但当我尝试再次更改时,会出现默认的白屏。数值永远不会超过255。
怎么才能做到呢?谢谢!
setColor
函数中加入Log.d("tag", color.toString())
并检查输入参数。Alpha 值必须大于0
才能显示,alpha == opacity != transparency
。 - frogatto