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 17package com.android.contacts.common.list; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.graphics.Canvas; 22import android.graphics.Paint; 23import android.util.AttributeSet; 24import android.view.View; 25import android.widget.LinearLayout; 26import com.android.contacts.common.R; 27 28public class ViewPagerTabStrip extends LinearLayout { 29 30 private final Paint mSelectedUnderlinePaint; 31 private int mSelectedUnderlineThickness; 32 private int mIndexForSelection; 33 private float mSelectionOffset; 34 35 public ViewPagerTabStrip(Context context) { 36 this(context, null); 37 } 38 39 public ViewPagerTabStrip(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 42 final Resources res = context.getResources(); 43 44 mSelectedUnderlineThickness = res.getDimensionPixelSize(R.dimen.tab_selected_underline_height); 45 int underlineColor = res.getColor(R.color.tab_selected_underline_color); 46 int backgroundColor = res.getColor(R.color.contactscommon_actionbar_background_color); 47 48 mSelectedUnderlinePaint = new Paint(); 49 mSelectedUnderlinePaint.setColor(underlineColor); 50 51 setBackgroundColor(backgroundColor); 52 setWillNotDraw(false); 53 } 54 55 /** 56 * Notifies this view that view pager has been scrolled. We save the tab index and selection 57 * offset for interpolating the position and width of selection underline. 58 */ 59 void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 60 mIndexForSelection = position; 61 mSelectionOffset = positionOffset; 62 invalidate(); 63 } 64 65 @Override 66 protected void onDraw(Canvas canvas) { 67 int childCount = getChildCount(); 68 69 // Thick colored underline below the current selection 70 if (childCount > 0) { 71 View selectedTitle = getChildAt(mIndexForSelection); 72 73 if (selectedTitle == null) { 74 // The view pager's tab count changed but we weren't notified yet. Ignore this draw 75 // pass, when we get a new selection we will update and draw the selection strip in 76 // the correct place. 77 return; 78 } 79 int selectedLeft = selectedTitle.getLeft(); 80 int selectedRight = selectedTitle.getRight(); 81 final boolean isRtl = isRtl(); 82 final boolean hasNextTab = 83 isRtl ? mIndexForSelection > 0 : (mIndexForSelection < (getChildCount() - 1)); 84 if ((mSelectionOffset > 0.0f) && hasNextTab) { 85 // Draw the selection partway between the tabs 86 View nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1)); 87 int nextLeft = nextTitle.getLeft(); 88 int nextRight = nextTitle.getRight(); 89 90 selectedLeft = 91 (int) (mSelectionOffset * nextLeft + (1.0f - mSelectionOffset) * selectedLeft); 92 selectedRight = 93 (int) (mSelectionOffset * nextRight + (1.0f - mSelectionOffset) * selectedRight); 94 } 95 96 int height = getHeight(); 97 canvas.drawRect( 98 selectedLeft, 99 height - mSelectedUnderlineThickness, 100 selectedRight, 101 height, 102 mSelectedUnderlinePaint); 103 } 104 } 105 106 private boolean isRtl() { 107 return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; 108 } 109} 110