我正在尝试制作一个颜色推子,它基本上是使用渐变更新布局背景,所以下面是我的主要活动
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Thread thread;
Button btnColor;
View layout;
Timer updateTimer;
int red,green,blue;
boolean flag=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnColor=(Button)findViewById(R.id.btnColor);
btnColor.setOnClickListener(this);
layout = findViewById(R.id.lOut);
updateTimer=new Timer();
red=green=blue=0;
}
@Override
protected void onStop()
{
super.onStop();
updateTimer.purge();
}
public void changeBackground()
{
int colors[]=new int[2];
if(red==255)
{
red=green=blue=0;
}
colors[0]=Color.rgb(red, green, blue);
colors[1]=Color.rgb(++red, ++green, ++blue);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,colors);
gd.setCornerRadius(0f);
layout.setBackgroundDrawable(gd);
}
private class UpdateTask extends TimerTask
{
Handler handler;
MainActivity ref;
public UpdateTask(Handler handler,MainActivity ref)
{
super();
this.handler=handler;
this.ref=ref;
}
@Override
public void run()
{
handler.post(new Runnable() {
@Override
public void run()
{
ref.changeBackground();
}
});
}
}
@Override
public void onClick(View v)
{
if(v.getId()==R.id.btnColor)
{
String text=btnColor.getText().toString();
if(text.equalsIgnoreCase("start"))
{
updateTimer.schedule(new UpdateTask(new Handler(), this), 200, 500);// =new UpdateTask(new Handler(),this);
btnColor.setText("stop");
}
else
{
btnColor.setText("start");
updateTimer.cancel();
updateTimer.purge();
}
}
}
}
问题是UI没有按照它应该进行更新,在调试代码时会到达后台更改的方法,但我看不到UI中的任何更改。
我缺少什么?
PS:我使用此video作为参考
LogCat日志:
02-15 05:14:37.421: ERROR/AndroidRuntime(419): java.lang.IllegalStateException: Timer was cancelled
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at java.util.Timer.scheduleImpl(Timer.java:566)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at java.util.Timer.schedule(Timer.java:485)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at com.android.gradient.MainActivity.onClick(MainActivity.java:91)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.view.View.performClick(View.java:2408)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.view.View$PerformClick.run(View.java:8816)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.os.Handler.handleCallback(Handler.java:587)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.os.Looper.loop(Looper.java:123)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at java.lang.reflect.Method.invoke(Method.java:521)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 05:14:37.421: ERROR/AndroidRuntime(419): at dalvik.system.NativeStart.main(Native Method)