• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.Rect;
23 import android.graphics.drawable.ColorDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.util.AttributeSet;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.ViewParent;
29 import android.widget.HorizontalScrollView;
30 import android.widget.LinearLayout;
31 
32 /** Extension of LinearLayout that takes care of drawing bottom strips over the tab children. */
33 public class TabStripView extends LinearLayout {
34 
35     private Drawable mBottomLeftStrip;
36     private Drawable mBottomRightStrip;
37     private int mSelectedTabIndex;
38 
TabStripView(Context context)39     public TabStripView(Context context) {
40         this(context, null);
41     }
42 
TabStripView(Context context, AttributeSet attrs)43     public TabStripView(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         init();
46     }
47 
init()48     private void init() {
49         mGroupFlags |= FLAG_USE_CHILD_DRAWING_ORDER;
50         mBottomLeftStrip = mContext.getResources().getDrawable(
51                 R.drawable.tab_bottom);
52         mBottomRightStrip = mContext.getResources().getDrawable(
53                 R.drawable.tab_bottom);
54     }
55 
setSelected(int index, boolean selected)56     public void setSelected(int index, boolean selected) {
57         mSelectedTabIndex = index;
58         getChildAt(index).setSelected(selected);
59     }
60 
61     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)62     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
63         ViewParent parent = getParent();
64         if (parent instanceof HorizontalScrollView) {
65             setMinimumWidth(((HorizontalScrollView) getParent()).getMeasuredWidth());
66         }
67 
68         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
69     }
70 
71     @Override
getChildDrawingOrder(int childCount, int i)72     protected int getChildDrawingOrder(int childCount, int i) {
73         // Always draw the selected tab last, so that drop shadows are drawn
74         // in the correct z-order.
75         if (i == childCount - 1) {
76             return mSelectedTabIndex;
77         } else if (i >= mSelectedTabIndex) {
78             return i + 1;
79         } else {
80             return i;
81         }
82     }
83 
84     @Override
childDrawableStateChanged(View child)85     public void childDrawableStateChanged(View child) {
86         if (child == getChildAt(mSelectedTabIndex)) {
87             // To make sure that the bottom strip is redrawn
88             invalidate();
89         }
90         super.childDrawableStateChanged(child);
91     }
92 
93     @Override
dispatchDraw(Canvas canvas)94     public void dispatchDraw(Canvas canvas) {
95         super.dispatchDraw(canvas);
96 
97         View selectedChild = getChildAt(mSelectedTabIndex);
98 
99         mBottomRightStrip.setState(selectedChild.getDrawableState());
100         mBottomLeftStrip.setState(selectedChild.getDrawableState());
101 
102         Rect selBounds = new Rect(); // Bounds of the selected tab indicator
103         selBounds.left = selectedChild.getLeft() - getScrollX();
104         selBounds.right = selectedChild.getRight() - getScrollX();
105         final int myHeight = getHeight();
106         mBottomLeftStrip.setBounds(
107                 Math.min(0, selBounds.left
108                              - mBottomLeftStrip.getIntrinsicWidth()),
109                 myHeight - mBottomLeftStrip.getIntrinsicHeight(),
110                 selBounds.left,
111                 myHeight);
112         mBottomRightStrip.setBounds(
113                 selBounds.right,
114                 myHeight - mBottomRightStrip.getIntrinsicHeight(),
115                 Math.max(getWidth(),
116                         selBounds.right + mBottomRightStrip.getIntrinsicWidth()),
117                 myHeight);
118 
119         mBottomLeftStrip.draw(canvas);
120         mBottomRightStrip.draw(canvas);
121     }
122 
123 }
124