blob: 82519287b7a1e73f43b597c03acd8644e660b391 [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
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));
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700338 mAnimator = new AnimatorSet();
339 ObjectAnimator trans;
340 ObjectAnimator alpha = ObjectAnimator.ofFloat(v, ALPHA, getAlpha(v,start),
341 getAlpha(v,target));
Michael Kolba3194d02011-09-07 11:23:51 -0700342 if (mHorizontal) {
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700343 trans = ObjectAnimator.ofFloat(v, TRANSLATION_Y, start, target);
Michael Kolba3194d02011-09-07 11:23:51 -0700344 } else {
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700345 trans = ObjectAnimator.ofFloat(v, TRANSLATION_X, start, target);
Michael Kolba3194d02011-09-07 11:23:51 -0700346 }
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700347 mAnimator.playTogether(trans, alpha);
Michael Kolba3194d02011-09-07 11:23:51 -0700348 mAnimator.setDuration(duration);
349 mAnimator.addListener(new AnimatorListenerAdapter() {
350 public void onAnimationEnd(Animator a) {
351 if (mRemoveListener != null) {
352 boolean needsGap = position < (mAdapter.getCount() - 1);
353 if (needsGap) {
354 setGapPosition(position, mHorizontal ? v.getWidth() : v.getHeight());
355 }
356 mRemoveListener.onRemovePosition(position);
357 mAnimator = null;
358 }
359 }
360 });
361 mAnimator.start();
362 }
363
364 @Override
365 public void draw(Canvas canvas) {
366 super.draw(canvas);
367 if (mGapPosition > INVALID_POSITION) {
368 adjustGap();
369 }
370 }
371
372 @Override
373 protected View findViewAt(int x, int y) {
374 x += mScrollX;
375 y += mScrollY;
376 final int count = mContentView.getChildCount();
377 for (int i = count - 1; i >= 0; i--) {
378 View child = mContentView.getChildAt(i);
379 if (child.getVisibility() == View.VISIBLE) {
380 if ((x >= child.getLeft()) && (x < child.getRight())
381 && (y >= child.getTop()) && (y < child.getBottom())) {
382 return child;
383 }
384 }
385 }
386 return null;
387 }
388
389 @Override
390 protected void onOrthoDrag(View v, float distance) {
391 if ((v != null) && (mAnimator == null)) {
392 offsetView(v, distance);
393 }
394 }
395
396 @Override
397 protected void onOrthoDragFinished(View downView) {
398 if (mAnimator != null) return;
399 if (mIsOrthoDragged && downView != null) {
400 // offset
401 float diff = mHorizontal ? downView.getTranslationY() : downView.getTranslationX();
402 if (Math.abs(diff) > (mHorizontal ? downView.getHeight() : downView.getWidth()) / 2) {
403 // remove it
404 animateOut(downView, Math.signum(diff) * mFlingVelocity, diff);
405 } else {
406 // snap back
407 offsetView(downView, 0);
408 }
409 }
410 }
411
412 @Override
413 protected void onOrthoFling(View v, float velocity) {
414 if (v == null) return;
415 if (mAnimator == null && Math.abs(velocity) > mFlingVelocity / 2) {
416 animateOut(v, velocity);
417 } else {
418 offsetView(v, 0);
419 }
420 }
421
422 private void offsetView(View v, float distance) {
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700423 v.setAlpha(getAlpha(v, distance));
Michael Kolba3194d02011-09-07 11:23:51 -0700424 if (mHorizontal) {
425 v.setTranslationY(distance);
426 } else {
427 v.setTranslationX(distance);
428 }
429 }
430
Michael Kolb1a8f3c42011-09-23 14:10:22 -0700431 private float getAlpha(View v, float distance) {
432 return 1 - (float) Math.abs(distance) / (mHorizontal ? v.getHeight() : v.getWidth());
433 }
434
435 private float ease(DecelerateInterpolator inter, float value, float start,
436 float dist, float duration) {
Michael Kolba3194d02011-09-07 11:23:51 -0700437 return start + dist * inter.getInterpolation(value / duration);
438 }
439
440 @Override
441 protected void onPull(int delta) {
442 boolean layer = false;
443 int count = 2;
444 if (delta == 0 && mPullValue == 0) return;
445 if (delta == 0 && mPullValue != 0) {
446 // reset
447 for (int i = 0; i < count; i++) {
448 View child = mContentView.getChildAt((mPullValue < 0)
449 ? i
450 : mContentView.getChildCount() - 1 - i);
451 if (child == null) break;
452 ObjectAnimator trans = ObjectAnimator.ofFloat(child,
453 mHorizontal ? "translationX" : "translationY",
454 mHorizontal ? getTranslationX() : getTranslationY(),
455 0);
456 ObjectAnimator rot = ObjectAnimator.ofFloat(child,
457 mHorizontal ? "rotationY" : "rotationX",
458 mHorizontal ? getRotationY() : getRotationX(),
459 0);
460 AnimatorSet set = new AnimatorSet();
461 set.playTogether(trans, rot);
462 set.setDuration(100);
463 set.start();
464 }
465 mPullValue = 0;
466 } else {
467 if (mPullValue == 0) {
468 layer = true;
469 }
470 mPullValue += delta;
471 }
472 final int height = mHorizontal ? getWidth() : getHeight();
473 int oscroll = Math.abs(mPullValue);
474 int factor = (mPullValue <= 0) ? 1 : -1;
475 for (int i = 0; i < count; i++) {
476 View child = mContentView.getChildAt((mPullValue < 0)
477 ? i
478 : mContentView.getChildCount() - 1 - i);
479 if (child == null) break;
480 if (layer) {
481 }
482 float k = PULL_FACTOR[i];
483 float rot = -factor * ease(mCubic, oscroll, 0, k * 2, height);
484 int y = factor * (int) ease(mCubic, oscroll, 0, k*20, height);
485 if (mHorizontal) {
486 child.setTranslationX(y);
487 } else {
488 child.setTranslationY(y);
489 }
490 if (mHorizontal) {
491 child.setRotationY(-rot);
492 } else {
493 child.setRotationX(rot);
494 }
495 }
496 }
497
498 static class ContentLayout extends LinearLayout {
499
500 NavTabScroller mScroller;
501
502 public ContentLayout(Context context, NavTabScroller scroller) {
503 super(context);
504 mScroller = scroller;
505 }
506
507 @Override
508 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
509 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
510 if (mScroller.getGap() > 0) {
511 View v = getChildAt(0);
512 if (v != null) {
513 if (mScroller.isHorizontal()) {
514 int total = v.getMeasuredWidth() + getMeasuredWidth();
515 setMeasuredDimension(total, getMeasuredHeight());
516 } else {
517 int total = v.getMeasuredHeight() + getMeasuredHeight();
518 setMeasuredDimension(getMeasuredWidth(), total);
519 }
520 }
521
522 }
523 }
524
525 }
526
527}