Java 如何在300毫秒内更改背景
如何在300毫秒内更改背景? 用这个色码Java 如何在300毫秒内更改背景,java,android-studio,Java,Android Studio,如何在300毫秒内更改背景? 用这个色码 final static String[] codes = { "#F0F8FF", "#FAEBD7", "#00FFFF", "#7FFFD4", "#F0FFFF", "#F5F5DC", "#FFE4C4", "#000000", "#FFEBCD", "#0000FF", "#8A2BE2", "#A52A2A",
final static String[] codes = {
"#F0F8FF", "#FAEBD7", "#00FFFF",
"#7FFFD4", "#F0FFFF", "#F5F5DC", "#FFE4C4",
"#000000", "#FFEBCD", "#0000FF", "#8A2BE2",
"#A52A2A", "#DEB887", "#5F9EA0", "#7FFF00",
"#D2691E", "#FF7F50", "#6495ED", "#FFF8DC",
"#DC143C", "#00FFFF", "#00008B", "#008B8B",
"#B8860B", "#A9A9A9", "#A9A9A9", "#006400",
"#BDB76B", "#8B008B", "#556B2F", "#FF8C00",
"#9932CC", "#8B0000", "#E9967A", "#8FBC8F",
"#483D8B", "#2F4F4F", "#2F4F4F", "#00CED1",
"#9400D3", "#FF1493", "#00BFFF", "#696969",
"#696969", "#1E90FF", "#B22222", "#FFFAF0",
"#228B22", "#FF00FF", "#DCDCDC", "#F8F8FF",
"#FFD700", "#DAA520"};
将数组转换为int数组,并将#替换为
0xff
然后,您应该能够使用值动画师来更改颜色
final static int[] codes = new int[] {
0xfff0f8ff, 0xffFAEBD7, 0xff00ffff, 0xff7FFFD4,
0xffF0FFFF, 0xffF5F5DC, 0xffffe4c4,
0xff000000, 0xffffebcd, 0xff0000ff, 0xff8a2be2,
0xffA52A2A, 0xffDEB887, 0xff5f9ea0, 0xff7FFF00,
0xffD2691E, 0xffff7f50, 0xff6495ED, 0xfffff8dc,
0xffDC143C, 0xff00FFFF, 0xff00008b, 0xff008b8b,
0xffb8860b, 0xffa9a9a9, 0xffA9A9A9, 0xff006400,
0xffbdb76b, 0xff8b008b, 0xff556b2f, 0xffff8c00,
0xff9932cc, 0xff8B0000, 0xffE9967A, 0xff8FBC8F,
0xff483D8B, 0xff2F4F4F, 0xff2F4F4F, 0xff00CED1,
0xff9400D3, 0xffFF1493, 0xff00BFFF, 0xff696969,
0xff696969, 0xff1E90FF, 0xffB22222, 0xffFFFAF0,
0xff228B22, 0xffFF00FF, 0xffDCDCDC, 0xffF8F8FF,
0xffFFD700, 0xffDAA520 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout colourSurface = (RelativeLayout)findViewById(R.id.main_rl_root);
ValueAnimator colourAnimator = ValueAnimator.ofInt(codes);
colourAnimator.setTarget(colourSurface);
colourAnimator.setDuration(300); //Time in milli seconds from start to finish animation
colourAnimator.setRepeatCount(-1); //-1 = repeat forever
colourAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (colourSurface != null)
colourSurface.setBackgroundColor((int) animation.getAnimatedValue());
}
});
colourAnimator.start();
}
使用集合和时间任务 例子:
字符串随机颜色;
最终字符串[]代码={”#F0F8FF", "#FAEBD7", "#00FFFF", "#7FFFD4", "#F0FFFF", "#F5F5DC", "#FFE4C4",
"#000000", "#FFEBCD", "#0000FF", "#8A2BE2", "#A52A2A", "#DEB887", "#5F9EA0", "#7FFF00", "#D2691E",
"#FF7F50", "#6495ED", "#FFF8DC", "#DC143C“,”00FFFF“,”#00008B", "#008B8B", "#B8860B", "#A9A9A9",
“#A9A9”、“#006400”、”#BDB76B", "#8B008B", "#556B2F", "#FF8C00", "#9932CC“,”8B0000“,”E9967A“,”8FBC8F“,
“#483D8B”、“#2F4F4F”、“#2F4F4F”、“#00CED1”、“#9400D3”、“#FF1493”、“#00BFFF”、“#696969”、“#1E90FF”、“#B2222”,
“FFFAF0”、“228B22”、“FF00FF”、“DCDC”、“F8F8FF”、“FFD700”、“DAA520”};
//从数组中创建一个列表
最终列表l=新的ArrayList(Arrays.asList(codes));
收藏。洗牌(l);
randomColor=l.remove(0);
新计时器().scheduleAtFixedRate(新计时器任务()){
@凌驾
公开募捐{
Collections.shuffle(l);//您可以对列表进行洗牌
randomColor=l.remove(0);//在任意位置获取元素,在本例中为零
//你的逻辑在这里
}
},0,300);//在此处放置时间300毫秒=0,3秒
}
为此使用处理程序的postDelayed函数。它将在主UI线程上以指定的延迟运行代码,因此您将能够更新UI控件。
注意:onStop/onDestroy删除可运行
在color.xml中设置颜色
#609EEB#FFEBCD
#3F51B5
#FF4081
是否可以在UI线程外修改视图(此处,从
计时器
线程)?
String randomColor;
final String[] codes = { "#F0F8FF", "#FAEBD7", "#00FFFF", "#7FFFD4", "#F0FFFF", "#F5F5DC", "#FFE4C4",
"#000000", "#FFEBCD", "#0000FF", "#8A2BE2", "#A52A2A", "#DEB887", "#5F9EA0", "#7FFF00", "#D2691E",
"#FF7F50", "#6495ED", "#FFF8DC", "#DC143C", "#00FFFF", "#00008B", "#008B8B", "#B8860B", "#A9A9A9",
"#A9A9A9", "#006400", "#BDB76B", "#8B008B", "#556B2F", "#FF8C00", "#9932CC", "#8B0000", "#E9967A", "#8FBC8F",
"#483D8B", "#2F4F4F", "#2F4F4F", "#00CED1", "#9400D3", "#FF1493", "#00BFFF", "#696969", "#696969", "#1E90FF", "#B22222",
"#FFFAF0", "#228B22", "#FF00FF", "#DCDCDC", "#F8F8FF", "#FFD700", "#DAA520" };
// create a list from the array
final List<String> l = new ArrayList<String>(Arrays.asList(codes));
Collections.shuffle(l);
randomColor = l.remove(0);
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Collections.shuffle(l); //you shuffle the list
randomColor = l.remove(0); //get the element at an arbitrary position, zero in this case
// your logic here
}
}, 0, 300);//put here time 300 milliseconds=0,3 second
}
Handler handler;
// On create handler need to be set
handler=new Handler();
runnable.run();
@Override
protected void onStop() {
super.onStop();
handler.removeCallbacks(runnable);
}
private int getColorCode(){
int[] color = {
R.color._1, R.color._2, R.color._3,R.color._4
};
int min = 0,max = color.length;
Random r = new Random();
int i= r.nextInt(max - min);
return color[i];
}
private Runnable runnable = new Runnable()
{
public void run()
{
try {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lineLinearLayout);
linearLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),getColorCode()));
handler.postDelayed(runnable, 300);
}catch (Exception e){
e.printStackTrace();
}
}
};