blob: 0bfe18516595e9e9e878c097e85801dd1707afe7 [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;
Michael Kolb1a8f3c42011-09-23 14:10:22 -070063 private AnimatorSet mAnimator;
Michael Kolba3194d02011-09-07 11:23:51 -070064
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
Michael Kolba3194d02011-09-07 11:23:51 -0700115 protected boolean isHorizontal() {
116 return mHorizontal;
117 }
118
119 public void setOrientation(int orientation) {
120 mContentView.setOrientation(orientation);
121 if (orientation == LinearLayout.HORIZONTAL) {
122 mContentView.setLayoutParams(
123 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
124 } else {
125 mContentView.setLayoutParams(
126 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
127 }
128 super.setOrientation(orientation);
129 }
130
131 @Override
132 protected void onMeasure(int wspec, int hspec) {
133 super.onMeasure(wspec, hspec);
134 calcPadding();
135 }
136
137 private void calcPadding() {
138 if (mAdapter.getCount() > 0) {
139 View v = mContentView.getChildAt(0);
140 if (mHorizontal) {
141 int pad = (getMeasuredWidth() - v.getMeasuredWidth()) / 2 + 2;
142 mContentView.setPadding(pad, 0, pad, 0);
143 } else {
144 int pad = (getMeasuredHeight() - v.getMeasuredHeight()) / 2 + 2;
145 mContentView.setPadding(0, pad, 0, pad);
146 }
147 }
148 }
149
150 public void setAdapter(BaseAdapter adapter) {
151 setAdapter(adapter, 0);
152 }
153
154
155 public void setOnRemoveListener(OnRemoveListener l) {
156 mRemoveListener = l;
157 }
158
159 public void setOnLayoutListener(OnLayoutListener l) {
160 mLayoutListener = l;
161 }
162
163 protected void setAdapter(BaseAdapter adapter, int selection) {
164 mAdapter = adapter;
165 mAdapter.registerDataSetObserver(new DataSetObserver() {
166
167 @Override
168 public void onChanged() {
169 super.onChanged();
170 handleDataChanged();
171 }
172
173 @Override
174 public void onInvalidated() {
175 super.onInvalidated();
176 }
177 });
178 handleDataChanged(selection);
179 }
180
181 protected ViewGroup getContentView() {
182 return mContentView;
183 }
184
185 protected int getRelativeChildTop(int ix) {
186 return mContentView.getChildAt(ix).getTop() - mScrollY;
187 }
188
189 protected void handleDataChanged() {
190 handleDataChanged(INVALID_POSITION);
191 }
192
John Reck1adf0302011-10-11 10:58:12 -0700193 void handleDataChanged(int newscroll) {
Michael Kolba3194d02011-09-07 11:23:51 -0700194 int scroll = getScrollValue();
195 if (mGapAnimator != null) {
196 mGapAnimator.cancel();
197 }
198 mContentView.removeAllViews();
199 for (int i = 0; i < mAdapter.getCount(); i++) {
200 View v = mAdapter.getView(i, null, mContentView);
201 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
202 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
203 lp.gravity = (mHorizontal ? Gravity.CENTER_VERTICAL : Gravity.CENTER_HORIZONTAL);
204 mContentView.addView(v, lp);
Michael Kolb0e3bff82011-10-17 11:24:27 -0700205 if (mGapPosition > INVALID_POSITION){
206 adjustViewGap(v, i);
Michael Kolba3194d02011-09-07 11:23:51 -0700207 }
208 }
209 if (newscroll > INVALID_POSITION) {
210 newscroll = Math.min(mAdapter.getCount() - 1, newscroll);
211 mNeedsScroll = true;
212 mScrollPosition = newscroll;
213 requestLayout();
214 } else {
215 setScrollValue(scroll);
216 }
Michael Kolba3194d02011-09-07 11:23:51 -0700217 }
218
219 protected void finishScroller() {
220 mScroller.forceFinished(true);
221 }
222
223 @Override
224 protected void onLayout(boolean changed, int l, int t, int r, int b) {
225 super.onLayout(changed, l, t, r, b);
226 if (mNeedsScroll) {
227 mScroller.forceFinished(true);
228 snapToSelected(mScrollPosition, false);
229 mNeedsScroll = false;
230 }
231 if (mLayoutListener != null) {
232 mLayoutListener.onLayout(l, t, r, b);
233 mLayoutListener = null;
234 }
235 }
236
Michael Kolba3194d02011-09-07 11:23:51 -0700237 void clearTabs() {
238 mContentView.removeAllViews();
239 }
240
241 void snapToSelected(int pos, boolean smooth) {
242 if (pos < 0) return;
243 View v = mContentView.getChildAt(pos);
244 int sx = 0;
245 int sy = 0;
246 if (mHorizontal) {
247 sx = (v.getLeft() + v.getRight() - getWidth()) / 2;
248 } else {
249 sy = (v.getTop() + v.getBottom() - getHeight()) / 2;
250 }
251 if ((sx != mScrollX) || (sy != mScrollY)) {
252 if (smooth) {
253 smoothScrollTo(sx,sy);
254 } else {
255 scrollTo(sx, sy);
256 }
257 }
258 }
259
260 protected void animateOut(View v) {
261 if (v == null) return;
262 animateOut(v, -mFlingVelocity);
263 }
264
265 private void animateOut(final View v, float velocity) {
266 float start = mHorizontal ? v.getTranslationY() : v.getTranslationX();
267 animateOut(v, velocity, start);
268 }
269
270 private void animateOut(final View v, float velocity, float start) {
271 if ((v == null) || (mAnimator != null)) return;
272 final int position = mContentView.indexOfChild(v);
273 int target = 0;
274 if (velocity < 0) {
275 target = mHorizontal ? -getHeight() : -getWidth();
276 } else {
277 target = mHorizontal ? getHeight() : getWidth();
278 }
279 int distance = target - (mHorizontal ? v.getTop() : v.getLeft());
280 long duration = (long) (Math.abs(distance) * 1000 / Math.abs(velocity));
Michael Kolb0e3bff82011-10-17 11:24:27 -0700281 int scroll = 0;
282 int translate = 0;
283 int gap = mHorizontal ? v.getWidth() : v.getHeight();
284 int centerView = getViewCenter(v);
285 int centerScreen = getScreenCenter();
286 int newpos = INVALID_POSITION;
287 if (centerView < centerScreen - gap / 2) {
288 // top view
289 scroll = - (centerScreen - centerView - gap);
290 translate = (position > 0) ? gap : 0;
291 newpos = position;
292 } else if (centerView > centerScreen + gap / 2) {
293 // bottom view
294 scroll = - (centerScreen + gap - centerView);
295 if (position < mAdapter.getCount() - 1) {
296 translate = -gap;
297 }
298 } else {
299 // center view
300 scroll = - (centerScreen - centerView);
301 if (position < mAdapter.getCount() - 1) {
302 translate = -gap;
303 } else {
304 scroll -= gap;
305 }
306 }
307 mGapPosition = position;
308 final int pos = newpos;
309 ObjectAnimator trans = ObjectAnimator.ofFloat(v,
310 (mHorizontal ? TRANSLATION_Y : TRANSLATION_X), start, target);
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700311 ObjectAnimator alpha = ObjectAnimator.ofFloat(v, ALPHA, getAlpha(v,start),
312 getAlpha(v,target));
Michael Kolb0e3bff82011-10-17 11:24:27 -0700313 AnimatorSet set1 = new AnimatorSet();
314 set1.playTogether(trans, alpha);
315 set1.setDuration(duration);
316 mAnimator = new AnimatorSet();
317 ObjectAnimator trans2 = null;
318 ObjectAnimator scroll1 = null;
319 if (scroll != 0) {
320 if (mHorizontal) {
321 scroll1 = ObjectAnimator.ofInt(this, "scrollX", getScrollX(), getScrollX() + scroll);
322 } else {
323 scroll1 = ObjectAnimator.ofInt(this, "scrollY", getScrollY(), getScrollY() + scroll);
324 }
Michael Kolba3194d02011-09-07 11:23:51 -0700325 }
Michael Kolb0e3bff82011-10-17 11:24:27 -0700326 if (translate != 0) {
327 trans2 = ObjectAnimator.ofInt(this, "gap", 0, translate);
328 }
329 final int duration2 = 200;
330 if (scroll1 != null) {
331 if (trans2 != null) {
332 AnimatorSet set2 = new AnimatorSet();
333 set2.playTogether(scroll1, trans2);
334 set2.setDuration(duration2);
335 mAnimator.playSequentially(set1, set2);
336 } else {
337 scroll1.setDuration(duration2);
338 mAnimator.playSequentially(set1, scroll1);
339 }
340 } else {
341 if (trans2 != null) {
342 trans2.setDuration(duration2);
343 mAnimator.playSequentially(set1, trans2);
344 }
345 }
Michael Kolba3194d02011-09-07 11:23:51 -0700346 mAnimator.addListener(new AnimatorListenerAdapter() {
347 public void onAnimationEnd(Animator a) {
348 if (mRemoveListener != null) {
Michael Kolba3194d02011-09-07 11:23:51 -0700349 mRemoveListener.onRemovePosition(position);
350 mAnimator = null;
Michael Kolb0e3bff82011-10-17 11:24:27 -0700351 mGapPosition = INVALID_POSITION;
352 mGap = 0;
353 handleDataChanged(pos);
Michael Kolba3194d02011-09-07 11:23:51 -0700354 }
355 }
356 });
357 mAnimator.start();
358 }
359
Michael Kolb0e3bff82011-10-17 11:24:27 -0700360 public void setGap(int gap) {
361 if (mGapPosition != INVALID_POSITION) {
362 mGap = gap;
363 postInvalidate();
364 }
365 }
366
367 public int getGap() {
368 return mGap;
369 }
370
371 void adjustGap() {
372 for (int i = 0; i < mContentView.getChildCount(); i++) {
373 final View child = mContentView.getChildAt(i);
374 adjustViewGap(child, i);
375 }
376 }
377
378 private void adjustViewGap(View view, int pos) {
379 if ((mGap < 0 && pos > mGapPosition)
380 || (mGap > 0 && pos < mGapPosition)) {
381 if (mHorizontal) {
382 view.setTranslationX(mGap);
383 } else {
384 view.setTranslationY(mGap);
385 }
386 }
387 }
388
389 private int getViewCenter(View v) {
390 if (mHorizontal) {
391 return v.getLeft() + v.getWidth() / 2;
392 } else {
393 return v.getTop() + v.getHeight() / 2;
394 }
395 }
396
397 private int getScreenCenter() {
398 if (mHorizontal) {
399 return getScrollX() + getWidth() / 2;
400 } else {
401 return getScrollY() + getHeight() / 2;
402 }
403 }
404
Michael Kolba3194d02011-09-07 11:23:51 -0700405 @Override
406 public void draw(Canvas canvas) {
Michael Kolba3194d02011-09-07 11:23:51 -0700407 if (mGapPosition > INVALID_POSITION) {
408 adjustGap();
409 }
Michael Kolb0e3bff82011-10-17 11:24:27 -0700410 super.draw(canvas);
Michael Kolba3194d02011-09-07 11:23:51 -0700411 }
412
413 @Override
414 protected View findViewAt(int x, int y) {
415 x += mScrollX;
416 y += mScrollY;
417 final int count = mContentView.getChildCount();
418 for (int i = count - 1; i >= 0; i--) {
419 View child = mContentView.getChildAt(i);
420 if (child.getVisibility() == View.VISIBLE) {
421 if ((x >= child.getLeft()) && (x < child.getRight())
422 && (y >= child.getTop()) && (y < child.getBottom())) {
423 return child;
424 }
425 }
426 }
427 return null;
428 }
429
430 @Override
431 protected void onOrthoDrag(View v, float distance) {
432 if ((v != null) && (mAnimator == null)) {
433 offsetView(v, distance);
434 }
435 }
436
437 @Override
438 protected void onOrthoDragFinished(View downView) {
439 if (mAnimator != null) return;
440 if (mIsOrthoDragged && downView != null) {
441 // offset
442 float diff = mHorizontal ? downView.getTranslationY() : downView.getTranslationX();
443 if (Math.abs(diff) > (mHorizontal ? downView.getHeight() : downView.getWidth()) / 2) {
444 // remove it
445 animateOut(downView, Math.signum(diff) * mFlingVelocity, diff);
446 } else {
447 // snap back
448 offsetView(downView, 0);
449 }
450 }
451 }
452
453 @Override
454 protected void onOrthoFling(View v, float velocity) {
455 if (v == null) return;
456 if (mAnimator == null && Math.abs(velocity) > mFlingVelocity / 2) {
457 animateOut(v, velocity);
458 } else {
459 offsetView(v, 0);
460 }
461 }
462
463 private void offsetView(View v, float distance) {
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700464 v.setAlpha(getAlpha(v, distance));
Michael Kolba3194d02011-09-07 11:23:51 -0700465 if (mHorizontal) {
466 v.setTranslationY(distance);
467 } else {
468 v.setTranslationX(distance);
469 }
470 }
471
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700472 private float getAlpha(View v, float distance) {
473 return 1 - (float) Math.abs(distance) / (mHorizontal ? v.getHeight() : v.getWidth());
474 }
475
476 private float ease(DecelerateInterpolator inter, float value, float start,
477 float dist, float duration) {
Michael Kolba3194d02011-09-07 11:23:51 -0700478 return start + dist * inter.getInterpolation(value / duration);
479 }
480
481 @Override
482 protected void onPull(int delta) {
483 boolean layer = false;
484 int count = 2;
485 if (delta == 0 && mPullValue == 0) return;
486 if (delta == 0 && mPullValue != 0) {
487 // reset
488 for (int i = 0; i < count; i++) {
489 View child = mContentView.getChildAt((mPullValue < 0)
490 ? i
491 : mContentView.getChildCount() - 1 - i);
492 if (child == null) break;
493 ObjectAnimator trans = ObjectAnimator.ofFloat(child,
494 mHorizontal ? "translationX" : "translationY",
495 mHorizontal ? getTranslationX() : getTranslationY(),
496 0);
497 ObjectAnimator rot = ObjectAnimator.ofFloat(child,
498 mHorizontal ? "rotationY" : "rotationX",
499 mHorizontal ? getRotationY() : getRotationX(),
500 0);
501 AnimatorSet set = new AnimatorSet();
502 set.playTogether(trans, rot);
503 set.setDuration(100);
504 set.start();
505 }
506 mPullValue = 0;
507 } else {
508 if (mPullValue == 0) {
509 layer = true;
510 }
511 mPullValue += delta;
512 }
513 final int height = mHorizontal ? getWidth() : getHeight();
514 int oscroll = Math.abs(mPullValue);
515 int factor = (mPullValue <= 0) ? 1 : -1;
516 for (int i = 0; i < count; i++) {
517 View child = mContentView.getChildAt((mPullValue < 0)
518 ? i
519 : mContentView.getChildCount() - 1 - i);
520 if (child == null) break;
521 if (layer) {
522 }
523 float k = PULL_FACTOR[i];
524 float rot = -factor * ease(mCubic, oscroll, 0, k * 2, height);
525 int y = factor * (int) ease(mCubic, oscroll, 0, k*20, height);
526 if (mHorizontal) {
527 child.setTranslationX(y);
528 } else {
529 child.setTranslationY(y);
530 }
531 if (mHorizontal) {
532 child.setRotationY(-rot);
533 } else {
534 child.setRotationX(rot);
535 }
536 }
537 }
538
539 static class ContentLayout extends LinearLayout {
540
541 NavTabScroller mScroller;
542
543 public ContentLayout(Context context, NavTabScroller scroller) {
544 super(context);
545 mScroller = scroller;
546 }
547
548 @Override
549 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
550 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Michael Kolb0e3bff82011-10-17 11:24:27 -0700551 if (mScroller.getGap() != 0) {
Michael Kolba3194d02011-09-07 11:23:51 -0700552 View v = getChildAt(0);
553 if (v != null) {
554 if (mScroller.isHorizontal()) {
555 int total = v.getMeasuredWidth() + getMeasuredWidth();
556 setMeasuredDimension(total, getMeasuredHeight());
557 } else {
558 int total = v.getMeasuredHeight() + getMeasuredHeight();
559 setMeasuredDimension(getMeasuredWidth(), total);
560 }
561 }
562
563 }
564 }
565
566 }
567
568}