如何访问 MyLocationOverlay 的画布?

发布于 2025-01-02 15:34:34 字数 1460 浏览 1 评论 0原文

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

下面是我尝试重写绘制方法时的代码。然而,尽管我可以看到它正在连续执行,但旋转的位图需要一段时间才能显示。

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

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;
}

I override the drawMyLocation of MyLocationOverlay to display an arrow as marker instead of the normal blue dot. However, I also need to change the direction to which the pointer is pointing depending on the sensor of the phone. I decided to call drawMyLocation inside onSensorChanged. But one of the parameters required to call drawMyLocation is a Canvas. How can I access the Canvas of the MyLocationOverlay? Or do I need to create a new Canvas and pass the new Canvas everytime I call drawMyLocation inside onSensorChanged?

Below is my code when I tried to override draw method. However, even though I can see that it was continuously being executed, it takes a while for the rotated bitmap to display.

**UPDATE: If I try to touch my map, I can see my arrow rotating as I touch the map. However, if I don't touch it, it takes a while for the arrow to update its direction.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2025-01-09 15:34:34

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

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

You should not be accessing the canvas outside of the draw() method. If you find yourself needing to do drawing or use the canvas, you should override the draw() method.

Use the onSensonrChanged method to save the current orientation or sensor information into your classes state and then override the draw() method and use the saved state to augment your drawing routine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文