summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/FilmStripView.java
blob: f2ffa8719f4993ec1f46114da7bb3f1fd19cbfee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.ui;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;

public class FilmStripView extends ViewGroup {
    private static final String TAG = "FilmStripView";
    private static final int BUFFER_SIZE = 5;
    // Horizontal padding of children.
    private static final int H_PADDING = 50;
    // Duration to go back to the first.
    private static final int DURATION_BACK_ANIM = 500;
    private static final int DURATION_SCROLL_TO_FILMSTRIP = 350;
    private static final int DURATION_GEOMETRY_ADJUST = 200;
    private static final float FILM_STRIP_SCALE = 0.6f;
    private static final float MAX_SCALE = 1f;

    private Context mContext;
    private FilmStripGestureRecognizer mGestureRecognizer;
    private DataAdapter mDataAdapter;
    private final Rect mDrawArea = new Rect();

    private int mCurrentInfo;
    private float mScale;
    private GeometryAnimator mGeometryAnimator;
    private int mCenterPosition = -1;
    private ViewInfo[] mViewInfo = new ViewInfo[BUFFER_SIZE];

    public interface ImageData {
        public static final int TYPE_NONE = 0;
        public static final int TYPE_CAMERA_PREVIEW = 1;
        public static final int TYPE_PHOTO = 2;
        public static final int TYPE_VIDEO = 3;
        public static final int TYPE_PHOTOSPHERE = 4;

        // The actions are defined bit-wise so we can use bit operations like
        // | and &.
        public static final int ACTION_NONE = 0;
        public static final int ACTION_PROMOTE = 1;
        public static final int ACTION_DEMOTE = 2;

        // SIZE_FULL means disgard the width or height when deciding the view size
        // of this ImageData, just use full screen size.
        public static final int SIZE_FULL = -2;

        // The values returned by getWidth() and getHeight() will be used for layout.
        public int getWidth();
        public int getHeight();
        public int getType();
        public boolean isActionSupported(int action);
    }

    public interface DataAdapter {
        public interface Listener {
            public void onDataLoaded();
            public void onDataInserted(int dataID);
            public void onDataRemoved(int dataID);
        }

        public int getTotalNumber();
        public View getView(Context context, int id);
        public ImageData getImageData(int id);
        public void suggestSize(int w, int h);

        public void requestLoad(ContentResolver r);
        public void setListener(Listener listener);
    }

    // A helper class to tract and calculate the view coordination.
    private static class ViewInfo {
        private int mDataID;
        // the position of the left of the view in the whole filmstrip.
        private int mLeftPosition;
        private View mView;
        private float mOffsetY;

        public ViewInfo(int id, View v) {
            v.setPivotX(0f);
            v.setPivotY(0f);
            mDataID = id;
            mView = v;
            mLeftPosition = -1;
            mOffsetY = 0;
        }

        public int getID() {
            return mDataID;
        }

        public void setLeftPosition(int pos) {
            mLeftPosition = pos;
        }

        public int getLeftPosition() {
            return mLeftPosition;
        }

        public float getOffsetY() {
            return mOffsetY;
        }

        public void setOffsetY(float offset) {
            mOffsetY = offset;
        }

        public int getCenterX() {
            return mLeftPosition + mView.getWidth() / 2;
        }

        public View getView() {
            return mView;
        }

        private void layoutAt(int left, int top) {
            mView.layout(left, top, left + mView.getMeasuredWidth(),
                    top + mView.getMeasuredHeight());
        }

        public void layoutIn(Rect drawArea, int refCenter, float scale) {
            // drawArea is where to layout in.
            // refCenter is the absolute horizontal position of the center of drawArea.
            int left = (int) (drawArea.centerX() + (mLeftPosition - refCenter) * scale);
            int top = (int) (drawArea.centerY() - (mView.getMeasuredHeight() / 2) * scale
                    + mOffsetY);
            layoutAt(left, top);
            mView.setScaleX(scale);
            mView.setScaleY(scale);
        }
    }

    public FilmStripView(Context context) {
        super(context);
        init(context);
    }

