在Android ViewFlipper中更好地实现Rotate3dAnimation

5
我想创建一个Android 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容器的内置功能。有没有更好或更有效的方法来实现这个目标?

2个回答

1

这是我能想到的最好方法。问题的一部分在于无法确定mapView的大小,以便计算动画构造函数的大小参数。我将我的onCreate代码更改为以下内容,并将其缩短了很多。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapoverviewlayout);

    // Initialize the map.
    mMapView = (MapView) findViewById(R.id.mapview);    
    mMapView.setBuiltInZoomControls(true);      

    // Obtain the view flipper.
    mFlipper = (ViewFlipper)findViewById(R.id.mapviewflipper);        

    // Initialize the animations.       
    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();          
    int h2 = display.getHeight() / 2;     
    int w2 = display.getWidth() / 2;

    mInMapAnimation = new Rotate3dAnimation(-90, 0, w2, h2, 0.0f, false);
    mInMapAnimation.setDuration(500);
    mInMapAnimation.setStartOffset(500);

    mInStgAnimation = new Rotate3dAnimation(90, 0, w2, h2, 0.0f, false);
    mInStgAnimation.setDuration(500);
    mInStgAnimation.setStartOffset(500);

    mOutMapAnimation = new Rotate3dAnimation(0, -90, w2, h2, 0.0f, false);
    mOutMapAnimation.setDuration(500);

    mOutStgAnimation = new Rotate3dAnimation(0, 90, w2, h2, 0.0f, false);
    mOutStgAnimation.setDuration(500);

}   

0
据我所知,ViewFlipper无论您当前看到哪个视图,都使用相同的动画进行进入和退出。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接