blob: 03bf5953b6bf13319733ef56b2f0709dd8c20f85 [file] [log] [blame]
Michael Kolba3194d02011-09-07 11:23:51 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
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, 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.
15 */
16
17package com.android.browser;
18
19
20import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.AnimatorSet;
23import android.animation.ObjectAnimator;
24import android.content.Context;
25import android.database.DataSetObserver;
26import android.graphics.Canvas;
27import android.util.AttributeSet;
28import android.view.Gravity;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.animation.DecelerateInterpolator;
32import android.widget.BaseAdapter;
33import android.widget.LinearLayout;
34
35import com.android.browser.view.ScrollerView;
36
37/**
38 * custom view for displaying tabs in the nav screen
39 */
40public class NavTabScroller extends ScrollerView {
41
42 static final int INVALID_POSITION = -1;
43 static final float[] PULL_FACTOR = { 2.5f, 0.9f };
44
45 interface OnRemoveListener {
46 public void onRemovePosition(int position);
47 }
48
49 interface OnLayoutListener {
50 public void onLayout(int l, int t, int r, int b);
51 }
52
53 private ContentLayout mContentView;
54 private BaseAdapter mAdapter;
55 private OnRemoveListener mRemoveListener;
56 private OnLayoutListener mLayoutListener;
57 private int mGap;
58 private int mGapPosition;
59 private ObjectAnimator mGapAnimator;
60
61 // after drag animation velocity in pixels/sec
62 private static final float MIN_VELOCITY = 1500;
63 private Animator mAnimator;
64
65 private float mFlingVelocity;
66 private boolean mNeedsScroll;
67 private int mScrollPosition;
68
69 DecelerateInterpolator mCubic;
70 int mPullValue;
71
72 public NavTabScroller(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
74 init(context);
75 }
76
77 public NavTabScroller(Context context, AttributeSet attrs) {
78 super(context, attrs);
79 init(context);
80 }
81
82 public NavTabScroller(Context context) {
83 super(context);
84 init(context);
85 }
86
87 private void init(Context ctx) {
88 mCubic = new DecelerateInterpolator(1.5f);
89 mGapPosition = INVALID_POSITION;
90 setHorizontalScrollBarEnabled(false);
91 setVerticalScrollBarEnabled(false);
92 mContentView = new ContentLayout(ctx, this);
93 mContentView.setOrientation(LinearLayout.HORIZONTAL);
94 addView(mContentView);
95 mContentView.setLayoutParams(
96 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
97 // ProGuard !
98 setGap(getGap());
99 mFlingVelocity = getContext().getResources().getDisplayMetrics().density
100 * MIN_VELOCITY;
101 }
102
103 protected int getScrollValue() {
104 return mHorizontal ? mScrollX : mScrollY;
105 }
106
107 protected void setScrollValue(int value) {
108 scrollTo(mHorizontal ? value : 0, mHorizontal ? 0 : value);
109 }
110
111 protected NavTabView getTabView(int pos) {
112 return (NavTabView) mContentView.getChildAt(pos);
113 }
114
115 /**
116 * define a visual gap in the list of items
117 * the gap is rendered in front (left or above)
118 * the given position
119 * @param position
120 * @param gap
121 */
122 public void setGapPosition(int position, int gap) {
123 mGapPosition = position;
124 mGap = gap;
125 }
126
127 public void setGap(int gap) {
128 if (mGapPosition != INVALID_POSITION) {
129 mGap = gap;
130 postInvalidate();
131 }
132 }
133
134 public int getGap() {
135 return mGap;
136 }
137
138 protected boolean isHorizontal() {
139 return mHorizontal;
140 }
141
142 public void setOrientation(int orientation) {
143 mContentView.setOrientation(orientation);
144 if (orientation == LinearLayout.HORIZONTAL) {
145 mContentView.setLayoutParams(
146 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
147 } else {
148 mContentView.setLayoutParams(
149 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
150 }
151 super.setOrientation(orientation);
152 }
153
154 @Override
155 protected void onMeasure(int wspec, int hspec) {
156 super.onMeasure(wspec, hspec);
157 calcPadding();
158 }
159
160 private void calcPadding() {
161 if (mAdapter.getCount() > 0) {
162 View v = mContentView.getChildAt(0);
163 if (mHorizontal) {
164 int pad = (getMeasuredWidth() - v.getMeasuredWidth()) / 2 + 2;
165 mContentView.setPadding(pad, 0, pad, 0);
166 } else {
167 int pad = (getMeasuredHeight() - v.getMeasuredHeight()) / 2 + 2;
168 mContentView.setPadding(0, pad, 0, pad);
169 }
170 }
171 }
172
173 public void setAdapter(BaseAdapter adapter) {
174 setAdapter(adapter, 0);
175 }
176
177
178 public void setOnRemoveListener(OnRemoveListener l) {
179 mRemoveListener = l;
180 }
181
182 public void setOnLayoutListener(OnLayoutListener l) {
183 mLayoutListener = l;
184 }
185
186 protected void setAdapter(BaseAdapter adapter, int selection) {
187 mAdapter = adapter;
188 mAdapter.registerDataSetObserver(new DataSetObserver() {
189
190 @Override
191 public void onChanged() {
192 super.onChanged();
193 handleDataChanged();
194 }
195
196 @Override
197 public void onInvalidated() {
198 super.onInvalidated();
199 }
200 });
201 handleDataChanged(selection);
202 }
203
204 protected ViewGroup getContentView() {
205 return mContentView;
206 }
207
208 protected int getRelativeChildTop(int ix) {
209 return mContentView.getChildAt(ix).getTop() - mScrollY;
210 }
211
212 protected void handleDataChanged() {
213 handleDataChanged(INVALID_POSITION);
214 }
215
216 protected void handleDataChanged(int newscroll) {
217 int scroll = getScrollValue();
218 if (mGapAnimator != null) {
219 mGapAnimator.cancel();
220 }
221 mContentView.removeAllViews();
222 for (int i = 0; i < mAdapter.getCount(); i++) {
223 View v = mAdapter.getView(i, null, mContentView);
224 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
225 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
226 lp.gravity = (mHorizontal ? Gravity.CENTER_VERTICAL : Gravity.CENTER_HORIZONTAL);
227 mContentView.addView(v, lp);
228 if ((mGapPosition > INVALID_POSITION) && (i >= mGapPosition)) {
229 adjustViewGap(v, mGap);
230 }
231 }
232 if (newscroll > INVALID_POSITION) {
233 newscroll = Math.min(mAdapter.getCount() - 1, newscroll);
234 mNeedsScroll = true;
235 mScrollPosition = newscroll;
236 requestLayout();
237 } else {
238 setScrollValue(scroll);
239 }
240 if (mGapPosition > INVALID_POSITION) {
241 mGapAnimator = ObjectAnimator.ofInt(this, "gap", mGap, 0);
242 mGapAnimator.setDuration(250);
243 mGapAnimator.addListener(new AnimatorListenerAdapter() {
244 @Override
245 public void onAnimationEnd(Animator a) {
246 mGap = 0;
247 adjustGap();
248 mGapPosition = INVALID_POSITION;
249 mGapAnimator = null;
250 mContentView.requestLayout();
251 }
252 });
253 mGapAnimator.start();
254 }
255
256 }
257
258 protected void finishScroller() {
259 mScroller.forceFinished(true);
260 }
261
262 @Override
263 protected void onLayout(boolean changed, int l, int t, int r, int b) {
264 super.onLayout(changed, l, t, r, b);
265 if (mNeedsScroll) {
266 mScroller.forceFinished(true);
267 snapToSelected(mScrollPosition, false);
268 mNeedsScroll = false;
269 }
270 if (mLayoutListener != null) {
271 mLayoutListener.onLayout(l, t, r, b);
272 mLayoutListener = null;
273 }
274 }
275
276 void adjustGap() {
277 for (int i = 0; i < mContentView.getChildCount(); i++) {
278 if (i >= mGapPosition) {
279 final View child = mContentView.getChildAt(i);
280 adjustViewGap(child, mGap);
281 }
282 }
283 }
284
285 private void adjustViewGap(View view, int gap) {
286 if (mHorizontal) {
287 view.setTranslationX(gap);
288 } else {
289 view.setTranslationY(gap);
290 }
291 }
292
293
294 void clearTabs() {
295 mContentView.removeAllViews();
296 }
297
298 void snapToSelected(int pos, boolean smooth) {
299 if (pos < 0) return;
300 View v = mContentView.getChildAt(pos);
301 int sx = 0;
302 int sy = 0;
303 if (mHorizontal) {
304 sx = (v.getLeft() + v.getRight() - getWidth()) / 2;
305 } else {
306 sy = (v.getTop() + v.getBottom() - getHeight()) / 2;
307 }
308 if ((sx != mScrollX) || (sy != mScrollY)) {
309 if (smooth) {
310 smoothScrollTo(sx,sy);
311 } else {
312 scrollTo(sx, sy);
313 }
314 }
315 }
316
317 protected void animateOut(View v) {
318 if (v == null) return;
319 animateOut(v, -mFlingVelocity);
320 }
321
322 private void animateOut(final View v, float velocity) {
323 float start = mHorizontal ? v.getTranslationY() : v.getTranslationX();
324 animateOut(v, velocity, start);
325 }
326
327 private void animateOut(final View v, float velocity, float start) {
328 if ((v == null) || (mAnimator != null)) return;
329 final int position = mContentView.indexOfChild(v);
330 int target = 0;
331 if (velocity < 0) {
332 target = mHorizontal ? -getHeight() : -getWidth();
333 } else {
334 target = mHorizontal ? getHeight() : getWidth();
335 }
336 int distance = target - (mHorizontal ? v.getTop() : v.getLeft());
337 long duration = (long) (Math.abs(distance) * 1000 / Math.abs(velocity));
338 if (mHorizontal) {
339 mAnimator = ObjectAnimator.ofFloat(v, TRANSLATION_Y, start, target);
340 } else {
341 mAnimator = ObjectAnimator.ofFloat(v, TRANSLATION_X, start, target);
342 }
343 mAnimator.setDuration(duration);
344 mAnimator.addListener(new AnimatorListenerAdapter() {
345 public void onAnimationEnd(Animator a) {
346 if (mRemoveListener != null) {
347 boolean needsGap = position < (mAdapter.getCount() - 1);
348 if (needsGap) {
349 setGapPosition(position, mHorizontal ? v.getWidth() : v.getHeight());
350 }
351 mRemoveListener.onRemovePosition(position);
352 mAnimator = null;
353 }
354 }
355 });
356 mAnimator.start();
357 }
358
359 @Override
360 public void draw(Canvas canvas) {
361 super.draw(canvas);
362 if (mGapPosition > INVALID_POSITION) {
363 adjustGap();
364 }
365 }
366
367 @Override
368 protected View findViewAt(int x, int y) {
369 x += mScrollX;
370 y += mScrollY;
371 final int count = mContentView.getChildCount();
372 for (int i = count - 1; i >= 0; i--) {
373 View child = mContentView.getChildAt(i);
374 if (child.getVisibility() == View.VISIBLE) {
375 if ((x >= child.getLeft()) && (x < child.getRight())
376 && (y >= child.getTop()) && (y < child.getBottom())) {
377 return child;
378 }
379 }
380 }
381 return null;
382 }
383
384 @Override
385 protected void onOrthoDrag(View v, float distance) {
386 if ((v != null) && (mAnimator == null)) {
387 offsetView(v, distance);
388 }
389 }
390
391 @Override
392 protected void onOrthoDragFinished(View downView) {
393 if (mAnimator != null) return;
394 if (mIsOrthoDragged && downView != null) {
395 // offset
396 float diff = mHorizontal ? downView.getTranslationY() : downView.getTranslationX();
397 if (Math.abs(diff) > (mHorizontal ? downView.getHeight() : downView.getWidth()) / 2) {
398 // remove it
399 animateOut(downView, Math.signum(diff) * mFlingVelocity, diff);
400 } else {
401 // snap back
402 offsetView(downView, 0);
403 }
404 }
405 }
406
407 @Override
408 protected void onOrthoFling(View v, float velocity) {
409 if (v == null) return;
410 if (mAnimator == null && Math.abs(velocity) > mFlingVelocity / 2) {
411 animateOut(v, velocity);
412 } else {
413 offsetView(v, 0);
414 }
415 }
416
417 private void offsetView(View v, float distance) {
418 if (mHorizontal) {
419 v.setTranslationY(distance);
420 } else {
421 v.setTranslationX(distance);
422 }
423 }
424
425 private float ease(DecelerateInterpolator inter, float value, float start, float dist, float duration) {
426 return start + dist * inter.getInterpolation(value / duration);
427 }
428
429 @Override
430 protected void onPull(int delta) {
431 boolean layer = false;
432 int count = 2;
433 if (delta == 0 && mPullValue == 0) return;
434 if (delta == 0 && mPullValue != 0) {
435 // reset
436 for (int i = 0; i < count; i++) {
437 View child = mContentView.getChildAt((mPullValue < 0)
438 ? i
439 : mContentView.getChildCount() - 1 - i);
440 if (child == null) break;
441 ObjectAnimator trans = ObjectAnimator.ofFloat(child,
442 mHorizontal ? "translationX" : "translationY",
443 mHorizontal ? getTranslationX() : getTranslationY(),
444 0);
445 ObjectAnimator rot = ObjectAnimator.ofFloat(child,
446 mHorizontal ? "rotationY" : "rotationX",
447 mHorizontal ? getRotationY() : getRotationX(),
448 0);
449 AnimatorSet set = new AnimatorSet();
450 set.playTogether(trans, rot);
451 set.setDuration(100);
452 set.start();
453 }
454 mPullValue = 0;
455 } else {
456 if (mPullValue == 0) {
457 layer = true;
458 }
459 mPullValue += delta;
460 }
461 final int height = mHorizontal ? getWidth() : getHeight();
462 int oscroll = Math.abs(mPullValue);
463 int factor = (mPullValue <= 0) ? 1 : -1;
464 for (int i = 0; i < count; i++) {
465 View child = mContentView.getChildAt((mPullValue < 0)
466 ? i
467 : mContentView.getChildCount() - 1 - i);
468 if (child == null) break;
469 if (layer) {
470 }
471 float k = PULL_FACTOR[i];
472 float rot = -factor * ease(mCubic, oscroll, 0, k * 2, height);
473 int y = factor * (int) ease(mCubic, oscroll, 0, k*20, height);
474 if (mHorizontal) {
475 child.setTranslationX(y);
476 } else {
477 child.setTranslationY(y);
478 }
479 if (mHorizontal) {
480 child.setRotationY(-rot);
481 } else {
482 child.setRotationX(rot);
483 }
484 }
485 }
486
487 static class ContentLayout extends LinearLayout {
488
489 NavTabScroller mScroller;
490
491 public ContentLayout(Context context, NavTabScroller scroller) {
492 super(context);
493 mScroller = scroller;
494 }
495
496 @Override
497 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
498 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
499 if (mScroller.getGap() > 0) {
500 View v = getChildAt(0);
501 if (v != null) {
502 if (mScroller.isHorizontal()) {
503 int total = v.getMeasuredWidth() + getMeasuredWidth();
504 setMeasuredDimension(total, getMeasuredHeight());
505 } else {
506 int total = v.getMeasuredHeight() + getMeasuredHeight();
507 setMeasuredDimension(getMeasuredWidth(), total);
508 }
509 }
510
511 }
512 }
513
514 }
515
516}