2010-11-02 79 views
4

您好 我有一個包含兩個子活動的TabHost活動。爲兒童活動添加「左 - 右」動畫最好的解決方案是什麼?更改標籤時添加動畫

此致敬禮!

回答

10


對於這一點,最好的解決辦法是動畫添加到佈局
假設你有兩個標籤

tabs = (TabHost) this.findViewById(R.id.tabhost_id); 
     tabs.setup();  
     tspec1 = tabs.newTabSpec(name_of_1st_tab) 
     tspec1.setIndicator(....); 
     tspec1.setContent(R.id.tab_1_layout_id); 
    tab1Layout = (LinearLayout)findViewById(R.id.tab_1_layout_id); 

    tspec2 = tabs.newTabSpec(name_of_2nd_tab) 
     tspec2.setIndicator(....); 
     tspec2.setContent(R.id.tab_1_layout_id); 
    tab1Layout = (LinearLayout)findViewByIdR.id.tab_2_layout_id); 

然後在TabChangedListener

tabs.setOnTabChangedListener(new OnTabChangeListener() { 

     public void onTabChanged(String tabId) { 
    tab1Layout.setAnimation(outToLeftAnimation()); 
    tab2Layout.setAnimation(inFromRightAnimation()); 
      } 
     }); 

    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(ConstandsUsed.ANIMATIION_DURATION); 
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
} 

public 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); 
    outtoLeft.setDuration(ConstandsUsed.ANIMATIION_DURATION); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
} 

希望這將有助於你得到一些想法

+0

感謝您的回覆!問題是我有一個活動標籤主機,而不是一個視圖標籤主機。 – Gratzi 2010-11-02 15:26:52

0

這可能會幫助你。主要想法是獲取選項卡的當前視圖並在onTabChanged事件中爲其設置動畫。

tabsHost.setOnTabChangedListener(new OnTabChangeListener() { 

    public void onTabChanged(String tabId) { 
     View currentView = tabsHost.getCurrentView(); 
     currentView.setAnimation(<Your animation object goes here>); 
    } 
});