代码之家  ›  专栏  ›  技术社区  ›  bporter

在Android ViewFlipper中更好地实现rotate3daniation

  •  5
  • bporter  · 技术社区  · 14 年前

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

    最后,我使用一个菜单按钮来选择合适的翻转动画。我这样做是因为如果地图翻转一个方向以显示“设置”视图,我希望它反向翻转以隐藏“设置”视图。所以在我的onprepareOptions菜单中(因为我的按钮就在那里),我做了以下操作:

    @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 回复  |  直到 14 年前
        1
  •  1
  •   bporter    14 年前

    这是我能想到的最好的了,部分问题是无法确定地图视图的大小,以便计算动画构造函数的大小参数。我将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);
    
    }   
    
        2
  •  0
  •   QRohlf    14 年前

    据我所知-没有。ViewFlipper使用相同的动画进行输入和输出,无论您当前看到的是哪个视图。