Android 设置背景setBackgroundColor问题~求解答
我想做一个APP,让背景慢慢改变。想法就是#AARRGGBB让其自加,但是背景总是只在白色到蓝色之间循环,蓝色之后直接白色。求大神指导~~~代码如下:packagecom...
我想做一个APP,让背景慢慢改变。
想法就是#AARRGGBB 让其自加,但是背景总是只在白色到蓝色之间循环,蓝色之后直接白色。求大神指导~~~
代码如下:
package com.example.raw;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public static int color = 0;
private LinearLayout ll;
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.root);
ll.setBackgroundColor(0XFF00FF00);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
color++;
System.out.println(color);
ll.setBackgroundColor(0x7f000000+color);
break;
default:
break;
}
super.handleMessage(msg);
}
};
task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Timer timer = new Timer(true);
timer.schedule(task, 1000, 10);
}
} 展开
想法就是#AARRGGBB 让其自加,但是背景总是只在白色到蓝色之间循环,蓝色之后直接白色。求大神指导~~~
代码如下:
package com.example.raw;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public static int color = 0;
private LinearLayout ll;
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.root);
ll.setBackgroundColor(0XFF00FF00);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
color++;
System.out.println(color);
ll.setBackgroundColor(0x7f000000+color);
break;
default:
break;
}
super.handleMessage(msg);
}
};
task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Timer timer = new Timer(true);
timer.schedule(task, 1000, 10);
}
} 展开
1个回答
展开全部
代码本身没有问题,只是你设置的颜色值变化过程与你预期的结果不相符。先不管透明度,这段代码的颜色值RGB变化过程是000000->0000FF->000100->0001FF->000200->0002FF->000300……依次变化,前面一段时间内的变化自然就是黑->蓝->黑->蓝->黑->蓝->黑,只不过黑色每次变化(000000->000100->000200->000300)不大,而你设置了透明度,可能看起来像白色。要想实现你预期的效果,你必须明确你的背景色如何变化,每次变化是什么颜色
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询