2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 package com.cyanogenmod.eleven.widgets;
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.LinearLayout;
27 import com.cyanogenmod.eleven.R;
29 public class ViewPagerTabStrip extends LinearLayout {
30 private int mSelectedUnderlineThickness;
31 private final Paint mSelectedUnderlinePaint;
33 private int mIndexForSelection;
34 private float mSelectionOffset;
36 public ViewPagerTabStrip(Context context) {
40 public ViewPagerTabStrip(Context context, AttributeSet attrs) {
41 super(context, attrs);
43 final Resources res = context.getResources();
45 mSelectedUnderlineThickness =
46 res.getDimensionPixelSize(R.dimen.tab_selected_underline_height);
47 int underlineColor = res.getColor(R.color.tab_selected_underline_color);
48 int backgroundColor = res.getColor(R.color.header_action_bar_color);
50 mSelectedUnderlinePaint = new Paint();
51 mSelectedUnderlinePaint.setColor(underlineColor);
53 setBackgroundColor(backgroundColor);
54 setWillNotDraw(false);
58 * Notifies this view that view pager has been scrolled. We save the tab index
59 * and selection offset for interpolating the position and width of selection
62 void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
63 mIndexForSelection = position;
64 mSelectionOffset = positionOffset;
69 protected void onDraw(Canvas canvas) {
70 int childCount = getChildCount();
72 // Thick colored underline below the current selection
74 View selectedTitle = getChildAt(mIndexForSelection);
75 int selectedLeft = selectedTitle.getLeft();
76 int selectedRight = selectedTitle.getRight();
77 final boolean isRtl = isRtl();
78 final boolean hasNextTab = isRtl ? mIndexForSelection > 0
79 : (mIndexForSelection < (getChildCount() - 1));
80 if ((mSelectionOffset > 0.0f) && hasNextTab) {
81 // Draw the selection partway between the tabs
82 View nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1));
83 int nextLeft = nextTitle.getLeft();
84 int nextRight = nextTitle.getRight();
86 selectedLeft = (int) (mSelectionOffset * nextLeft +
87 (1.0f - mSelectionOffset) * selectedLeft);
88 selectedRight = (int) (mSelectionOffset * nextRight +
89 (1.0f - mSelectionOffset) * selectedRight);
92 int height = getHeight();
93 canvas.drawRect(selectedLeft, height - mSelectedUnderlineThickness,
94 selectedRight, height, mSelectedUnderlinePaint);
98 private boolean isRtl() {
99 return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;