为切换选项卡时添加动画效果

4

你好,我有一个包含两个子活动的TabHost活动。如何为子活动添加“从左到右”的动画效果是最佳解决方案?

最好的问候!

2个回答

9

你好
解决这个问题的最佳方案是为布局添加动画效果
假设你有两个选项卡

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

希望这能帮助您了解一些思路。


谢谢回复!问题是我有一个活动选项卡宿主,而不是视图选项卡宿主。 - Gratzi

0

这可能会对你有所帮助。主要思路是在onTabChanged事件中获取当前选项卡的视图,并为其设置动画。

tabsHost.setOnTabChangedListener(new OnTabChangeListener() {

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接