0
在應用程序的概述中,我從SurfaceView上的攝像頭獲取數據源,並且我想根據gps位置(聽起來很熟悉,我猜)繪製它。 因此,爲了更新畫布而不阻塞實時饋送,我需要一個新的線程? 代碼:從多個線程在畫布上繪製Android 17
@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,然後創建該類從主類傳遞畫布並嘗試重繪,但再次我看到日誌,但沒有任何新的畫布。
非常感謝!
因此,新線程將檢查GPS更新並調用canvasobject.invalidate()/ postinvalidate()? – Drakoumel
View.invalidate,但是。 – Goz
對不起,嗯,我不得不看看,當我回家,但謝謝! – Drakoumel