在应用程序的概述中,我从 SurfaceView 上的摄像头获取提要,我想根据 gps 位置在此基础上进行绘制(我猜听起来很熟悉)。因此,为了在不阻塞实时提要的情况下更新画布,我需要一个新线程吗?代码:
@Override
public void draw(Canvas canvas1) {
Log.w("MyActivity","DRAW");
canvas = canvas1;
super.draw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2,30,p);
String output = "";
output = "Current longitude:" + Double.toString(gpsGo.RequestLocationUpdate().getLongitude()) + " latitude: " + Double.toString(gpsGo.RequestLocationUpdate().getLatitude());
canvas.drawText(output,canvas.getWidth()/(5 - rand.nextInt(5)) + min,canvas.getHeight() - canvas.getHeight()/(5 - rand.nextInt(5)) + min,p);
Thread thread = new Thread()
{
@Override
public void run() {
Looper.prepare();
mHandler = new Handler();
r = new Runnable()
{
public void run()
{
Log.v(TAG, "PRE REDRAW ");
ReDraw();
Log.v(TAG,"AFTER REDRAW ");
mHandler.postDelayed(this, 10000);
}
};
Looper.loop();
}
};
thread.start();
}
重绘在哪里:
public void ReDraw()
{
Log.v("MyActivity","INNER START REDRAW ");
Paint p = new Paint();
p.setColor(Color.RED);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawCircle(canvas.getWidth()/4,canvas.getHeight()/5,60,p);
String output = "";
output = "Current longitude:" + Double.toString(gpsGo.RequestLocationUpdate().getLongitude()) + " latitude: " + Double.toString(gpsGo.RequestLocationUpdate().getLatitude());
canvas.drawText(output,canvas.getWidth()/2,canvas.getHeight() - canvas.getHeight()/3,p);
Log.v("MyActivity","INNER END REDRAW ");
}
在日志中,我看到了日志,但屏幕上没有任何新内容。我知道这不是处理新线程的最佳方式,而只是试图找出它为什么不起作用。
我尝试过的另一种方法是使 Thread thread = new Thread() {...} 成为一个扩展 Thread 的新类,然后创建该类从主类传递画布并尝试重绘,但我再次看到日志但画布上没有任何新的图纸。
非常感谢!