    public FilmStripView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FilmStripView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mCurrentInfo = (BUFFER_SIZE - 1) / 2;
        setWillNotDraw(false);
        mContext = context;
        mScale = 1.0f;
        mGeometryAnimator = new GeometryAnimator(context);
        mGestureRecognizer =
                new FilmStripGestureRecognizer(context, new MyGestureReceiver());
    }

    public float getScale() {
        return mScale;
    }

    public boolean isAnchoredTo(int id) {
        if (mViewInfo[mCurrentInfo].getID() == id
                && mViewInfo[mCurrentInfo].getCenterX() == mCenterPosition) {
            return true;
        }
        return false;
    }

    public int getCurrentType() {
        ViewInfo curr = mViewInfo[mCurrentInfo];
        if (curr == null) return ImageData.TYPE_NONE;
        return mDataAdapter.getImageData(curr.getID()).getType();
    }

    @Override
    public void onDraw(Canvas c) {
        if (mGeometryAnimator.hasNewGeometry()) {
            layoutChildren();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int boundWidth = MeasureSpec.getSize(widthMeasureSpec);
        int boundHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (mDataAdapter != null) {
            mDataAdapter.suggestSize(boundWidth / 2, boundHeight / 2);
        }

        int wMode = View.MeasureSpec.EXACTLY;
        int hMode = View.MeasureSpec.EXACTLY;

        for (int i = 0; i < mViewInfo.length; i++) {
            ViewInfo info = mViewInfo[i];
            if (mViewInfo[i] == null) continue;

            int imageWidth = mDataAdapter.getImageData(info.getID()).getWidth();
            int imageHeight = mDataAdapter.getImageData(info.getID()).getHeight();
            if (imageWidth == ImageData.SIZE_FULL) imageWidth = boundWidth;
            if (imageHeight == ImageData.SIZE_FULL) imageHeight = boundHeight;

            int scaledWidth = boundWidth;
            int scaledHeight = boundHeight;

            if (imageWidth * scaledHeight > scaledWidth * imageHeight) {
                scaledHeight = imageHeight * scaledWidth / imageWidth;
            } else {
                scaledWidth = imageWidth * scaledHeight / imageHeight;
            }
            scaledWidth += H_PADDING * 2;
            mViewInfo[i].getView().measure(
                    View.MeasureSpec.makeMeasureSpec(scaledWidth, wMode)
                    , View.MeasureSpec.makeMeasureSpec(scaledHeight, hMode));
        }
        setMeasuredDimension(boundWidth, boundHeight);
    }

    private int findTheNearestView(int pointX) {

        int nearest = 0;
        // find the first non-null ViewInfo.
        for (; nearest < BUFFER_SIZE
                && (mViewInfo[nearest] == null || mViewInfo[nearest].getLeftPosition() == -1);
                nearest++);
        // no existing available ViewInfo
        if (nearest == BUFFER_SIZE) return -1;
        int min = Math.abs(pointX - mViewInfo[nearest].getCenterX());

        for (int infoID = nearest + 1;
                infoID < BUFFER_SIZE && mViewInfo[infoID] != null; infoID++) {
            // not measured yet.
            if  (mViewInfo[infoID].getLeftPosition() == -1) continue;

            int c = mViewInfo[infoID].getCenterX();
            int dist = Math.abs(pointX - c);
            if (dist < min) {
                min = dist;
                nearest = infoID;
            }
        }
        return nearest;
    }

    private ViewInfo buildInfoFromData(int dataID) {
        View v = mDataAdapter.getView(mContext, dataID);
        if (v == null) return null;
        v.setPadding(H_PADDING, 0, H_PADDING, 0);
        addView(v);
        return new ViewInfo(dataID, v);
    }

    // We try to keep the one closest to the center of the screen at position mCurrentInfo.
    private void stepIfNeeded() {
        int nearest = findTheNearestView(mCenterPosition);
        // no change made.
        if (nearest == -1 || nearest == mCurrentInfo) return;

        int adjust = nearest - mCurrentInfo;
        if (adjust > 0) {
            for (int k = 0; k < adjust; k++) {
                if (mViewInfo[k] != null) {
                    removeView(mViewInfo[k].getView());
                }
            }
            for (int k = 0; k + adjust < BUFFER_SIZE; k++) {
                mViewInfo[k] = mViewInfo[k + adjust];
            }
            for (int k = BUFFER_SIZE - adjust; k < BUFFER_SIZE; k++) {
                mViewInfo[k] = null;
                if (mViewInfo[k - 1] != null)
                        mViewInfo[k] = buildInfoFromData(mViewInfo[k - 1].getID() + 1);
            }
        } else {
            for (int k = BUFFER_SIZE - 1; k >= BUFFER_SIZE + adjust; k--) {
                if (mViewInfo[k] != null) {
                    removeView(mViewInfo[k].getView());
                }
            }
            for (int k = BUFFER_SIZE - 1; k + adjust >= 0; k--) {
                mViewInfo[k] = mViewInfo[k + adjust];
            }
            for (int k = -1 - adjust; k >= 0; k--) {
                mViewInfo[k] = null;
                if (mViewInfo[k + 1] != null)
                        mViewInfo[k] = buildInfoFromData(mViewInfo[k + 1].getID() - 1);
            }
        }
    }

    // Don't go out of bound.
    private void adjustCenterPosition() {
        ViewInfo curr = mViewInfo[mCurrentInfo];
        if (curr == null) return;

        if (curr.getID() == 0 && mCenterPosition < curr.getCenterX()) {
            mCenterPosition = curr.getCenterX();
            mGeometryAnimator.stopScroll();
        }
        if (curr.getID() == mDataAdapter.getTotalNumber() - 1
                && mCenterPosition > curr.getCenterX()) {
            mCenterPosition = curr.getCenterX();
            mGeometryAnimator.stopScroll();
        }
    }

    private void layoutChildren() {
        if (mGeometryAnimator.hasNewGeometry()) {
            mCenterPosition = mGeometryAnimator.getNewPosition();
            mScale = mGeometryAnimator.getNewScale();
        }

        adjustCenterPosition();

        mViewInfo[mCurrentInfo].layoutIn(mDrawArea, mCenterPosition, mScale);

        // images on the left
        for (int infoID = mCurrentInfo - 1; infoID >= 0; infoID--) {
            ViewInfo curr = mViewInfo[infoID];
            if (curr != null) {
                ViewInfo next = mViewInfo[infoID + 1];
                curr.setLeftPosition(
                        next.getLeftPosition() - curr.getView().getMeasuredWidth());
                curr.layoutIn(mDrawArea, mCenterPosition, mScale);
            }
        }

        // images on the right
        for (int infoID = mCurrentInfo + 1; infoID < BUFFER_SIZE; infoID++) {
            ViewInfo curr = mViewInfo[infoID];
            if (curr != null) {
                ViewInfo prev = mViewInfo[infoID - 1];
                curr.setLeftPosition(
                        prev.getLeftPosition() + prev.getView().getMeasuredWidth());
                curr.layoutIn(mDrawArea, mCenterPosition, mScale);
            }
        }

        stepIfNeeded();
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (mViewInfo[mCurrentInfo] == null) return;

        mDrawArea.left = l;
        mDrawArea.top = t;
        mDrawArea.right = r;
        mDrawArea.bottom = b;

        layoutChildren();
    }

    public void setDataAdapter(DataAdapter adapter) {
        mDataAdapter = adapter;
        mDataAdapter.suggestSize(getMeasuredWidth(), getMeasuredHeight());
        mDataAdapter.setListener(new DataAdapter.Listener() {
            @Override
            public void onDataLoaded() {
                reload();
            }

            @Override
            public void onDataInserted(int dataID) {
            }

            @Override
            public void onDataRemoved(int dataID) {
            }
        });
    }

    public boolean isInCameraFullscreen() {
        return (isAnchoredTo(0) && mScale == 1f
                && getCurrentType() == ImageData.TYPE_CAMERA_PREVIEW);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isInCameraFullscreen()) return false;
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mGestureRecognizer.onTouchEvent(ev);
        return true;
    }

    public void reload() {
        removeAllViews();
        int dataNumber = mDataAdapter.getTotalNumber();
        if (dataNumber == 0) return;

        int currentData = 0;
        int currentLeft = 0;
        // previous data exists.
        if (mViewInfo[mCurrentInfo] != null) {
            currentLeft = mViewInfo[mCurrentInfo].getLeftPosition();
            currentData = mViewInfo[mCurrentInfo].getID();
        }
        mViewInfo[mCurrentInfo] = buildInfoFromData(currentData);
        mViewInfo[mCurrentInfo].setLeftPosition(currentLeft);
        if (getCurrentType() == ImageData.TYPE_CAMERA_PREVIEW
                && currentLeft == 0) {
            // we are in camera mode by default.
            mGeometryAnimator.lockPosition(currentLeft);
        }
        for (int i = 1; mCurrentInfo + i < BUFFER_SIZE || mCurrentInfo - i >= 0; i++) {
            int infoID = mCurrentInfo + i;
            if (infoID < BUFFER_SIZE && mViewInfo[infoID - 1] != null) {
                mViewInfo[infoID] = buildInfoFromData(mViewInfo[infoID - 1].getID() + 1);
            }
            infoID = mCurrentInfo - i;
            if (infoID >= 0 && mViewInfo[infoID + 1] != null) {
                mViewInfo[infoID] = buildInfoFromData(mViewInfo[infoID + 1].getID() - 1);
            }
        }
        layoutChildren();
    }

    // GeometryAnimator controls all the geometry animations. It passively
    // tells the geometry information on demand.
    private class GeometryAnimator implements
            ValueAnimator.AnimatorUpdateListener,
            Animator.AnimatorListener {

        private ValueAnimator mScaleAnimator;
        private boolean mHasNewScale;
        private float mNewScale;

        private Scroller mScroller;
        private boolean mHasNewPosition;
        private DecelerateInterpolator mDecelerateInterpolator;

        private boolean mCanStopScroll;
        private boolean mCanStopScale;

        private boolean mIsPositionLocked;
        private int mLockedPosition;

        private Runnable mPostAction;

        GeometryAnimator(Context context) {
            mScroller = new Scroller(context);
            mHasNewPosition = false;
            mScaleAnimator = new ValueAnimator();
            mScaleAnimator.addUpdateListener(GeometryAnimator.this);
            mScaleAnimator.addListener(GeometryAnimator.this);
            mDecelerateInterpolator = new DecelerateInterpolator();
            mCanStopScroll = true;
            mCanStopScale = true;
            mHasNewScale = false;
        }

        boolean hasNewGeometry() {
            mHasNewPosition = mScroller.computeScrollOffset();
            if (!mHasNewPosition) {
                mCanStopScroll = true;
            }
            // If the position is locked, then we always return true to force
            // the position value to use the locked value.
            return (mHasNewPosition || mHasNewScale || mIsPositionLocked);
        }

        // Always call hasNewGeometry() before getting the new scale value.
        float getNewScale() {
            if (!mHasNewScale) return mScale;
            mHasNewScale = false;
            return mNewScale;
        }

        // Always call hasNewGeometry() before getting the new position value.
        int getNewPosition() {
            if (mIsPositionLocked) return mLockedPosition;
            if (!mHasNewPosition) return mCenterPosition;
            return mScroller.getCurrX();
        }

        void lockPosition(int pos) {
            mIsPositionLocked = true;
            mLockedPosition = pos;
        }

        void unlockPosition() {
            if (mIsPositionLocked) {
                // only when the position is previously locked we set the current
                // position to make it consistent.
                mCenterPosition = mLockedPosition;
                mIsPositionLocked = false;
            }
        }

        void fling(int velocityX, int minX, int maxX) {
            if (!stopScroll() || mIsPositionLocked) return;
            mScroller.fling(mCenterPosition, 0, velocityX, 0, minX, maxX, 0, 0);
        }

        boolean stopScroll() {
            if (!mCanStopScroll) return false;
            mScroller.forceFinished(true);
            mHasNewPosition = false;
            return true;
        }

        boolean stopScale() {
            if (!mCanStopScale) return false;
            mScaleAnimator.cancel();
            mHasNewScale = false;
            return true;
        }

        void stop() {
            stopScroll();
            stopScale();
        }

        void scrollTo(int position, int duration, boolean interruptible) {
            if (!stopScroll() || mIsPositionLocked) return;
            mCanStopScroll = interruptible;
            stopScroll();
            mScroller.startScroll(mCenterPosition, 0, position - mCenterPosition,
                    0, duration);
        }

        void scrollTo(int position, int duration) {
            scrollTo(position, duration, true);
        }

        void scaleTo(float scale, int duration, boolean interruptible) {
            if (!stopScale()) return;
            mCanStopScale = interruptible;
            mScaleAnimator.setDuration(duration);
            mScaleAnimator.setFloatValues(mScale, scale);
            mScaleAnimator.setInterpolator(mDecelerateInterpolator);
            mScaleAnimator.start();
            mHasNewScale = true;
        }

        void scaleTo(float scale, int duration) {
            scaleTo(scale, duration, true);
        }

        void setPostAction(Runnable act) {
            mPostAction = act;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mHasNewScale = true;
            mNewScale = (Float) animation.getAnimatedValue();
            layoutChildren();
        }

        @Override
        public void onAnimationStart(Animator anim) {
        }

        @Override
        public void onAnimationEnd(Animator anim) {
            if (mPostAction != null) {
                mPostAction.run();
                mPostAction = null;
            }
            mCanStopScale = true;
        }

        @Override
        public void onAnimationCancel(Animator anim) {
            mPostAction = null;
        }

        @Override
        public void onAnimationRepeat(Animator anim) {
        }
    }

    private class MyGestureReceiver implements FilmStripGestureRecognizer.Listener {
        // Indicating the current trend of scaling is up (>1) or down (<1).
        private float mScaleTrend;

        @Override
        public boolean onSingleTapUp(float x, float y) {
            return false;
        }

        @Override
        public boolean onDoubleTap(float x, float y) {
            return false;
        }

        @Override
        public boolean onDown(float x, float y) {
            mGeometryAnimator.stop();
            return true;
        }

        @Override
        public boolean onScroll(float x, float y, float dx, float dy) {
            int deltaX = (int) (dx / mScale);
            if (deltaX > 0 && isInCameraFullscreen() ) {
                mGeometryAnimator.unlockPosition();
                mGeometryAnimator.scaleTo(FILM_STRIP_SCALE, DURATION_GEOMETRY_ADJUST, false);
            }

            mCenterPosition += deltaX;

            // Vertical part. Promote or demote.
            int scaledDeltaY = (int) (dy / mScale);

            for (int i = 0; i < BUFFER_SIZE; i++) {
                if (mViewInfo[i] == null) continue;
                Rect hitRect = new Rect();
                View v = mViewInfo[i].getView();
                v.getHitRect(hitRect);
                if (hitRect.contains((int) x, (int) y)) {
                    ImageData data = mDataAdapter.getImageData(mViewInfo[i].getID());
                    if ((data.isActionSupported(ImageData.ACTION_DEMOTE) && dy > 0)
                            || (data.isActionSupported(ImageData.ACTION_PROMOTE) && dy < 0)) {
                        mViewInfo[i].setOffsetY(mViewInfo[i].getOffsetY() - dy);
                    }
                    break;
                }
            }

            layoutChildren();
            return true;
        }

        @Override
        public boolean onFling(float velocityX, float velocityY) {
            float scaledVelocityX = velocityX / mScale;
            if (isInCameraFullscreen() && scaledVelocityX < 0) {
                mGeometryAnimator.unlockPosition();
                mGeometryAnimator.scaleTo(FILM_STRIP_SCALE, DURATION_GEOMETRY_ADJUST, false);
            }
            ViewInfo info = mViewInfo[mCurrentInfo];
            int w = getWidth();
            if (info == null) return true;
            mGeometryAnimator.fling((int) -scaledVelocityX,
                    // estimation of possible length on the left
                    info.getLeftPosition() - info.getID() * w * 2,
                    // estimation of possible length on the right
                    info.getLeftPosition()
                    + (mDataAdapter.getTotalNumber() - info.getID()) * w * 2);
            layoutChildren();
            return true;
        }

        @Override
        public boolean onScaleBegin(float focusX, float focusY) {
            if (isInCameraFullscreen()) return false;
            mScaleTrend = 1f;
            return true;
        }

        @Override
        public boolean onScale(float focusX, float focusY, float scale) {
            if (isInCameraFullscreen()) return false;

            mScaleTrend = mScaleTrend * 0.5f + scale * 0.5f;
            mScale *= scale;
            if (mScale <= FILM_STRIP_SCALE) mScale = FILM_STRIP_SCALE;
            if (mScale >= MAX_SCALE) mScale = MAX_SCALE;
            layoutChildren();
            return true;
        }

        @Override
        public void onScaleEnd() {
            if (mScaleTrend >= 1f) {
                if (mScale != 1f) {
                    mGeometryAnimator.scaleTo(1f, DURATION_GEOMETRY_ADJUST, false);
                }

                if (getCurrentType() == ImageData.TYPE_CAMERA_PREVIEW) {
                    if (isAnchoredTo(0)) {
                        mGeometryAnimator.lockPosition(mViewInfo[mCurrentInfo].getCenterX());
                    } else {
                        mGeometryAnimator.scrollTo(
                                mViewInfo[mCurrentInfo].getCenterX(),
                                DURATION_GEOMETRY_ADJUST, false);
                        mGeometryAnimator.setPostAction(mLockPositionRunnable);
                    }
                }
            } else {
                // Scale down to film strip mode.
                if (mScale == FILM_STRIP_SCALE) {
                    mGeometryAnimator.unlockPosition();
                    return;
                }
                mGeometryAnimator.scaleTo(FILM_STRIP_SCALE, DURATION_GEOMETRY_ADJUST, false);
                mGeometryAnimator.setPostAction(mUnlockPositionRunnable);
            }
        }

        private Runnable mLockPositionRunnable = new Runnable() {
            @Override
            public void run() {
                mGeometryAnimator.lockPosition(mViewInfo[mCurrentInfo].getCenterX());
            }
        };

        private Runnable mUnlockPositionRunnable = new Runnable() {
            @Override
            public void run() {
                mGeometryAnimator.unlockPosition();
            }
        };
    }
}