Domanda

i have implement a a rotation for the map in my application. I would like to use a button to switch from normal map view (no rotation) to rotation effects. The problem is that in the Oncreate the setContentView can start or normal map, or map with rotation. How can i switch the 2 Views?

This is the map rotation file:

public class MapViewCompassDemo extends MapActivity {


private SensorManager mSensorManager;
protected RotateView mRotateView;
private float mHeading = 0;

class RotateView extends ViewGroup  {
        private Matrix mMatrix = new Matrix();
                private float[] mTemp = new float[2]; 
    private static final float SQ2 = 1.414213562373095f;

    public RotateView(Context context) {
        super(context);

    }


    @SuppressWarnings("deprecation")
    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(-mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
        canvas.getMatrix().invert(mMatrix);
       super.dispatchDraw(canvas);
        canvas.restore();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = getWidth();
        final int height = getHeight();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View view = getChildAt(i);
            final int childWidth = view.getMeasuredWidth();
            final int childHeight = view.getMeasuredHeight();
            final int childLeft = (width - childWidth) / 2;
            final int childTop = (height - childHeight) / 2;
            view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        int sizeSpec;
        if (w > h) {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (w * SQ2), MeasureSpec.EXACTLY);
        } else {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (h * SQ2), MeasureSpec.EXACTLY);
        }
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(sizeSpec, sizeSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final float[] temp = mTemp;
        temp[0] = event.getX();
        temp[1] = event.getY();
        mMatrix.mapPoints(temp);
        event.setLocation(temp[0], temp[1]);
        return super.dispatchTouchEvent(event);
    }

 }

@Override
protected void onResume() {
    super.onResume();
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    mSensorManager.registerListener(myOrientationListener, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);

}

@Override
protected void onStop() {
    mSensorManager.unregisterListener(myOrientationListener);
    super.onStop();
}

protected boolean isRouteDisplayed() {
    return false;
}

final SensorEventListener myOrientationListener = new SensorEventListener() {
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mHeading = sensorEvent.values[0];
            mRotateView.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};

}

and this is an extends of the activity (i use to build commands for the map).

public class CommandActivity extends MapViewCompassDemo{
protected  MapView mapView;
protected MyLocationOverlay myLocationOverlay;


protected static final File MAP_FLORENCE = new File(Environment.getExternalStorageDirectory().getPath() + “/folder/map/", "florence.map");

private static final Byte MIN_ZOOM_LEVEL = new Byte("12");
private static final Byte MAX_ZOOM_LEVEL = new Byte("22");
private static final Byte START_ZOOM_LEVEL = new Byte("12");

private byte zoomLevelMax;
private byte zoomLevelMin;
private byte startZoomLevel;




public void configureMapViewFlorence() {
    FileOpenResult fileOpenResult = this.mapView.setMapFile(MAP_FLORENCE);
    GeoPoint geoPoint = new GeoPoint(43.7874, 11.2499);
    this.mapView.getMapViewPosition().setCenter(geoPoint);

    if (!fileOpenResult.isSuccess()) {
        Toast.makeText(this, fileOpenResult.getErrorMessage(), Toast.LENGTH_LONG).show();

    }

}



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


        zoomLevelMin = MIN_ZOOM_LEVEL;
    zoomLevelMax = MAX_ZOOM_LEVEL;
    startZoomLevel = START_ZOOM_LEVEL;

        mapView = new MapView(this);

        mapView.setBuiltInZoomControls(false);
        mapView.setEnabled(true);
        mapView.setClickable(true);
        mapView.setFocusable(true);
        MapScaleBar mapScaleBar = mapView.getMapScaleBar();
    mapScaleBar.setShowMapScaleBar(true);

    mRotateView = new RotateView(this);

        mRotateView.addView(mapView);   //   if i use this 2 lines, the activity start with the map rotation
        setContentView(mRotateView);   //


    mapView.addView(mRotateView);   //   if i use this 2 lines, the activity start with normal map ( no rotation)
        setContentView(mapView);





        FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); // this is where i call my xml layout with my buttons
        View.inflate(this, R.layout.map, rootLayout);

    configureMapViewFlorence();



    ImageButton compass = (ImageButton) findViewById(R.id.compass);
            compass.setBackgroundResource(R.drawable.action_click_gps);
            compass.setEnabled(true);




    compass.setOnClickListener(new View.OnClickListener() {   // i would like to use this button to switch from  normal map to rotate map
        public void onClick(View v) {


        }
    });


}

This is how you suggest

public class CommandActivity extends MapViewCompassDemo{
protected  MapView mapView;
protected MyLocationOverlay myLocationOverlay;


protected static final File MAP_FLORENCE = new File(Environment.getExternalStorageDirectory().getPath() + “/folder/map/", "florence.map");

private static final Byte MIN_ZOOM_LEVEL = new Byte("12");
private static final Byte MAX_ZOOM_LEVEL = new Byte("22");
private static final Byte START_ZOOM_LEVEL = new Byte("12");

private byte zoomLevelMax;
private byte zoomLevelMin;
private byte startZoomLevel;




public void configureMapViewFlorence() {
    FileOpenResult fileOpenResult = this.mapView.setMapFile(MAP_FLORENCE);
    GeoPoint geoPoint = new GeoPoint(43.7874, 11.2499);
    this.mapView.getMapViewPosition().setCenter(geoPoint);

    if (!fileOpenResult.isSuccess()) {
        Toast.makeText(this, fileOpenResult.getErrorMessage(), Toast.LENGTH_LONG).show();

    }

}



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

        zoomLevelMin = MIN_ZOOM_LEVEL;
    zoomLevelMax = MAX_ZOOM_LEVEL;
    startZoomLevel = START_ZOOM_LEVEL;

        FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); 
        View.inflate(this, R.layout.map, rootLayout);

