gpt4 book ai didi

java - 如何在同一个 Activity 中切换 View ?

转载 作者:太空狗 更新时间:2023-10-29 15:05:03 26 4
gpt4 key购买 nike

我在我的应用程序中实现了 map 旋转。我想使用一个按钮从普通 map View (无旋转)切换到旋转效果。问题是在 Oncreate 中,setContentView 可以启动或法线贴图,或带旋转的贴图。如何切换 2 个 View ?

这是 map 旋转文件:

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

}

这是 Activity 的扩展(我用来为 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) {


}
});


}

这是你的建议

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

有了这个,它就可以工作了,但是当我调用旋转 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;

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



}

最佳答案

您需要一个 boolean 值来检查女巫布局是否显示:

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

旋转 map :

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

}

关于java - 如何在同一个 Activity 中切换 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22815971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com