0

我覆盖了 MyLocationOverlay 的 drawMyLocation 以显示一个箭头作为标记,而不是普通的蓝点。但是,我还需要根据手机的传感器更改指针指向的方向。我决定在 onSensorChanged 中调用 drawMyLocation。但是调用 drawMyLocation 所需的参数之一是 Canvas。如何访问 MyLocationOverlay 的画布?还是每次我在 onSensorChanged 中调用 drawMyLocation 时都需要创建一个新的 Canvas 并传递新的 Canvas?

下面是我尝试覆盖 draw 方法时的代码。但是,即使我可以看到它一直在执行,旋转的位图也需要一段时间才能显示。

**更新:如果我尝试触摸我的地图,我可以看到我的箭头在触摸地图时旋转。但是,如果我不触摸它,箭头需要一段时间才能更新其方向。

public boolean draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow, long when) 
{
    Log.v("Message", "Executing draw method...");        
    boolean result;        
    result = super.draw(canvas, mapView, shadow, when);

    if(geoLastFix != null) //geoLastFix contains the GeoPoint of my Location.
    {
        Point screenPts = mapView.getProjection().toPixels(geoLastFix, null);

        //rotate bitmap
        Bitmap arrowBitmap = marker;
        Matrix matrix = new Matrix();           
        matrix.postRotate(orientation);
        Bitmap rotatedBmp = Bitmap.createBitmap(
            arrowBitmap, 
            0, 0, 
            arrowBitmap.getWidth(), 
            arrowBitmap.getHeight(), 
            matrix, 
            true
        );
       //print bitmap to canvas
        canvas.drawBitmap(
            rotatedBmp, 
            screenPts.x - (rotatedBmp.getWidth()  / 2), 
            screenPts.y - (rotatedBmp.getHeight() / 2), 
            null
        );
    }        
    return result;
}
4

1 回答 1

1

您不应该在 draw() 方法之外访问画布。如果您发现自己需要进行绘图或使用画布,则应覆盖 draw() 方法。

使用 onSensonrChanged 方法将当前方向或传感器信息保存到您的类状态中,然后覆盖 draw() 方法并使用保存的状态来增强您的绘图例程。

于 2012-02-03T07:36:30.730 回答