1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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 15 */ 16 17 package com.android.contacts.common.list; 18 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; 26 27 import com.android.contacts.common.R; 28 29 public class ViewPagerTabStrip extends LinearLayout { 30 private int mSelectedUnderlineThickness; 31 private final Paint mSelectedUnderlinePaint; 32 33 private int mIndexForSelection; 34 private float mSelectionOffset; 35 36 public ViewPagerTabStrip(Context context) { 37 this(context, null); 38 } 39 40 public ViewPagerTabStrip(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 43 final Resources res = context.getResources(); 44 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.actionbar_background_color); 49 50 mSelectedUnderlinePaint = new Paint(); 51 mSelectedUnderlinePaint.setColor(underlineColor); 52 53 setBackgroundColor(backgroundColor); 54 setWillNotDraw(false); 55 } 56 57 /** 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 60 * underline. 61 */ 62 void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 63 mIndexForSelection = position; 64 mSelectionOffset = positionOffset; 65 invalidate(); 66 } 67 68 @Override 69 protected void onDraw(Canvas canvas) { 70 int childCount = getChildCount(); 71 72 // Thick colored underline below the current selection 73 if (childCount > 0) { 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(); 85 86 selectedLeft = (int) (mSelectionOffset * nextLeft + 87 (1.0f - mSelectionOffset) * selectedLeft); 88 selectedRight = (int) (mSelectionOffset * nextRight + 89 (1.0f - mSelectionOffset) * selectedRight); 90 } 91 92 int height = getHeight(); 93 canvas.drawRect(selectedLeft, height - mSelectedUnderlineThickness, 94 selectedRight, height, mSelectedUnderlinePaint); 95 } 96 } 97 98 private boolean isRtl() { 99 return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; 100 } 101 }