旋转图形页面中的Andr​​oid [英] Rotate MapView in Android

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

问题描述

我写一个Android应用程序,其中的特点之一是,地图将旋转根据指南针(即如果手机指向东,地图将被取向,使得地图的东边是顶部) 。我发现previous答案建议在写图形页面中的OnDraw()方法,不过该API改变了方法,最终因此它不能被覆盖。因此,我试图覆盖 dispatchDraw()方法,像这样:

请注意:

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

-bearing是有度的,视图就会以浮点型变量

 保护无效dispatchDraw(帆布油画){
    canvas.save();
         如果(指南针){
             最终浮动W = this.getWidth();
             最终浮动H = this.getHeight();

             最终浮动比例因子=(浮点)(的Math.sqrt(H * H + W * W)/ Math.min(W,H));

             最终浮动的centerX = W / 2.0f;
             最终浮动centerY = H / 2.0f;

             canvas.rotate(轴承,的centerX,centerY);
             canvas.scale(比例因子,比例因子,的centerX,centerY);

         }
         super.dispatchDraw(画布);
         canvas.restore();
}
 

解决方案

由于pheelicks和尼基塔Koksharov答案,我管理根据指南针打开一个图形页面的开/关的旋转。

首先,你需要在两个内部 MapViewCompassDemo.java 的发现:Android_SDK_工具\附加组件\插件,google_apis - 谷歌 - #\样本\ MapsDemo \ SRC \ COM \例如\机器人\蜜蜂\查看\

  RotateView
SmoothCanvas
 

提取内部类RotateView为 RotateView.java 添加 SmoothCanvas作为一个内部类RotateView.java代替MapViewCompassDemo.java的

 公共类RotateView扩展的ViewGroup实现SensorListener {
...
   静态final类SmoothCanvas扩展画布{
...
   } //结束SmoothCanvas
} //结束RotateView
 

maplayout.xml:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / map_layout_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<的LinearLayout
    机器人:ID =@ + ID / rotating_view
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < com.google.android.maps.MapView
        机器人:ID =@ + ID / map_view
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:apiKey =#####地图这里的关键######
        机器人:可点击=真/>
< / LinearLayout中>

<切换按钮
    机器人:ID =@ + ID / button_compass
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:的onClick =的onClick
    机器人:textOff =北斗关
    机器人:/&GT纹元=指南针;

< / RelativeLayout的>
 

在MapActivity

  / **
 *如何显示一个谷歌地图视图旋转带指南针例活动
 *为了使它工作,你需要添加:
 *  - <使用库机器人:名称=com.google.android.maps/>在manifest.xml文件
 *  - 您的Andr​​oid地图API从https://developers.google.com/android/maps-api-注册的关键
 *  - 设置项目的构建目标,以谷歌的API
 *  - 提取/添加两个内部类RotateView和MapViewCompassDemo.java的SmoothCanvas,网址为:
 * .. \机器人\ Android的SDK工具\附加组件\插件,google_apis  - 谷歌 - #\样本\ MapsDemo的\ src \ COM \例如\机器人\蜜蜂\查看\
 *
 * @author hsigmond  -  touchboarder.com  - 
 *
 * /
公共类MapViewRotationWithCompass扩展MapActivity {

私人MapView类mMapView;
私人MyLocationOverlay mMyLocationOverlay = NULL;
私人布尔mModeCompass = FALSE;
私人的SensorManager mSensorManager;
私人的LinearLayout mRotateViewContainer;
私人RotateView mRotateView;


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.maplayout);
    mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
    mRotateViewContainer =(的LinearLayout)findViewById(R.id.rotating_view);
    mRotateView =新RotateView(本);

    //注册在Android的地图API:
    // https://developers.google.com/android/maps-api-signup
    //添加Android地图API密钥到的MapView在maplayout.xml文件
    mMapView =(图形页面)findViewById(R.id.map_view);
    mMyLocationOverlay =新MyLocationOverlay(这一点,mMapView);

}

@燮pressWarnings(德precation)
公共无效的onClick(视图v){

    开关(v.getId()){

    案例R.id.button_compass:
        如果(mMyLocationOverlay.isCompassEnabled()){
            mSensorManager.unregisterListener(mRotateView);
            mRotateView.removeAllViews();
            mRotateViewContainer.removeAllViews();
            mRotateViewContainer.addView(mMapView);
            mMyLocationOverlay.disableCompass();
            mModeCompass = FALSE;
        } 其他 {
            mRotateViewContainer.removeAllViews();
            mRotateView.removeAllViews();
            mRotateView.addView(mMapView);
            mRotateViewContainer.addView(mRotateView);
            mMapView.setClickable(真正的);
            mSensorManager.registerListener(mRotateView,
                    SensorManager.SENSOR_ORIENTATION,
                    SensorManager.SENSOR_DELAY_UI);
            mMyLocationOverlay.enableCompass();
            mModeCompass = TRUE;
        }
        打破;
    }
}

@燮pressWarnings(德precation)
@覆盖
公共无效onResume(){
    super.onResume();
    如果(mModeCompass){
        mMyLocationOverlay.enableCompass();
        mSensorManager.registerListener(mRotateView,
                SensorManager.SENSOR_ORIENTATION,
                SensorManager.SENSOR_DELAY_UI);
    }
}

