我在我的应用程序中实现了地图的旋转。我想使用一个按钮从法线贴图视图(无旋转)切换到旋转效果。 问题是在Oncreate中setContentView可以启动或法线贴图,或者用旋转贴图。如何切换2个视图?
这是地图轮换文件:
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) {
}
};
}
这是活动的扩展(我用来为地图构建命令)。
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) {
}
});
}
这就是你的建议
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);
}
}
但它会抛出NullPointerException
有了它,它可以工作,但是当我调用旋转地图时,我的按钮布局消失了
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();
}
答案 0 :(得分:0)
你需要一个布尔值来检查女巫布局是否显示:
boolean isNormal = true;
并在onClick方法中:
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;
}
});
法线:
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);
}