2010-08-10 6 views
5

Ich wollte eine Android MapActivity mit der Karte in einer Ansicht erstellen, die umgedreht und dann in einer anderen Ansicht konfiguriert werden konnte, und dann wieder auf die Karte zurückkippen. Die Lösung, die ich entwickelt habe, funktioniert, aber ich frage mich, ob es einen besseren Weg dafür gibt.Bessere Implementierung von Rotate3dAnimation in Android ViewFlipper

ich die Rotate3DAnimation Klasse aus dem Android ApiDemos kopiert, und erklärte, diese an der Spitze meiner MapActivity Klasse:

private MapView mMapView; 
private ViewFlipper mFlipper; 
private Rotate3dAnimation mInMapAnimation; 
private Rotate3dAnimation mInStgAnimation; 
private Rotate3dAnimation mOutMapAnimation; 
private Rotate3dAnimation mOutStgAnimation; 

nächste in meiner onCreate Methode des MapActivity Ich tat dies:

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



} 

Und schließlich habe ich eine Menü-Taste verwendet, um die entsprechenden Flip-Animationen auszuwählen. Ich tat dies, weil wenn die Karte um eine Richtung kippt, um die Einstellungsansicht zu öffnen, ich wollte, dass sie in die umgekehrte Richtung kippt, um die Einstellungsansicht auszublenden. Also in meinem onPrepareOptionsMenu (denn das ist, wo meine Taste war) Ich tat dies:

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

Dies funktioniert gut, aber es scheint nur ineffizient. Ich (anscheinend fälschlicherweise) nahm an, dass das "Spiegeln" eine eingebaute Funktion des ViewFlipper-Containers wäre. Gibt es einen besseren oder effizienteren Weg, dies zu erreichen?

Antwort

1

Dies ist das Beste, was ich könnte. Ein Teil des Problems bestand darin, die Größe der mapView nicht bestimmen zu können, um die Größenargumente für die Animationskonstruktoren zu berechnen. Ich habe meinen onCreate-Code in den folgenden geändert, und es hat es ein wenig gekürzt.

@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

So weit ich sagen konnte - nein. ViewFlipper verwendet dieselben Animationen für In und Out, egal welche Ansicht Sie gerade sehen.

Verwandte Themen