@覆盖
公共无效的onPause(){
    super.onPause();
    mMyLocationOverlay.disableCompass();
}

@燮pressWarnings(德precation)
@覆盖
保护无效的onStop(){
    mSensorManager.unregisterListener(mRotateView);
    super.onStop();
}

@覆盖
保护的布尔isRouteDisplayed(){
    返回(假); //不显示路线
}

}
 

更新: 旋转谷歌的MapView指南针示例项目:<一href="https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT">https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT

I am writing an Android app where one of the features is that the map will rotate according to the compass (i.e. if the phone is pointing east, the map will be oriented so that the east side of the map is on top). Previous answers that I have found suggested over writing the onDraw() method in mapView, however, the api changed the method to final so it cannot be overwritten. As a result I have tried to overwrite the dispatchDraw() method like so:

Note:

-compass is a boolean that if true, rotate the view

-bearing is a float variable that has the degrees that the view should rotate

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

解决方案

Thanks to pheelicks and Nikita Koksharov answers, I manage to turn on/off the rotation of a mapview according to the compass.

First you will need the two inner class of MapViewCompassDemo.java found at: Android_SDK_ Tools\add-ons\addon-google_apis-google-#\samples\MapsDemo\src\com\example\android\apis\view\

RotateView
SmoothCanvas

Extract the inner class RotateView to RotateView.java and add SmoothCanvas as a inner class of RotateView.java instead of MapViewCompassDemo.java

public class RotateView extends ViewGroup implements SensorListener {
...
   static final class SmoothCanvas extends Canvas {
...
   }//end SmoothCanvas 
}//end RotateView 

maplayout.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/rotating_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey="##### YOUR MAP KEY HERE ######"
        android:clickable="true" />
</LinearLayout>

<ToggleButton
    android:id="@+id/button_compass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClick"
    android:textOff="compass off"
    android:textOn="compass on" />

</RelativeLayout>

The MapActivity

 /**
 * Example activity on how to display a google map view rotation with compass
 * To make it work you need to add:
 *  - <uses-library android:name="com.google.android.maps" /> in the manifest.xml file
 *  - Your Android Maps API Key from https://developers.google.com/android/maps-api- signup
 *  - Set the project build target to "Google APIs"
 *  - Extract/Add the two inner classes RotateView and SmoothCanvas of MapViewCompassDemo.java found at: 
 * ..\Android\Android SDK Tools\add-ons\addon-google_apis-google-#\samples\MapsDemo\src\com\example\android\apis\view\
 * 
 * @author hsigmond - touchboarder.com - 
 *
 */
public class MapViewRotationWithCompass extends MapActivity {

private MapView mMapView;
private MyLocationOverlay mMyLocationOverlay = null;
private boolean mModeCompass = false;
private SensorManager mSensorManager;
private LinearLayout mRotateViewContainer;
private RotateView mRotateView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.maplayout);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mRotateViewContainer = (LinearLayout) findViewById(R.id.rotating_view);
    mRotateView = new RotateView(this);

    // Sign Up for the Android Maps API at:
    // https://developers.google.com/android/maps-api-signup
    // Add the Android Maps API key to the MapView in the maplayout.xml file 
    mMapView = (MapView) findViewById(R.id.map_view);
    mMyLocationOverlay = new MyLocationOverlay(this, mMapView);     

}

@SuppressWarnings("deprecation")
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.button_compass:
        if (mMyLocationOverlay.isCompassEnabled()) {
            mSensorManager.unregisterListener(mRotateView);
            mRotateView.removeAllViews();
            mRotateViewContainer.removeAllViews();
            mRotateViewContainer.addView(mMapView);
            mMyLocationOverlay.disableCompass();
            mModeCompass = false;
        } else {
            mRotateViewContainer.removeAllViews();
            mRotateView.removeAllViews();
            mRotateView.addView(mMapView);
            mRotateViewContainer.addView(mRotateView);
            mMapView.setClickable(true);
            mSensorManager.registerListener(mRotateView,
                    SensorManager.SENSOR_ORIENTATION,
                    SensorManager.SENSOR_DELAY_UI);
            mMyLocationOverlay.enableCompass();
            mModeCompass = true;
        }
        break;
    }
}

@SuppressWarnings("deprecation")
@Override
public void onResume() {
    super.onResume();
    if (mModeCompass) {
        mMyLocationOverlay.enableCompass();
        mSensorManager.registerListener(mRotateView,
                SensorManager.SENSOR_ORIENTATION,
                SensorManager.SENSOR_DELAY_UI);
    }
}

@Override
public void onPause() {
    super.onPause();
    mMyLocationOverlay.disableCompass();
}

@SuppressWarnings("deprecation")
@Override
protected void onStop() {
    mSensorManager.unregisterListener(mRotateView);
    super.onStop();
}

@Override
protected boolean isRouteDisplayed() {
    return (false);// Don't display a route
}

}

Update: Rotating Google MapView with Compass Example Project: https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT

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

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