Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从多个线程在画布上绘制Android 17_Android_Multithreading_Android Canvas_Draw - Fatal编程技术网

从多个线程在画布上绘制Android 17

从多个线程在画布上绘制Android 17,android,multithreading,android-canvas,draw,Android,Multithreading,Android Canvas,Draw,在该应用程序的概述中,我从SurfaceView上的摄像头获得了提要,我想根据gps位置(我想听起来很熟悉)在上面绘制。 所以为了在不阻塞实时提要的情况下更新画布,我需要一个新线程? 代码: 其中重画是: public void ReDraw() { Log.v("MyActivity","INNER START REDRAW "); Paint p = new Paint(); p.setColor(Color.RED); canvas.drawColo

在该应用程序的概述中,我从SurfaceView上的摄像头获得了提要,我想根据gps位置(我想听起来很熟悉)在上面绘制。 所以为了在不阻塞实时提要的情况下更新画布,我需要一个新线程? 代码:

其中重画是:

    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的新类,然后创建该类,从主类传递画布并尝试重新绘制,但我再次看到日志,但画布上没有任何新的绘图


非常感谢

绘图和任何UI内容必须出现在主线程上。你应该做的是在一个单独的线程中运行GPS更新,然后使你的曲面无效,以便它从主线程重新绘制


编辑:多读一点(我玩GPS已经有一段时间了)。您定义了一个LocationListener,并且有一个“onLocationChanged”函数。当你移动时,这被称为。通过此函数,您需要使视图无效。

因此,新线程只需检查GPS更新并调用CanvaObject.invalidate()/postinvalidate()?是的视图抱歉,嗯,我回家后必须看一看,但谢谢!好吧@Goz GPSGo是一个完全不同的类,我不想包含更多与GPS无关的东西。我在drawing类的“listener”中所想的是做一个locationManager。getLastKnownLocation()@drakoumelitos:你可以这样做,但最终会浪费一些电池电量。这应该不会太糟糕,但允许内核在需要的时候唤醒您将为您提供最佳的电池性能。
    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 ");
}