【发布时间】:2010-12-22 06:30:02
【问题描述】:
我正在编写一个 Android 应用程序,其中一个功能是地图将根据指南针旋转(即,如果手机指向东方,则地图将被定向,以便地图的东侧位于顶部) .我发现以前的答案建议在 mapView 中编写 onDraw() 方法,但是,api 将方法更改为 final,因此它不能被覆盖。结果,我尝试像这样覆盖dispatchDraw() 方法:
注意:
-compass 是一个布尔值,如果为真,则旋转视图
-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();
}
【问题讨论】:
-
为什么需要重写 onDraw,难道不能只为 Activity 中的视图设置动画吗?
-
@Matthew:你有答案吗,如果有,请给我解决方案
标签: android google-maps map rotation android-mapview