1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/* 2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project 3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License"); 5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License. 6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at 7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * http://www.apache.org/licenses/LICENSE-2.0 9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software 11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS, 12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and 14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License. 15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui; 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.res.Resources; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Canvas; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Paint; 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.util.AttributeSet; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View; 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.LinearLayout; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.OsUtil; 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ViewPagerTabStrip extends LinearLayout { 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private int mSelectedUnderlineThickness; 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private final Paint mSelectedUnderlinePaint; 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private int mIndexForSelection; 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private float mSelectionOffset; 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public ViewPagerTabStrip(Context context) { 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd this(context, null); 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public ViewPagerTabStrip(Context context, AttributeSet attrs) { 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(context, attrs); 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Resources res = context.getResources(); 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSelectedUnderlineThickness = 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd res.getDimensionPixelSize(R.dimen.pager_tab_underline_selected); 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int underlineColor = res.getColor(R.color.contact_picker_tab_underline); 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int backgroundColor = res.getColor(R.color.action_bar_background_color); 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSelectedUnderlinePaint = new Paint(); 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSelectedUnderlinePaint.setColor(underlineColor); 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setBackgroundColor(backgroundColor); 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setWillNotDraw(false); 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Notifies this view that view pager has been scrolled. We save the tab index 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * and selection offset for interpolating the position and width of selection 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * underline. 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mIndexForSelection = position; 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSelectionOffset = positionOffset; 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd invalidate(); 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected void onDraw(Canvas canvas) { 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int childCount = getChildCount(); 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Thick colored underline below the current selection 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (childCount > 0) { 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd View selectedTitle = getChildAt(mIndexForSelection); 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int selectedLeft = selectedTitle.getLeft(); 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int selectedRight = selectedTitle.getRight(); 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean isRtl = isRtl(); 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean hasNextTab = isRtl ? mIndexForSelection > 0 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd : (mIndexForSelection < (getChildCount() - 1)); 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if ((mSelectionOffset > 0.0f) && hasNextTab) { 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Draw the selection partway between the tabs 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd View nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1)); 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int nextLeft = nextTitle.getLeft(); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int nextRight = nextTitle.getRight(); 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd selectedLeft = (int) (mSelectionOffset * nextLeft + 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd (1.0f - mSelectionOffset) * selectedLeft); 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd selectedRight = (int) (mSelectionOffset * nextRight + 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd (1.0f - mSelectionOffset) * selectedRight); 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd int height = getHeight(); 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd canvas.drawRect(selectedLeft, height - mSelectedUnderlineThickness, 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd selectedRight, height, mSelectedUnderlinePaint); 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private boolean isRtl() { 100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return OsUtil.isAtLeastJB_MR2() ? getLayoutDirection() == View.LAYOUT_DIRECTION_RTL : false; 101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}