Evan Millar | 76c67fc | 2009-08-07 09:12:49 -0700 | [diff] [blame] | 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.widget.HorizontalScrollView; |
| 29 | import android.widget.LinearLayout; |
| 30 | |
| 31 | /** Extension of LinearLayout that takes care of drawing bottom strips over the tab children. */ |
| 32 | public class TabStripView extends LinearLayout { |
| 33 | |
| 34 | private Drawable mBottomLeftStrip; |
| 35 | private Drawable mBottomRightStrip; |
| 36 | private int mSelectedTabIndex; |
| 37 | |
| 38 | public TabStripView(Context context) { |
| 39 | this(context, null); |
| 40 | } |
| 41 | |
| 42 | public TabStripView(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
| 44 | init(); |
| 45 | } |
| 46 | |
| 47 | private void init() { |
| 48 | mBottomLeftStrip = mContext.getResources().getDrawable( |
| 49 | R.drawable.tab_bottom); |
| 50 | mBottomRightStrip = mContext.getResources().getDrawable( |
| 51 | R.drawable.tab_bottom); |
| 52 | } |
| 53 | |
| 54 | public void setSelected(int index, boolean selected) { |
| 55 | mSelectedTabIndex = index; |
| 56 | getChildAt(index).setSelected(selected); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void childDrawableStateChanged(View child) { |
| 61 | if (child == getChildAt(mSelectedTabIndex)) { |
| 62 | // To make sure that the bottom strip is redrawn |
| 63 | invalidate(); |
| 64 | } |
| 65 | super.childDrawableStateChanged(child); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void dispatchDraw(Canvas canvas) { |
| 70 | super.dispatchDraw(canvas); |
| 71 | |
| 72 | View selectedChild = getChildAt(mSelectedTabIndex); |
| 73 | |
| 74 | mBottomRightStrip.setState(selectedChild.getDrawableState()); |
| 75 | mBottomLeftStrip.setState(selectedChild.getDrawableState()); |
| 76 | |
| 77 | Rect selBounds = new Rect(); // Bounds of the selected tab indicator |
| 78 | selBounds.left = selectedChild.getLeft() - getScrollX(); |
| 79 | selBounds.right = selectedChild.getRight() - getScrollX(); |
| 80 | final int myHeight = getHeight(); |
| 81 | mBottomLeftStrip.setBounds( |
| 82 | Math.min(0, selBounds.left |
| 83 | - mBottomLeftStrip.getIntrinsicWidth()), |
| 84 | myHeight - mBottomLeftStrip.getIntrinsicHeight(), |
| 85 | selBounds.left, |
| 86 | myHeight); |
| 87 | mBottomRightStrip.setBounds( |
| 88 | selBounds.right, |
| 89 | myHeight - mBottomRightStrip.getIntrinsicHeight(), |
| 90 | Math.max(getWidth(), |
| 91 | selBounds.right + mBottomRightStrip.getIntrinsicWidth()), |
| 92 | myHeight); |
| 93 | |
| 94 | mBottomLeftStrip.draw(canvas); |
| 95 | mBottomRightStrip.draw(canvas); |
| 96 | } |
| 97 | |
| 98 | } |