Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorListenerAdapter; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.PropertyValuesHolder; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.drawable.Drawable; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.View; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 29 | import android.view.animation.AccelerateInterpolator; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 30 | import android.widget.HorizontalScrollView; |
| 31 | import android.widget.LinearLayout; |
| 32 | |
| 33 | /** |
| 34 | * custom view for displaying tabs in the tabbed title bar |
| 35 | */ |
| 36 | public class TabScrollView extends HorizontalScrollView { |
| 37 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 38 | private Context mContext; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 39 | private LinearLayout mContentView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 40 | private int mSelected; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 41 | private Drawable mArrowLeft; |
| 42 | private Drawable mArrowRight; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 43 | private int mAnimationDuration; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * @param context |
| 47 | * @param attrs |
| 48 | * @param defStyle |
| 49 | */ |
| 50 | public TabScrollView(Context context, AttributeSet attrs, int defStyle) { |
| 51 | super(context, attrs, defStyle); |
| 52 | init(context); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param context |
| 57 | * @param attrs |
| 58 | */ |
| 59 | public TabScrollView(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
| 61 | init(context); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param context |
| 66 | */ |
| 67 | public TabScrollView(Context context) { |
| 68 | super(context); |
| 69 | init(context); |
| 70 | } |
| 71 | |
| 72 | private void init(Context ctx) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 73 | mContext = ctx; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 74 | mAnimationDuration = ctx.getResources().getInteger( |
| 75 | R.integer.tab_animation_duration); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 76 | setHorizontalScrollBarEnabled(false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 77 | mContentView = new LinearLayout(mContext); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 78 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 79 | mContentView.setLayoutParams( |
| 80 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 81 | addView(mContentView); |
| 82 | mSelected = -1; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 83 | mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left); |
| 84 | mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 85 | // prevent ProGuard from removing the property methods |
| 86 | setScroll(getScroll()); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 91 | super.onLayout(changed, left, top, right, bottom); |
| 92 | ensureChildVisible(getSelectedTab()); |
| 93 | } |
| 94 | |
| 95 | void setSelectedTab(int position) { |
| 96 | View v = getSelectedTab(); |
| 97 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 98 | v.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 99 | } |
| 100 | mSelected = position; |
| 101 | v = getSelectedTab(); |
| 102 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 103 | v.setActivated(true); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 104 | } |
| 105 | requestLayout(); |
| 106 | } |
| 107 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 108 | int getChildIndex(View v) { |
| 109 | return mContentView.indexOfChild(v); |
| 110 | } |
| 111 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 112 | View getSelectedTab() { |
| 113 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 114 | return mContentView.getChildAt(mSelected); |
| 115 | } else { |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void clearTabs() { |
| 121 | mContentView.removeAllViews(); |
| 122 | } |
| 123 | |
| 124 | void addTab(View tab) { |
| 125 | mContentView.addView(tab); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 126 | animateIn(tab); |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 127 | tab.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 130 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame] | 131 | int ix = mContentView.indexOfChild(tab); |
| 132 | if (ix == mSelected) { |
| 133 | mSelected = -1; |
| 134 | } else if (ix < mSelected) { |
| 135 | mSelected--; |
| 136 | } |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 137 | animateOut(tab); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 140 | private void ensureChildVisible(View child) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 141 | if (child != null) { |
| 142 | int childl = child.getLeft(); |
| 143 | int childr = childl + child.getWidth(); |
| 144 | int viewl = getScrollX(); |
| 145 | int viewr = viewl + getWidth(); |
| 146 | if (childl < viewl) { |
| 147 | // need scrolling to left |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 148 | animateScroll(childl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 149 | } else if (childr > viewr) { |
| 150 | // need scrolling to right |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 151 | animateScroll(childr - viewr + viewl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 156 | @Override |
| 157 | protected void dispatchDraw(Canvas canvas) { |
| 158 | super.dispatchDraw(canvas); |
| 159 | int l = getScrollX(); |
| 160 | int r = l + getWidth(); |
| 161 | int dis = 8; |
| 162 | if (l > 0) { |
| 163 | int aw = mArrowLeft.getIntrinsicWidth(); |
| 164 | mArrowLeft.setBounds(l + dis, 0, l + dis + aw, getHeight()); |
| 165 | mArrowLeft.draw(canvas); |
| 166 | } |
| 167 | if (r < mContentView.getWidth()) { |
| 168 | int aw = mArrowRight.getIntrinsicWidth(); |
| 169 | mArrowRight.setBounds(r - dis - aw, 0, r - dis, getHeight()); |
| 170 | mArrowRight.draw(canvas); |
| 171 | } |
| 172 | } |
| 173 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 174 | private void animateIn(View tab) { |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 175 | ObjectAnimator animator = ObjectAnimator.ofInt(tab, "TranslationX", 500, 0); |
| 176 | animator.setDuration(mAnimationDuration); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 177 | animator.start(); |
| 178 | } |
| 179 | |
| 180 | private void animateOut(final View tab) { |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 181 | ObjectAnimator animator = ObjectAnimator.ofInt( |
| 182 | tab, "TranslationX", 0, getScrollX() - tab.getRight()); |
| 183 | animator.setDuration(mAnimationDuration); |
| 184 | animator.addListener(new AnimatorListenerAdapter() { |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void onAnimationEnd(Animator animation) { |
| 187 | mContentView.removeView(tab); |
| 188 | } |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 189 | }); |
| 190 | animator.setInterpolator(new AccelerateInterpolator()); |
| 191 | animator.start(); |
| 192 | } |
| 193 | |
| 194 | private void animateScroll(int newscroll) { |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 195 | ObjectAnimator animator = ObjectAnimator.ofInt(this, "scroll", getScrollX(), newscroll); |
| 196 | animator.setDuration(mAnimationDuration); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 197 | animator.start(); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * required for animation |
| 202 | */ |
| 203 | public void setScroll(int newscroll) { |
| 204 | scrollTo(newscroll, getScrollY()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * required for animation |
| 209 | */ |
| 210 | public int getScroll() { |
| 211 | return getScrollX(); |
| 212 | } |
| 213 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 214 | } |