连续改变布局的背景 [英] Change Layout background continuously
问题描述
我正在在我改变了布局背景不断翻转一样的背景,我在执行使用 Activity.runOnUiThread()
函数,因为背景变化的应用程序它是一个用户界面功能以及使用等待2秒钟线程()。睡眠()
,但应用程序只显示我在末尾提到的布局颜色。
包com.tutorial.flasher;进口android.app.Activity;
进口android.graphics.Color;
进口android.os.Bundle;
//进口android.os.SystemClock;
//进口android.os.SystemClock;
进口android.widget.LinearLayout;
公共类闪光延伸活动{
/ **当第一次创建活动调用。 * /
的LinearLayout llaLayout;
日=新主题(ThreadOne)螺纹;
活动_activity =新活动();
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
llaLayout =(的LinearLayout)findViewById(R.id.layo); 螺纹T1 =新主题(新的Runnable(){ 公共无效的run(){
尝试
{
视频下载(2000);
}
赶上(例外五){
// TODO:处理异常
}
}
}); t1.start();
t1.run();
t1.stop();
_activity.runOnUiThread(新的Runnable(){
公共无效的run(){
llaLayout.setBackgroundColor(Color.parseColor(#00FF00)); }
});
//t1.currentThread();
t1.start();
t1.run();
t1.stop(); _activity.runOnUiThread(新的Runnable(){
公共无效的run(){
布局//颜色蓝色
llaLayout.setBackgroundColor(Color.parseColor(#0000FF)); }
});
}
}
两者用户界面的变化和螺纹凝视将在一个循环中发生(未示出),但仍然在应用程序改变布局颜色只有一次。
谢谢,
希德
- 您不应该
新
活动,活动由Android系统的创建。 - 呼叫
Thread.start
将启动线程,没有必要调用Thread.run
。
Activity.runOnUiThread
也许不是存档最好的方法,尝试处理程序
。下面是示例code:
公共类BroadcastActivity延伸活动{ 公共静态最终诠释CHANGE_BGCOLOR = 1; 私人的LinearLayout llaLayout; 私人处理程序处理程序=新的处理程序(){
公共无效的handleMessage(android.os.Message MSG){
如果(msg.what == 1){
串色=(字符串)msg.obj;
llaLayout.setBackgroundColor(Color.parseColor(彩色));
串nextColor =; //接下来背景颜色;
消息m = obtainMessage(CHANGE_BGCOLOR,nextColor);
sendMessageDelayed(M,200);
}
}
}; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
llaLayout =(的LinearLayout)findViewById(R.id.layo);
串nextColor =; //接下来背景颜色;
消息m = handler.obtainMessage(CHANGE_BGCOLOR,nextColor);
handler.sendMessageDelayed(M,200);
}
}
I am making an application in which i am changing the the layout background continually like flipping the background, I am implementing the background change using Activity.runOnUiThread()
function since it is a UI function and waiting for 2 seconds using Thread().sleep()
but the application only shows the layout color I mentioned in the end.
package com.tutorial.flasher;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
//import android.os.SystemClock;
//import android.os.SystemClock;
import android.widget.LinearLayout;
public class flasher extends Activity {
/** Called when the activity is first created. */
LinearLayout llaLayout;
Thread th = new Thread("ThreadOne");
Activity _activity = new Activity();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
llaLayout = (LinearLayout)findViewById(R.id.layo);
Thread t1 = new Thread(new Runnable() {
public void run() {
try
{
Thread.sleep(2000);
}
catch (Exception e) {
// TODO: handle exception
}
}
});
t1.start();
t1.run();
t1.stop();
_activity.runOnUiThread(new Runnable(){
public void run(){
llaLayout.setBackgroundColor(Color.parseColor("#00FF00"));
}
});
//t1.currentThread();
t1.start();
t1.run();
t1.stop();
_activity.runOnUiThread(new Runnable(){
public void run(){
//Color BLUE of layout
llaLayout.setBackgroundColor(Color.parseColor("#0000FF"));
}
});
}
}
Both the UI changes and Thread staring would be happening in a loop(which is not shown) but still the application is changing the layout color only once. Thanks, Sid
- You should not
new
Activity, Activity is created by Android system. - Call
Thread.start
will start the thread, there is no need to callThread.run
.
Activity.runOnUiThread
maybe not the best way to archive this, try Handler
. Here is sample code:
public class BroadcastActivity extends Activity {
public static final int CHANGE_BGCOLOR = 1;
private LinearLayout llaLayout;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
String color = (String) msg.obj;
llaLayout.setBackgroundColor(Color.parseColor(color));
String nextColor = ""; // Next background color;
Message m = obtainMessage(CHANGE_BGCOLOR, nextColor);
sendMessageDelayed(m, 200);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
llaLayout = (LinearLayout)findViewById(R.id.layo);
String nextColor = ""; // Next background color;
Message m = handler.obtainMessage(CHANGE_BGCOLOR, nextColor);
handler.sendMessageDelayed(m, 200);
}
}
这篇关于连续改变布局的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!