嗨,我正在开发一个 android 应用程序,该应用程序涉及同时重绘画布和启动计时器。有人知道如何实现吗?
当调用 invalidate() 时,我试图在 View 类中调用 chronometer.start。然而,只有画布被重绘,而计时器根本没有启动。
编辑:这是我尝试过的代码:
public class ReDraw extends View{
public ReDraw(Context context){
super(context);
this.selfPointer = this;
setFocusable(true);
chrono(context);
}
public void chrono(Context context){
chrono = new Chronometer(context);
chrono.setOnChronometerTickListener(new OnChronometerTickListener(){
public void onChronometerTick(Chronometer arg){
elapsedTime = (SystemClock.elapsedRealtime() - arg.getBase()) / 1000;
long milliseconds= (long) (elapsedTime/60);
String millisec=Long.toString(milliseconds);
arg.setText(millisec);
}
});
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
}
protected void onDraw(Canvas Square)
{
super.onDraw(Square);
Paint squareColor = new Paint();
squareColor.setColor(Color.BLACK);
Square.drawRect(200,100,200,100, squareColor);
return;
}
}
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
invalidate();
}
return;
}
}