2012-04-04 37 views
6

Sto tentando di aggiungere un'animazione al mio TabActivty. Ad esempio, quando l'utente seleziona la seconda scheda, voglio che la nuova attività provenga da destra. Quando l'utente seleziona la prima scheda, voglio che l'attività provenga da sinistra.Android - TabActivity con animazione Transition

Ho trovato come aggiungere un'animazione, ma voglio aggiungerne un'altra. Ecco il codice che sto utilizzando:

public Animation inFromRightAnimation() 
{ 
    Animation inFromRight = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, +1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromRight.setDuration(240); 
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
} 

E

getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 
     public void onTabChanged(String tabId) 
     { 
      View currentView = getTabHost().getCurrentView(); 
      currentView.setAnimation(inFromRightAnimation()); 
     } 
}); 

Come posso fare questo?

Grazie.

Saluti.

V.

+0

può essere utile: http://www.techienjoy.com/android-tab-example.php – Nimit

risposta

12

Ciò funziona correttamente:

getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 
    public void onTabChanged(String tabId) 
    { 
      View currentView = getTabHost().getCurrentView(); 
      if (getTabHost().getCurrentTab() > currentTab) 
      { 
       currentView.setAnimation(inFromRightAnimation()); 
      } 
      else 
      { 
       currentView.setAnimation(outToRightAnimation()); 
      } 

      currentTab = getTabHost().getCurrentTab(); 
    } 
}); 

E le animazioni:

public Animation inFromRightAnimation() 
{ 
    Animation inFromRight = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, +1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromRight.setDuration(240); 
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
} 

public Animation outToRightAnimation() 
{ 
    Animation outtoLeft = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, -1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoLeft.setDuration(240); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
} 
1

Devi usare String tabId e verificare if questo tabId==firstTab poi mettere l'animazione da sinistra else animazione da destra.

+0

Ok, grazie. Ma quello che voglio è qualcosa come 'if (previous_tab> tabId) animation = 'left to right'' e' if (previous_tab Manitoba

1

Se si desidera, è possibile utilizzare Android Support Package - http://developer.android.com/sdk/compatibility-library.html

Con poco sforzo è possibile modificare la vostra attività per usare i frammenti in modo che le tue schede possano avere animazioni di transizione come l'app di YouTube. Ecco un codice di esempio di come implementarlo - http://developer.android.com/sdk/compatibility-library.html

Edit: Se non si desidera utilizzare pacchetto di sostegno, forse questa implementazione contribuirà

classe privata MyGestureDetector estende SimpleOnGestureListener {

 private static final int SWIPE_MIN_DISTANCE = 120; 
     private static final int SWIPE_MAX_OFF_PATH = 250; 
     private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     //get density 
      final DisplayMetrics metrics = getResources().getDisplayMetrics(); 
      final float density = metrics.density; 
     //System.out.println(" in onFling() :: "); 
      //off path 
      if (Math.abs(e1.getY() - e2.getY()) > density*SWIPE_MAX_OFF_PATH) 
       return false; 
      //fling from right to left 
      if (e1.getX() - e2.getX() > density*SWIPE_MIN_DISTANCE && Math.abs(velocityX) > density*SWIPE_THRESHOLD_VELOCITY) { 
       //if the first tab is selected 
       if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_info))) { 
        //switch to second tab and save current selection 
        tabs.setCurrentTab(1); 
        currentSelection = tabs.getCurrentTabTag(); 
       } 
       //if the second tab is selected 
       else if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_details))) { 
        //switch to second tab and save current selection 
        tabs.setCurrentTab(2); 
        currentSelection = tabs.getCurrentTabTag(); 
       } 
      } 
      //fling from left to right 
      else if (e2.getX() - e1.getX() > density*SWIPE_MIN_DISTANCE && Math.abs(velocityX) > density*SWIPE_THRESHOLD_VELOCITY) { 

       //if the second tab is selected 
       if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_details))) { 
        //switch to second tab and save current selection 
        tabs.setCurrentTab(0); 
        currentSelection = tabs.getCurrentTabTag(); 
       } 
       //if the third tab is selected 
       else if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_company))) { 
        //switch to second tab and save current selection 
        tabs.setCurrentTab(1); 
        currentSelection = tabs.getCurrentTabTag(); 
       } 
      } 
      return super.onFling(e1, e2, velocityX, velocityY); 
     } 
} 

