Android Open Source - AndroidNavigationDrawerSample Sliding Tab Strip






From Project

Back to project page AndroidNavigationDrawerSample.

License

The source code is released under:

Apache License

If you think the Android project AndroidNavigationDrawerSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 The Android Open Source Project
 */*from w  w  w  .j a v a 2 s.c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.dev.eth0.android.sample.navigationDrawer.view.slidingtab;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Tab-Strip based on example from Android SDK
 *
 * @author deveth0
 */
public class SlidingTabStrip extends LinearLayout {

  private static final int SELECTED_INDICATOR_THICKNESS_DIPS = 4;

  private final int mSelectedIndicatorThickness;
  private final Paint mSelectedIndicatorPaint;

  private int mSelectedPosition;
  private float mSelectionOffset;

  SlidingTabStrip(Context context) {
    this(context, null);
  }

  SlidingTabStrip(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWillNotDraw(false);

    final float density = getResources().getDisplayMetrics().density;

    mSelectedIndicatorThickness = (int)(SELECTED_INDICATOR_THICKNESS_DIPS * density);
    mSelectedIndicatorPaint = new Paint();
  }

  void onViewPagerPageChanged(int position, float positionOffset) {
    mSelectedPosition = position;
    mSelectionOffset = positionOffset;
    invalidate();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    final int height = getHeight();
    final int childCount = getChildCount();

    // Thick colored underline below the current selection
    if (childCount > 0) {
      View selectedTitle = getChildAt(mSelectedPosition);
      int left = selectedTitle.getLeft();
      int right = selectedTitle.getRight();

      if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) {

        // Draw the selection partway between the tabs
        View nextTitle = getChildAt(mSelectedPosition + 1);
        left = (int)(mSelectionOffset * nextTitle.getLeft()
                + (1.0f - mSelectionOffset) * left);
        right = (int)(mSelectionOffset * nextTitle.getRight()
                + (1.0f - mSelectionOffset) * right);
      }

      mSelectedIndicatorPaint.setColor(SlidingTabLayout.DEFAULT_COLOR_INDICATOR);

      canvas.drawRect(left, height - mSelectedIndicatorThickness, right,
              height, mSelectedIndicatorPaint);
    }

  }
}




Java Source Code List

de.dev.eth0.android.sample.navigationDrawer.NavigationDrawerApplication.java
de.dev.eth0.android.sample.navigationDrawer.navigation.NavigationDrawerPagerAdapter.java
de.dev.eth0.android.sample.navigationDrawer.navigation.SlidingTabsPagerAdapter.java
de.dev.eth0.android.sample.navigationDrawer.ui.AboutActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.AbstractBaseActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.MainActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.AboutFragment.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.AbstractSlidingTabsFragment.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment1.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment2.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment3.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment4.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.MainFragment.java
de.dev.eth0.android.sample.navigationDrawer.view.slidingtab.SlidingTabLayout.java
de.dev.eth0.android.sample.navigationDrawer.view.slidingtab.SlidingTabStrip.java