Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

打造帶切換動畫的 TabHost(仿微博客戶端效果)

編輯:關於 Android 程式設計

先看效果圖:

首先是版面配置檔。核心思路是在底部 Tab 上方覆蓋一個可滑動的 ImageView,用來呈現切換時的滑動動畫:



    
        
              
              
              
              
              
        
        
        
            
            
        
    

程式碼中先進行初始化,為每個分頁建立對應的 TabSpec 與指示器:

private void initTabHost() {
    mTabHost = getTabHost();
    mTabWidget = mTabHost.getTabWidget();
    mTabHost.setCurrentTab(0);

    // 以「首頁」為例,其餘分頁寫法相同
    View homeWidgetView = mLayoutInflater.inflate(R.layout.tab_indicator, mTabWidget, false);
    Intent intent = new Intent();
    intent.setClass(this, MainActivity.class);
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec(HOME_TAB);
    TextView homeTitle = (TextView) homeWidgetView.findViewById(R.id.title);
    homeTitle.setText(R.string.main_tab_home);
    ImageView homeIcon = (ImageView) homeWidgetView.findViewById(R.id.icon);
    homeIcon.setBackgroundResource(NORMAL_IMAGE[0]);
    tabSpec = tabSpec.setIndicator(homeWidgetView).setContent(intent);
    mTabHost.addTab(tabSpec);

    // ...其餘 CHAT_TAB、MESSAGE_TAB、PROFILE_TAB、MORE_TAB 依此類推

    mTabHost.setOnTabChangedListener(new TabHostListener(this));
    animImage = (ImageView) findViewById(R.id.tab_widget_image);
    animImage.setVisibility(View.INVISIBLE);
}

切換動畫透過滑動位移來呈現目前所在的分頁位置:

private void showAnimation() {
    if (lastTabIndex == currTabIndex) return;
    if (mTabWidget.getChildCount() < 2) return;

    // 為支援橫屏,需重新設定子 view 的寬高
    if (getResources().getConfiguration().orientation != lastImageChangeOrientation) {
        lastImageChangeOrientation = getResources().getConfiguration().orientation;
        widgetItemWidth = mTabWidget.getWidth() / mTabWidget.getChildCount();
        View currView = mTabWidget.getChildAt(currTabIndex);
        focusWidgetItemWidth = currView.getWidth();
        focusWidgetItemHeight = currView.getHeight();
        LayoutParams lp = animImage.getLayoutParams();
        lp.width = focusWidgetItemWidth;
        lp.height = focusWidgetItemHeight;
        animImage.setLayoutParams(lp);
    }

    int fromX = lastTabIndex * widgetItemWidth;
    int toX = currTabIndex * widgetItemWidth;
    TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0);
    animation.setDuration(600);
    animation.setAnimationListener(new AnimationListener() {
        @Override public void onAnimationStart(Animation animation) {
            mTabWidget.getChildAt(lastTabIndex).setBackgroundResource(R.drawable.tab_bottom);
            ((ImageView) mTabWidget.getChildAt(lastTabIndex).findViewById(R.id.icon))
                .setImageResource(NORMAL_IMAGE[lastTabIndex]);
            ((TextView) mTabWidget.getChildAt(lastTabIndex).findViewById(R.id.title))
                .setTextColor(getResources().getColor(R.drawable.gray2));
            animImage.setVisibility(View.VISIBLE);
        }
        @Override public void onAnimationRepeat(Animation animation) { }
        @Override public void onAnimationEnd(Animation animation) {
            animImage.setVisibility(View.INVISIBLE);
            ((ImageView) mTabWidget.getChildAt(currTabIndex).findViewById(R.id.icon))
                .setImageResource(SELECTED_IMAGE[currTabIndex]);
            ((TextView) mTabWidget.getChildAt(currTabIndex).findViewById(R.id.title))
                .setTextColor(getResources().getColor(R.color.white));
            mTabWidget.getChildAt(currTabIndex).setBackgroundResource(R.drawable.tab_bottom_selected);
            lastTabIndex = currTabIndex;
        }
    });
    animImage.startAnimation(animation);
}

private class TabHostListener implements TabHost.OnTabChangeListener {
    Context context;
    public TabHostListener(Context context) { this.context = context; }
    public void onTabChanged(String paramString) {
        lastTabIndex = currTabIndex;
        currTabIndex = mTabHost.getCurrentTab();
        if (lastTabIndex != currTabIndex) {
            showAnimation();
        }
    }
}

整體關鍵在於:透過覆蓋在 TabWidget 之上的 ImageView,配合 TranslateAnimation 做水平位移,就能模擬出類似微博客戶端那樣流暢的分頁切換動畫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved