Android的旋转图形页面 [英] Android Rotating MapView

查看:156
本文介绍了Android的旋转图形页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造这trakcs下用户路径上的一个图形页面的应用程序。我想,当用户需要向左转,例如地图就会以它自身以这样的方式的MapView的负责人应始终指向向上的方向,实现什么。我希望是有道理的。

I am creating an application which trakcs down users path on a mapview. What I want to achieve when user takes a left turn for instance map should rotate it self in such a way that the head of the mapview should always point to upward direction. I hope that makes sense.

我碰到罗曼盖伊先生的职位的,他说:

I came across Mr. Romain Guy's post an he says

我在过去做到了这一点,它   需要创建一个自定义的ViewGroup   旋转在画布   dispatchDraw()方法。您还需要   增加的MapView的大小   (因此它吸引足够的像素时   旋转。)您还需要旋转   在触摸事件   dispatchTouchEvent()。或者,如果你使用   安卓3.0,你可以简单地调用   theMapView.rotate():

I have done this in the past and it requires to create a custom ViewGroup that rotates the Canvas in the dispatchDraw() method. You also need to increase the size of the MapView (so that it draws enough pixels when rotated.) You will also need to rotate the touch events in dispatchTouchEvent(). Or if you use Android 3.0 you can simply call theMapView.rotate() :

有谁来到翻过一些解决方案类同我的问题?一个工作的例子是极好的:)

Does anyone came accross some solution similiar to my question ? A working example would be excellent :)

推荐答案

请记住,这不是一个理想的方法,因为地图上的文字是一个静态图片,并一起转动的地图图块(在某些时候它会颠倒)。

Keep in mind that it's not an ideal approach, because the text on the map is a static image and will rotate along with the map tiles (at some point it will be upside down).

下面是一个如何把图形页面到自己的布局部件和旋转的例子。我已经与使用OpenStreetMaps做了,但它应该是完全一样的谷歌地图。

Here's an example of how to put the MapView into your own Layout widget and rotate it. I've done it with the OpenStreetMaps, but it should be quite the same for Google Maps.

首先,创建了旋转布局控件

package com.eli.util;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;

public class RotatingLinearLayout extends LinearLayout {

    private final int mDiagonal;
    private float mBearing;

    public RotatingLinearLayout(final Context pContext, final AttributeSet pAttrs) {
        super(pContext, pAttrs);
        final DisplayMetrics dm = pContext.getResources().getDisplayMetrics();
        mDiagonal = (int) Math.hypot(dm.widthPixels, dm.heightPixels);
    }

    public void setBearing(final float pBearing) {
        mBearing = pBearing;
    }

    @Override
    protected void dispatchDraw(final Canvas pCanvas) {
        pCanvas.rotate(-mBearing, getWidth() >> 1, getHeight() >> 1);
        super.dispatchDraw(pCanvas);
    }

    @Override
    protected void onMeasure(final int pWidthMeasureSpec,
            final int pHeightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
        super.onMeasure(MeasureSpec.makeMeasureSpec(mDiagonal, widthMode), MeasureSpec.makeMeasureSpec(mDiagonal, heightMode));
    }
}

被它环绕你的图形页面 layout.xml

<com.eli.util.RotatingLinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rotating_layout">
    <org.osmdroid.views.MapView
        android:id="@+id/map_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"/>
</com.eli.util.RotatingLinearLayout>

现在,每次你收到一个地理修复时间,更新布局旋转的轴承,它应该打开。

Now, every time you receive a geo fix, update the bearing of the rotating layout and it should turn.

这篇关于Android的旋转图形页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