我正在創建一個由畫布組成的畫布來繪製圓圈,畫布顏色將每5秒更換一次。這裏的代碼是我做的,但它沒有改變顏色,請告訴我我在哪裏做了這個錯誤如何在5秒內動態更改Android中畫布的顏色。
package com.developer.milandemoapp;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Automode_Activity extends Activity {
Thread timer;
boolean Count=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
getWidth();
getHeight();
final int radius = 25;
System.currentTimeMillis();
final Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#D50000"));
final Paint paint1 = new Paint();
paint1.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint1);
paint1.setColor(Color.parseColor("#33691E"));
//Count=0;
final Thread delay_thread= new Thread(new Runnable() {
@Override
public void run() {
while(true){
try{
Thread.sleep(5000);
change_colors();
}
catch(Exception e)
{
Log.e("Error:",e.getMessage());
}
}}
private void change_colors() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(Count)
{
paint1.setColor(Color.parseColor("#D50000"));
}
//paint1.setColor(Color.parseColor("#33691E"));
else
{
paint1.setColor(Color.parseColor("#33691E"));
}
Count=!Count;
canvas.drawCircle(300, 300, radius, paint1);
canvas.drawCircle(400, 300, radius, paint1);
canvas.drawCircle(500, 300, radius, paint1);
canvas.drawCircle(600, 300, radius, paint1);
canvas.drawCircle(700, 300, radius, paint1);
canvas.drawCircle(800, 300, radius, paint1);
canvas.drawCircle(900, 300, radius, paint1);
}
});
}
});
canvas.drawCircle(300, 300, radius, paint);
canvas.drawCircle(400, 300, radius, paint);
canvas.drawCircle(500, 300, radius, paint);
canvas.drawCircle(600, 300, radius, paint);
canvas.drawCircle(700, 300, radius, paint);
canvas.drawCircle(800, 300, radius, paint);
canvas.drawCircle(900, 300, radius, paint);
delay_thread.start();
}
}
}
你的顏色只改變一次? – Muthu 2015-04-03 06:37:14
這不會改變他只有一種顏色的傢伙@Muthu – 2015-04-03 06:38:43
你需要每5秒調用invalidate()才能使其工作!嘗試使用處理程序後推遲的方法來實現這個 – Muthu 2015-04-03 06:40:56