我想创建一个Android MapActivity,其中地图在一个视图中,可以翻转到另一个视图中进行配置,然后再翻转回地图。我想到的解决方案可行,但我想知道是否有更好的方法。
我复制了Android ApiDemos中的Rotate3DAnimation类,并在我的MapActivity类顶部声明了以下内容:
我复制了Android ApiDemos中的Rotate3DAnimation类,并在我的MapActivity类顶部声明了以下内容:
private MapView mMapView;
private ViewFlipper mFlipper;
private Rotate3dAnimation mInMapAnimation;
private Rotate3dAnimation mInStgAnimation;
private Rotate3dAnimation mOutMapAnimation;
private Rotate3dAnimation mOutStgAnimation;
接下来,在我的MapActivity的onCreate方法中我这样做:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapoverviewlayout);
// Initialize the map.
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// Obtain the view flipper.
mFlipper = (ViewFlipper)findViewById(R.id.mapviewflipper);
// Initialize the settings view and handle the setting clicks.
Button stgMapDone = (Button)findViewById(R.id.MapViewOptionsDone);
stgMapDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
UnitOverviewMapActivity.this.mFlipper.showNext();
}
});
}
最后,我使用了一个菜单按钮来选择适当的翻转动画。我这样做是因为如果地图向一个方向翻转以显示设置视图,我希望它向相反的方向翻转以隐藏设置视图。因此,在我的onPrepareOptionsMenu中(因为那里有我的按钮),我做了以下操作:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mInMapAnimation == null) {
mInMapAnimation = new Rotate3dAnimation(-90, 0, mMapView.getWidth() / 2, mMapView.getHeight() / 2, 0.0f, false);
mInMapAnimation.setDuration(1000);
mInMapAnimation.setStartOffset(1000);
}
if (mInStgAnimation == null) {
mInStgAnimation = new Rotate3dAnimation(90, 0, mMapView.getWidth() / 2, mMapView.getHeight() / 2, 0.0f, false);
mInStgAnimation.setDuration(1000);
mInStgAnimation.setStartOffset(1000);
}
if (mOutMapAnimation == null) {
mOutMapAnimation = new Rotate3dAnimation(0, -90, mMapView.getWidth() / 2, mMapView.getHeight() / 2, 0.0f, false);
mOutMapAnimation.setDuration(1000);
}
if (mOutStgAnimation == null) {
mOutStgAnimation = new Rotate3dAnimation(0, 90, mMapView.getWidth() / 2, mMapView.getHeight() / 2, 0.0f, false);
mOutStgAnimation.setDuration(1000);
}
if (mFlipper.getCurrentView().getId() == R.id.mapparentlayout) {
mFlipper.setInAnimation(mInStgAnimation);
mFlipper.setOutAnimation(mOutMapAnimation);
}
else {
mFlipper.setInAnimation(mInMapAnimation);
mFlipper.setOutAnimation(mOutStgAnimation);
}
return true;
}
这样做可以正常运行,但似乎效率不高。我(显然是错误的)认为"翻转"应该是ViewFlipper容器的内置功能。有没有更好或更有效的方法来实现这个目标?