Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2015 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.messaging.ui;
     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.messaging.R;
     28 import com.android.messaging.util.OsUtil;
     29 
     30 public class ViewPagerTabStrip extends LinearLayout {
     31     private int mSelectedUnderlineThickness;
     32     private final Paint mSelectedUnderlinePaint;
     33 
     34     private int mIndexForSelection;
     35     private float mSelectionOffset;
     36 
     37     public ViewPagerTabStrip(Context context) {
     38         this(context, null);
     39     }
     40 
     41     public ViewPagerTabStrip(Context context, AttributeSet attrs) {
     42         super(context, attrs);
     43 
     44         final Resources res = context.getResources();
     45 
     46         mSelectedUnderlineThickness =
     47                 res.getDimensionPixelSize(R.dimen.pager_tab_underline_selected);
     48         int underlineColor = res.getColor(R.color.contact_picker_tab_underline);
     49         int backgroundColor = res.getColor(R.color.action_bar_background_color);
     50 
     51         mSelectedUnderlinePaint = new Paint();
     52         mSelectedUnderlinePaint.setColor(underlineColor);
     53 
     54         setBackgroundColor(backgroundColor);
     55         setWillNotDraw(false);
     56     }
     57 
     58     /**
     59      * Notifies this view that view pager has been scrolled. We save the tab index
     60      * and selection offset for interpolating the position and width of selection
     61      * underline.
     62      */
     63     void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
     64         mIndexForSelection = position;
     65         mSelectionOffset = positionOffset;
     66         invalidate();
     67     }
     68 
     69     @Override
     70     protected void onDraw(Canvas canvas) {
     71         int childCount = getChildCount();
     72 
     73         // Thick colored underline below the current selection
     74         if (childCount > 0) {
     75             View selectedTitle = getChildAt(mIndexForSelection);
     76             int selectedLeft = selectedTitle.getLeft();
     77             int selectedRight = selectedTitle.getRight();
     78             final boolean isRtl = isRtl();
     79             final boolean hasNextTab = isRtl ? mIndexForSelection > 0
     80                     : (mIndexForSelection < (getChildCount() - 1));
     81             if ((mSelectionOffset > 0.0f) && hasNextTab) {
     82                 // Draw the selection partway between the tabs
     83                 View nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1));
     84                 int nextLeft = nextTitle.getLeft();
     85                 int nextRight = nextTitle.getRight();
     86 
     87                 selectedLeft = (int) (mSelectionOffset * nextLeft +
     88                         (1.0f - mSelectionOffset) * selectedLeft);
     89                 selectedRight = (int) (mSelectionOffset * nextRight +
     90                         (1.0f - mSelectionOffset) * selectedRight);
     91             }
     92 
     93             int height = getHeight();
     94             canvas.drawRect(selectedLeft, height - mSelectedUnderlineThickness,
     95                     selectedRight, height, mSelectedUnderlinePaint);
     96         }
     97     }
     98 
     99     private boolean isRtl() {
    100         return OsUtil.isAtLeastJB_MR2() ? getLayoutDirection() == View.LAYOUT_DIRECTION_RTL : false;
    101     }
    102 }