blob: 9b875d1ba521f036402cc3095a0978ec9888713a [file] [log] [blame]
Evan Millar76c67fc2009-08-07 09:12:49 -07001/*
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
17package com.android.contacts;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Rect;
23import android.graphics.drawable.ColorDrawable;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.view.MotionEvent;
27import android.view.View;
Evan Millar56d2caa2009-08-20 20:30:12 -070028import android.view.ViewParent;
Evan Millar76c67fc2009-08-07 09:12:49 -070029import android.widget.HorizontalScrollView;
30import android.widget.LinearLayout;
31
32/** Extension of LinearLayout that takes care of drawing bottom strips over the tab children. */
33public class TabStripView extends LinearLayout {
34
35 private Drawable mBottomLeftStrip;
36 private Drawable mBottomRightStrip;
37 private int mSelectedTabIndex;
38
39 public TabStripView(Context context) {
40 this(context, null);
41 }
42
43 public TabStripView(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 init();
46 }
47
48 private void init() {
Evan Millar67a58532009-08-26 15:09:02 -070049 mGroupFlags |= FLAG_USE_CHILD_DRAWING_ORDER;
Evan Millar76c67fc2009-08-07 09:12:49 -070050 mBottomLeftStrip = mContext.getResources().getDrawable(
51 R.drawable.tab_bottom);
52 mBottomRightStrip = mContext.getResources().getDrawable(
53 R.drawable.tab_bottom);
54 }
55
56 public void setSelected(int index, boolean selected) {
57 mSelectedTabIndex = index;
58 getChildAt(index).setSelected(selected);
59 }
60
61 @Override
Evan Millar56d2caa2009-08-20 20:30:12 -070062 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
Evan Millar67a58532009-08-26 15:09:02 -070072 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
Evan Millar76c67fc2009-08-07 09:12:49 -070085 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
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}