e quindi sul listener modificato della scheda è sufficiente caricare l'animazione appropriata poiché si sa quale è stato selezionato prima del gesto e quello a cui si sta passando.

 @Override 
     public void onTabChanged(String tabId) { 



      //if the first tab is selected 
      if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) { 
       //if we switch to second 
       if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) { 
        linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out)); 
        linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in)); 
        linearCompany.setAnimation(null); 
       } 
       //if switch to third 
       else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) { 
        linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out)); 
        linearDetails.setAnimation(null); 
        linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in)); 
       } 
      } 
      //if the second tab is selected 
      else if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) { 
       //if we switch to first 
       if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) { 
        linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in)); 
        linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out)); 
        linearCompany.setAnimation(null); 
       } 
       //if switch to third 
       else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) { 
        linearInfo.setAnimation(null); 
        linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out)); 
        linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in)); 
       } 
      } 
      //if the third tab is selected 
      else if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) { 
       //if we switch to first 
       if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) { 
        linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in)); 
        linearDetails.setAnimation(null); 
        linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out)); 
       } 
       //if switch to second 
       else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) { 
        linearInfo.setAnimation(null); 
        linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in)); 
        linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out)); 
       } 
      } 

      currentSelection = tabId; 
     } 
    }; 

Inoltre è necessario per catturare il gesto sovrascrivendo l'onTouchListener con voi rilevatore gesto personalizzato (e forse rappresentano diversa densità dello schermo quando si determina se un gesto è un'azione colpo)

Ci scusiamo per la lunga risposta , ma spero che aiuti :)

+0

Sì, lo so che posso farlo con i frammenti e il loro stack, ma voglio che la mia app utilizzi il livello API più basso.Inoltre, la mia app è quasi finita. Devo solo implementare quel gesto. – Manitoba

5

Ho scritto un OnTabChangeListener personalizzato basato su questo codice che volevo condividere. Spero che qualcuno possa usarlo :). Il merito va a Vomenki per il codice originale.

package net.danielkvist.receipttracker.listener; 

import android.view.View; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 

/** 
* A custom OnTabChangeListener that uses the TabHost its related to to fetch information about the current and previous 
* tabs. It uses this information to perform some custom animations that slide the tabs in and out from left and right. 
* 
* @author Daniel Kvist 
* 
*/ 
public class AnimatedTabHostListener implements OnTabChangeListener 
{ 

    private static final int ANIMATION_TIME = 240; 
    private TabHost tabHost; 
    private View previousView; 
    private View currentView; 
    private int currentTab; 

    /** 
    * Constructor that takes the TabHost as a parameter and sets previousView to the currentView at instantiation 
    * 
    * @param tabHost 
    */ 
    public AnimatedTabHostListener(TabHost tabHost) 
    { 
     this.tabHost = tabHost; 
     this.previousView = tabHost.getCurrentView(); 
    } 

    /** 
    * When tabs change we fetch the current view that we are animating to and animate it and the previous view in the 
    * appropriate directions. 
    */ 
    @Override 
    public void onTabChanged(String tabId) 
    { 

     currentView = tabHost.getCurrentView(); 
     if (tabHost.getCurrentTab() > currentTab) 
     { 
      previousView.setAnimation(outToLeftAnimation()); 
      currentView.setAnimation(inFromRightAnimation()); 
     } 
     else 
     { 
      previousView.setAnimation(outToRightAnimation()); 
      currentView.setAnimation(inFromLeftAnimation()); 
     } 
     previousView = currentView; 
     currentTab = tabHost.getCurrentTab(); 

    } 

    /** 
    * Custom animation that animates in from right 
    * 
    * @return Animation the Animation object 
    */ 
    private Animation inFromRightAnimation() 
    { 
     Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     return setProperties(inFromRight); 
    } 

    /** 
    * Custom animation that animates out to the right 
    * 
    * @return Animation the Animation object 
    */ 
    private Animation outToRightAnimation() 
    { 
     Animation outToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     return setProperties(outToRight); 
    } 

    /** 
    * Custom animation that animates in from left 
    * 
    * @return Animation the Animation object 
    */ 
    private Animation inFromLeftAnimation() 
    { 
     Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     return setProperties(inFromLeft); 
    } 

    /** 
    * Custom animation that animates out to the left 
    * 
    * @return Animation the Animation object 
    */ 
    private Animation outToLeftAnimation() 
    { 
     Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     return setProperties(outtoLeft); 
    } 

    /** 
    * Helper method that sets some common properties 
    * @param animation the animation to give common properties 
    * @return the animation with common properties 
    */ 
    private Animation setProperties(Animation animation) 
    { 
     animation.setDuration(ANIMATION_TIME); 
     animation.setInterpolator(new AccelerateInterpolator()); 
     return animation; 
    } 
} 
+0

Grazie per lo sforzo, funziona bene! – wufoo

+0

Ho aggiunto dei gesti per scorrere tra le schede ora, puoi trovare la classe qui: http://danielkvist.net/portfolio/animated-tabhost-with-slide-gesture-in-android – span