    configureMapViewFlorence();

    ImageButton compass = (ImageButton) findViewById(R.id.compass);
            compass.setBackgroundResource(R.drawable.action_click_gps);
            compass.setEnabled(true);

    compass.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v) {
        if (isNnormal)
            RotateMap();                     
        else 
            NnormalMap();


        isNnormal = !isNormal;

        }
    });

    boolean isNormal = true;

    private void RotateMap()
    {
      mRotateView = new RotateView(this);
      mRotateView.addView(mapView);  
      setContentView(mRotateView); 

    }

     private void NnormalMap()
    {
     mapView = new MapView(this);
     mapView.setBuiltInZoomControls(false);
     mapView.setEnabled(true);
     mapView.setClickable(true);
     mapView.setFocusable(true);
     MapScaleBar mapScaleBar = mapView.getMapScaleBar();
     mapScaleBar.setShowMapScaleBar(true);
     mapView.addView(mRotateView);  
     setContentView(mapView);
    }
}

but it throws a NullPointerException

with this, it works, but when i call rotate map, my buttons layout disappear

public class CommandActivity extends MapViewCompassDemo{
protected  MapView mapView;
protected MyLocationOverlay myLocationOverlay;
protected static final File MAP_FLORENCE = new     File(Environment.getExternalStorageDirectory().getPath() + “/folder/map/", "florence.map");

private static final Byte MIN_ZOOM_LEVEL = new Byte("12");
private static final Byte MAX_ZOOM_LEVEL = new Byte("22");
private static final Byte START_ZOOM_LEVEL = new Byte("12");

private byte zoomLevelMax;
private byte zoomLevelMin;
private byte startZoomLevel;

 boolean isNormal = true;

private void RotateMap()
{
  mapView = new MapView(this);
  mapView.setBuiltInZoomControls(false);
  mapView.setEnabled(true);
  mapView.setClickable(true);
  mapView.setFocusable(true);
  MapScaleBar mapScaleBar = mapView.getMapScaleBar();
  mapScaleBar.setShowMapScaleBar(true);
  mRotateView = new RotateView(this);
  mRotateView.addView(mapView);  
  setContentView(mRotateView); 

}

 private void NnormalMap()
{
 mapView = new MapView(this);
 mapView.setBuiltInZoomControls(false);
 mapView.setEnabled(true);
 mapView.setClickable(true);
 mapView.setFocusable(true);
 MapScaleBar mapScaleBar = mapView.getMapScaleBar();
 mapScaleBar.setShowMapScaleBar(true);
 mRotateView = new RotateView(this);
 mapView.addView(mRotateView);  
 setContentView(mapView);
}


public void configureMapViewFlorence() {
    FileOpenResult fileOpenResult = this.mapView.setMapFile(MAP_FLORENCE);
    GeoPoint geoPoint = new GeoPoint(43.7874, 11.2499);
    this.mapView.getMapViewPosition().setCenter(geoPoint);

    if (!fileOpenResult.isSuccess()) {
    Toast.makeText(this, fileOpenResult.getErrorMessage(), Toast.LENGTH_LONG).show();

}

}



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

    zoomLevelMin = MIN_ZOOM_LEVEL;
    zoomLevelMax = MAX_ZOOM_LEVEL;
    startZoomLevel = START_ZOOM_LEVEL;

    NnormalMap();


    FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content); 
    View.inflate(this, R.layout.map, rootLayout);

    ImageButton compass = (ImageButton) findViewById(R.id.compass);
        compass.setBackgroundResource(R.drawable.action_click_gps);
        compass.setEnabled(true);

compass.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                 if (isNormal)
                        RotateMap();                     
                    else 
                        NnormalMap();



                    isNormal = !isNormal;


            }
        });

configureMapViewFlorence();



}
È stato utile?

Soluzione

you need one boolean value for checking witch layout is show:

boolean isNormal = true;

and in onClick method:

compass.setOnClickListener(new View.OnClickListener() {   // i would like to use this button to switch from  normal map to rotate map
        public void onClick(View v) {

            if (isNormal)
                RotateMap();                     
            else 
                NormalMap();


            isNormal = !isNormal;

        }
    });

NormalMap:

private void NnormalMap()
{
        mapView = new MapView(this);

        mapView.setBuiltInZoomControls(false);
        mapView.setEnabled(true);
        mapView.setClickable(true);
        mapView.setFocusable(true);
        MapScaleBar mapScaleBar = mapView.getMapScaleBar();
        mapScaleBar.setShowMapScaleBar(true);


        mapView.addView(mRotateView);   //   if i use this 2 lines, the activity start with normal map ( no rotation)
        setContentView(mapView);
}

RotateMap:

private void RotateMap()
{
     mRotateView = new RotateView(this);

     mRotateView.addView(mapView);   //   if i use this 2 lines, the activity start with the map rotation
     setContentView(mRotateView); 

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top