2012-04-04 15 views
6

मैं अपने टैबएक्टिवटी में एनीमेशन जोड़ने की कोशिश कर रहा हूं। उदाहरण के लिए, जब उपयोगकर्ता दूसरे टैब का चयन करता है, तो मैं चाहता हूं कि नई गतिविधि दाईं ओर से आती है। जब उपयोगकर्ता पहले टैब का चयन करता है, तो मैं चाहता हूं कि गतिविधि बाईं ओर से आती है।एंड्रॉइड - संक्रमण एनीमेशन के साथ टैबएक्टिविटी

मुझे एक एनीमेशन जोड़ने का तरीका मिला है, लेकिन मैं एक बार फिर से जोड़ना चाहता हूं। यहाँ कोड मैं का उपयोग कर रहा है:

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

और

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

मैं ऐसा कैसे कर सकते हैं?

धन्यवाद।

सम्मान।

वी

+0

यह उपयोगी हो सकता है: http://www.techienjoy.com/android-tab-example.php – Nimit

उत्तर

12

यह सही ढंग से काम करता है:

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

और एनिमेशन:

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

आप String tabId का उपयोग करें और जाँच if इस tabId==firstTab तो सही से छोड़ दिया else एनीमेशन से एनीमेशन डाल करने के लिए की है।

+0

ठीक है धन्यवाद। लेकिन मैं जो चाहता हूं वह कुछ है जैसे 'if (पिछला_टैब> टैबआईडी) एनीमेशन =' बाएं से दाएं 'और' अगर (पिछला_टैब <टैबआईडी) एनीमेशन = 'दाएं से बाएं''। मैं उसे कैसे कर सकता हूँ? – Manitoba

1

आप चाहें, तो आप उपयोग कर सकते हैं एंड्रॉयड सहायता पैकेज - http://developer.android.com/sdk/compatibility-library.html

कम प्रयास आप अपनी गतिविधि को संशोधित कर सकते हैं

टुकड़ों का उपयोग करने के लिए ताकि आपके टैब में यूट्यूब ऐप की तरह संक्रमण एनिमेशन हो सकें। यह इस प्रकार से लागू करने के लिए का एक नमूना कोड है - http://developer.android.com/sdk/compatibility-library.html

संपादित करें: आप सहायता पैकेज का उपयोग नहीं करना चाहते हैं, तो हो सकता है इस कार्यान्वयन

निजी वर्ग MyGestureDetector में मदद मिलेगी 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); 
     } 
} 
फैली

और उसके बाद आपके टैब पर श्रोता बदल गया उचित एनीमेशन लोड करें क्योंकि आप जानते हैं कि इशारा से पहले कौन सा चुना गया था, और जिसे हम बाद में स्विच कर रहे हैं।

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

इसके अलावा, आप (और शायद अलग स्क्रीन घनत्व के लिए खाते में यह निर्धारित करते समय एक इशारा एक कड़ी चोट कार्रवाई है) कस्टम इशारा डिटेक्टर आप के साथ onTouchListener अधिभावी द्वारा इशारा पकड़ने के लिए की जरूरत है

विस्तृत उत्तर के लिए खेद है , लेकिन मुझे उम्मीद है कि यह मदद करता है :)

+0

हाँ मुझे पता है कि मैं टुकड़ों और उनके ढेर के साथ ऐसा कर सकता हूं, लेकिन मैं चाहता हूं कि मेरा ऐप निम्नतम एपीआई स्तर का उपयोग करे।इसके अलावा, मेरा ऐप लगभग समाप्त हो गया है। मुझे बस उस इशारा को लागू करने की जरूरत है। – Manitoba

5

मैंने इस कोड के आधार पर एक कस्टम OnTabChangeListener लिखा था जिसे मैं साझा करना चाहता था। उम्मीद है कि कोई इसका उपयोग कर सकता है :)। मूल कोड के लिए क्रेडिट Vomenki जाता है।

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

प्रयास के लिए धन्यवाद, अच्छी तरह से काम करता है! – wufoo

+0

मैंने टैब के बीच स्लाइड करने के लिए इशारे भी जोड़े हैं, आप यहां कक्षा पा सकते हैं: http://danielkvist.net/portfolio/animated-tabhost-with-slide-gesture-in-android – span

संबंधित मुद्दे