小编Mat*_* B.的帖子

在Android中旋转MapView

我正在编写一个Android应用程序,其中一个功能是地图将根据指南针旋转(即如果手机指向东方,地图将定向,以便地图的东侧位于顶部).我发现之前的答案建议在mapView中编写onDraw()方法,然而,api将方法更改为final,因此无法覆盖.结果我试图dispatchDraw()像这样覆盖这个方法:

注意:

-compass是一个布尔值,如果为true,则旋转视图

-bearing是一个浮点变量,具有视图应该旋转的度数

protected void dispatchDraw(Canvas canvas) {
    canvas.save();
         if (compass) {
             final float w = this.getWidth();
             final float h = this.getHeight();

             final float scaleFactor = (float)(Math.sqrt(h * h + w * w) / Math.min(w, h));

             final float centerX = w / 2.0f;
             final float centerY = h / 2.0f;

             canvas.rotate(bearing, centerX, centerY);
             canvas.scale(scaleFactor, scaleFactor, centerX, centerY);

         }
         super.dispatchDraw(canvas);
         canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)

android google-maps map rotation android-mapview

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

android-mapview ×1

google-maps ×1

map ×1

rotation ×1