blob: cf41d3a3033d725ddb92cf23db29f7dbc2fc8e3b [file] [log] [blame]
Chih-Chung Changec412542011-09-26 17:34:06 +08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.ui;
18
Owen Lin2b3ee0e2012-03-14 17:27:24 +080019import android.content.Context;
Yuli Huang04ac0452012-03-20 16:37:05 +080020import android.graphics.Rect;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080021import android.util.Log;
Chih-Chung Changd8488622012-04-17 12:56:08 +080022import android.widget.OverScroller;
Owen Lin2b3ee0e2012-03-14 17:27:24 +080023
Chih-Chung Changec412542011-09-26 17:34:06 +080024import com.android.gallery3d.common.Utils;
Chih-Chung Chang8f568da2012-01-05 12:00:53 +080025import com.android.gallery3d.util.GalleryUtils;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080026import com.android.gallery3d.util.RangeArray;
27import com.android.gallery3d.util.RangeIntArray;
Chih-Chung Changec412542011-09-26 17:34:06 +080028
Chih-Chung Changec412542011-09-26 17:34:06 +080029class PositionController {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080030 private static final String TAG = "PositionController";
31
Yuli Huang2ce3c3b2012-02-23 22:26:12 +080032 public static final int IMAGE_AT_LEFT_EDGE = 1;
33 public static final int IMAGE_AT_RIGHT_EDGE = 2;
34 public static final int IMAGE_AT_TOP_EDGE = 4;
35 public static final int IMAGE_AT_BOTTOM_EDGE = 8;
36
Chih-Chung Changc4791b72012-05-18 19:10:36 -070037 public static final int CAPTURE_ANIMATION_TIME = 700;
Chih-Chung Changf5ce6ae2012-05-11 17:55:02 +080038
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080039 // Special values for animation time.
Chih-Chung Changec412542011-09-26 17:34:06 +080040 private static final long NO_ANIMATION = -1;
41 private static final long LAST_ANIMATION = -2;
42
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080043 private static final int ANIM_KIND_SCROLL = 0;
44 private static final int ANIM_KIND_SCALE = 1;
45 private static final int ANIM_KIND_SNAPBACK = 2;
46 private static final int ANIM_KIND_SLIDE = 3;
47 private static final int ANIM_KIND_ZOOM = 4;
48 private static final int ANIM_KIND_OPENING = 5;
49 private static final int ANIM_KIND_FLING = 6;
Chih-Chung Chang2c617382012-04-20 20:06:19 +080050 private static final int ANIM_KIND_CAPTURE = 7;
Chih-Chung Changec412542011-09-26 17:34:06 +080051
Chih-Chung Chang676170e2011-09-30 18:33:17 +080052 // Animation time in milliseconds. The order must match ANIM_KIND_* above.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080053 private static final int ANIM_TIME[] = {
Chih-Chung Chang676170e2011-09-30 18:33:17 +080054 0, // ANIM_KIND_SCROLL
55 50, // ANIM_KIND_SCALE
56 600, // ANIM_KIND_SNAPBACK
57 400, // ANIM_KIND_SLIDE
58 300, // ANIM_KIND_ZOOM
Yuli Huang5338d192012-05-17 11:32:15 +080059 400, // ANIM_KIND_OPENING
Chih-Chung Changb3aab902011-10-03 21:11:39 +080060 0, // ANIM_KIND_FLING (the duration is calculated dynamically)
Chih-Chung Changf5ce6ae2012-05-11 17:55:02 +080061 CAPTURE_ANIMATION_TIME, // ANIM_KIND_CAPTURE
Chih-Chung Chang676170e2011-09-30 18:33:17 +080062 };
63
Chih-Chung Changec412542011-09-26 17:34:06 +080064 // We try to scale up the image to fill the screen. But in order not to
65 // scale too much for small icons, we limit the max up-scaling factor here.
66 private static final float SCALE_LIMIT = 4;
67
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080068 // For user's gestures, we give a temporary extra scaling range which goes
69 // above or below the usual scaling limits.
70 private static final float SCALE_MIN_EXTRA = 0.7f;
Chih-Chung Chang534b12f2012-03-21 19:01:30 +080071 private static final float SCALE_MAX_EXTRA = 1.4f;
72
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080073 // Setting this true makes the extra scaling range permanent (until this is
74 // set to false again).
Chih-Chung Chang534b12f2012-03-21 19:01:30 +080075 private boolean mExtraScalingRange = false;
Chih-Chung Chang676170e2011-09-30 18:33:17 +080076
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080077 // Film Mode v.s. Page Mode: in film mode we show smaller pictures.
78 private boolean mFilmMode = false;
Chih-Chung Changb3aab902011-10-03 21:11:39 +080079
Chih-Chung Chang642561d2012-04-16 16:29:13 +080080 // These are the limits for width / height of the picture in film mode.
81 private static final float FILM_MODE_PORTRAIT_HEIGHT = 0.48f;
82 private static final float FILM_MODE_PORTRAIT_WIDTH = 0.7f;
83 private static final float FILM_MODE_LANDSCAPE_HEIGHT = 0.7f;
84 private static final float FILM_MODE_LANDSCAPE_WIDTH = 0.7f;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080085
86 // In addition to the focused box (index == 0). We also keep information
87 // about this many boxes on each side.
88 private static final int BOX_MAX = PhotoView.SCREEN_NAIL_MAX;
89
Chih-Chung Changfb1a1552012-04-19 13:34:48 +080090 private static final int IMAGE_GAP = GalleryUtils.dpToPixel(16);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080091 private static final int HORIZONTAL_SLACK = GalleryUtils.dpToPixel(12);
92
93 private Listener mListener;
94 private volatile Rect mOpenAnimationRect;
Chih-Chung Changba12eae2012-05-07 02:29:37 +080095
96 // Use a large enough value, so we won't see the gray shadown in the beginning.
97 private int mViewW = 1200;
98 private int mViewH = 1200;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +080099
100 // A scaling guesture is in progress.
101 private boolean mInScale;
102 // The focus point of the scaling gesture, relative to the center of the
103 // picture in bitmap pixels.
104 private float mFocusX, mFocusY;
105
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800106 // whether there is a previous/next picture.
107 private boolean mHasPrev, mHasNext;
108
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800109 // This is used by the fling animation (page mode).
110 private FlingScroller mPageScroller;
111
112 // This is used by the fling animation (film mode).
Chih-Chung Changd8488622012-04-17 12:56:08 +0800113 private OverScroller mFilmScroller;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800114
115 // The bound of the stable region that the focused box can stay, see the
116 // comments above calculateStableBound() for details.
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800117 private int mBoundLeft, mBoundRight, mBoundTop, mBoundBottom;
118
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800119 // Constrained frame is a rectangle that the focused box should fit into if
120 // it is constrained. It has two effects:
121 //
122 // (1) In page mode, if the focused box is constrained, scaling for the
123 // focused box is adjusted to fit into the constrained frame, instead of the
124 // whole view.
125 //
126 // (2) In page mode, if the focused box is constrained, the mPlatform's
127 // default center (mDefaultX/Y) is moved to the center of the constrained
128 // frame, instead of the view center.
129 //
130 private Rect mConstrainedFrame = new Rect();
131
132 // Whether the focused box is constrained.
133 //
134 // Our current program's first call to moveBox() sets constrained = true, so
135 // we set the initial value of this variable to true, and we will not see
136 // see unwanted transition animation.
137 private boolean mConstrained = true;
138
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800139 //
140 // ___________________________________________________________
141 // | _____ _____ _____ _____ _____ |
142 // | | | | | | | | | | | |
143 // | | Box | | Box | | Box*| | Box | | Box | |
144 // | |_____|.....|_____|.....|_____|.....|_____|.....|_____| |
145 // | Gap Gap Gap Gap |
146 // |___________________________________________________________|
147 //
148 // <-- Platform -->
149 //
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800150 // The focused box (Box*) centers at mPlatform's (mCurrentX, mCurrentY)
Chih-Chung Changec412542011-09-26 17:34:06 +0800151
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800152 private Platform mPlatform = new Platform();
153 private RangeArray<Box> mBoxes = new RangeArray<Box>(-BOX_MAX, BOX_MAX);
154 // The gap at the right of a Box i is at index i. The gap at the left of a
155 // Box i is at index i - 1.
156 private RangeArray<Gap> mGaps = new RangeArray<Gap>(-BOX_MAX, BOX_MAX - 1);
Chih-Chung Changba12eae2012-05-07 02:29:37 +0800157 private FilmRatio mFilmRatio = new FilmRatio();
Chih-Chung Changec412542011-09-26 17:34:06 +0800158
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800159 // These are only used during moveBox().
160 private RangeArray<Box> mTempBoxes = new RangeArray<Box>(-BOX_MAX, BOX_MAX);
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800161 private RangeArray<Gap> mTempGaps =
162 new RangeArray<Gap>(-BOX_MAX, BOX_MAX - 1);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800163
164 // The output of the PositionController. Available throught getPosition().
165 private RangeArray<Rect> mRects = new RangeArray<Rect>(-BOX_MAX, BOX_MAX);
166
167 public interface Listener {
168 void invalidate();
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800169 boolean isHolding();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800170
171 // EdgeView
172 void onPull(int offset, int direction);
173 void onRelease();
174 void onAbsorb(int velocity, int direction);
Chih-Chung Changec412542011-09-26 17:34:06 +0800175 }
176
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800177 public PositionController(Context context, Listener listener) {
178 mListener = listener;
179 mPageScroller = new FlingScroller();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800180 mFilmScroller = new OverScroller(context,
181 null /* default interpolator */, false /* no flywheel */);
Chih-Chung Changec412542011-09-26 17:34:06 +0800182
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800183 // Initialize the areas.
184 initPlatform();
185 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
186 mBoxes.put(i, new Box());
187 initBox(i);
188 mRects.put(i, new Rect());
189 }
190 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
191 mGaps.put(i, new Gap());
192 initGap(i);
193 }
194 }
195
196 public void setOpenAnimationRect(Rect r) {
197 mOpenAnimationRect = r;
198 }
199
200 public void setViewSize(int viewW, int viewH) {
201 if (viewW == mViewW && viewH == mViewH) return;
202
203 mViewW = viewW;
204 mViewH = viewH;
205 initPlatform();
206
207 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
208 setBoxSize(i, viewW, viewH, true);
209 }
210
211 updateScaleAndGapLimit();
Chih-Chung Change6251df2012-05-22 11:35:46 -0700212
213 // If we have the opening animation, do it. Otherwise go directly to the
214 // right position.
215 if (!startOpeningAnimationIfNeeded()) {
216 snapAndRedraw();
217 skipAnimation();
218 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800219 }
220
Chih-Chung Chang9f44f352012-05-29 15:28:36 -0700221 public void setConstrainedFrame(Rect cFrame) {
222 if (mConstrainedFrame.equals(cFrame)) return;
223 mConstrainedFrame.set(cFrame);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800224 mPlatform.updateDefaultXY();
225 updateScaleAndGapLimit();
226 snapAndRedraw();
227 }
228
Chih-Chung Chang9f44f352012-05-29 15:28:36 -0700229 public void forceImageSize(int index, int width, int height) {
230 if (width == 0 || height == 0) return;
231 Box b = mBoxes.get(index);
232 b.mImageW = width;
233 b.mImageH = height;
234 return;
235 }
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800236
Chih-Chung Chang9f44f352012-05-29 15:28:36 -0700237 public void setImageSize(int index, int width, int height, Rect cFrame) {
238 if (width == 0 || height == 0) return;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800239
Chih-Chung Chang9f44f352012-05-29 15:28:36 -0700240 boolean needUpdate = false;
241 if (cFrame != null && !mConstrainedFrame.equals(cFrame)) {
242 mConstrainedFrame.set(cFrame);
243 mPlatform.updateDefaultXY();
244 needUpdate = true;
245 }
246 needUpdate |= setBoxSize(index, width, height, false);
247
248 if (!needUpdate) return;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800249 updateScaleAndGapLimit();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800250 snapAndRedraw();
251 }
252
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800253 // Returns false if the box size doesn't change.
254 private boolean setBoxSize(int i, int width, int height, boolean isViewSize) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800255 Box b = mBoxes.get(i);
Chih-Chung Changd8488622012-04-17 12:56:08 +0800256 boolean wasViewSize = b.mUseViewSize;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800257
Chih-Chung Changd8488622012-04-17 12:56:08 +0800258 // If we already have an image size, we don't want to use the view size.
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800259 if (!wasViewSize && isViewSize) return false;
Chih-Chung Changd8488622012-04-17 12:56:08 +0800260
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800261 b.mUseViewSize = isViewSize;
262
263 if (width == b.mImageW && height == b.mImageH) {
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800264 return false;
Chih-Chung Changec412542011-09-26 17:34:06 +0800265 }
266
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800267 // The ratio of the old size and the new size.
Chih-Chung Chang9f44f352012-05-29 15:28:36 -0700268 //
269 // If the aspect ratio changes, we don't know if it is because one side
270 // grows or the other side shrinks. Currently we just assume the view
271 // angle of the longer side doesn't change (so the aspect ratio change
272 // is because the view angle of the shorter side changes). This matches
273 // what camera preview does.
274 float ratio = (width > height)
275 ? (float) b.mImageW / width
276 : (float) b.mImageH / height;
Chih-Chung Changec412542011-09-26 17:34:06 +0800277
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800278 b.mImageW = width;
279 b.mImageH = height;
280
Chih-Chung Changd8488622012-04-17 12:56:08 +0800281 // If this is the first time we receive an image size, we change the
282 // scale directly. Otherwise adjust the scales by a ratio, and snapback
283 // will animate the scale into the min/max bounds if necessary.
284 if (wasViewSize && !isViewSize) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800285 b.mCurrentScale = getMinimalScale(b);
Chih-Chung Changd8488622012-04-17 12:56:08 +0800286 b.mAnimationStartTime = NO_ANIMATION;
287 } else {
288 b.mCurrentScale *= ratio;
289 b.mFromScale *= ratio;
290 b.mToScale *= ratio;
291 }
Chih-Chung Changec412542011-09-26 17:34:06 +0800292
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800293 if (i == 0) {
294 mFocusX /= ratio;
295 mFocusY /= ratio;
Chih-Chung Changec412542011-09-26 17:34:06 +0800296 }
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800297
298 return true;
Chih-Chung Changec412542011-09-26 17:34:06 +0800299 }
300
Chih-Chung Change6251df2012-05-22 11:35:46 -0700301 private boolean startOpeningAnimationIfNeeded() {
302 if (mOpenAnimationRect == null) return false;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800303 Box b = mBoxes.get(0);
Chih-Chung Change6251df2012-05-22 11:35:46 -0700304 if (b.mUseViewSize) return false;
Chih-Chung Chang676170e2011-09-30 18:33:17 +0800305
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800306 // Start animation from the saved rectangle if we have one.
307 Rect r = mOpenAnimationRect;
308 mOpenAnimationRect = null;
Yuli Huangf320b842012-05-16 01:38:06 +0800309
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800310 mPlatform.mCurrentX = r.centerX() - mViewW / 2;
311 b.mCurrentY = r.centerY() - mViewH / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800312 b.mCurrentScale = Math.max(r.width() / (float) b.mImageW,
313 r.height() / (float) b.mImageH);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800314 startAnimation(mPlatform.mDefaultX, 0, b.mScaleMin,
315 ANIM_KIND_OPENING);
Yuli Huangf320b842012-05-16 01:38:06 +0800316
317 // Animate from large gaps for neighbor boxes to avoid them
318 // shown on the screen during opening animation.
319 for (int i = -1; i < 1; i++) {
320 Gap g = mGaps.get(i);
321 g.mCurrentGap = mViewW;
322 g.doAnimation(g.mDefaultSize, ANIM_KIND_OPENING);
323 }
Chih-Chung Change6251df2012-05-22 11:35:46 -0700324
325 return true;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800326 }
327
328 public void setFilmMode(boolean enabled) {
329 if (enabled == mFilmMode) return;
330 mFilmMode = enabled;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800331
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800332 mPlatform.updateDefaultXY();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800333 updateScaleAndGapLimit();
334 stopAnimation();
335 snapAndRedraw();
336 }
337
338 public void setExtraScalingRange(boolean enabled) {
339 if (mExtraScalingRange == enabled) return;
340 mExtraScalingRange = enabled;
341 if (!enabled) {
342 snapAndRedraw();
343 }
344 }
345
346 // This should be called whenever the scale range of boxes or the default
347 // gap size may change. Currently this can happen due to change of view
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800348 // size, image size, mFilmMode, mConstrained, and mConstrainedFrame.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800349 private void updateScaleAndGapLimit() {
350 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
351 Box b = mBoxes.get(i);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800352 b.mScaleMin = getMinimalScale(b);
353 b.mScaleMax = getMaximalScale(b);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800354 }
355
356 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
357 Gap g = mGaps.get(i);
358 g.mDefaultSize = getDefaultGapSize(i);
359 }
360 }
361
362 // Returns the default gap size according the the size of the boxes around
363 // the gap and the current mode.
364 private int getDefaultGapSize(int i) {
365 if (mFilmMode) return IMAGE_GAP;
366 Box a = mBoxes.get(i);
367 Box b = mBoxes.get(i + 1);
368 return IMAGE_GAP + Math.max(gapToSide(a), gapToSide(b));
369 }
370
371 // Here is how we layout the boxes in the page mode.
372 //
373 // previous current next
374 // ___________ ________________ __________
375 // | _______ | | __________ | | ______ |
376 // | | | | | | right->| | | | | |
377 // | | |<-------->|<--left | | | | | |
378 // | |_______| | | | |__________| | | |______| |
379 // |___________| | |________________| |__________|
380 // | <--> gapToSide()
381 // |
382 // IMAGE_GAP + MAX(gapToSide(previous), gapToSide(current))
383 private int gapToSide(Box b) {
384 return (int) ((mViewW - getMinimalScale(b) * b.mImageW) / 2 + 0.5f);
385 }
386
387 // Stop all animations at where they are now.
388 public void stopAnimation() {
389 mPlatform.mAnimationStartTime = NO_ANIMATION;
390 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
391 mBoxes.get(i).mAnimationStartTime = NO_ANIMATION;
392 }
393 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
394 mGaps.get(i).mAnimationStartTime = NO_ANIMATION;
395 }
396 }
397
398 public void skipAnimation() {
399 if (mPlatform.mAnimationStartTime != NO_ANIMATION) {
400 mPlatform.mCurrentX = mPlatform.mToX;
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800401 mPlatform.mCurrentY = mPlatform.mToY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800402 mPlatform.mAnimationStartTime = NO_ANIMATION;
403 }
404 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
405 Box b = mBoxes.get(i);
406 if (b.mAnimationStartTime == NO_ANIMATION) continue;
407 b.mCurrentY = b.mToY;
408 b.mCurrentScale = b.mToScale;
409 b.mAnimationStartTime = NO_ANIMATION;
410 }
411 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
412 Gap g = mGaps.get(i);
413 if (g.mAnimationStartTime == NO_ANIMATION) continue;
414 g.mCurrentGap = g.mToGap;
415 g.mAnimationStartTime = NO_ANIMATION;
416 }
417 redraw();
418 }
419
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800420 public void snapback() {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800421 snapAndRedraw();
422 }
423
424 ////////////////////////////////////////////////////////////////////////////
425 // Start an animations for the focused box
426 ////////////////////////////////////////////////////////////////////////////
427
428 public void zoomIn(float tapX, float tapY, float targetScale) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800429 tapX -= mViewW / 2;
430 tapY -= mViewH / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800431 Box b = mBoxes.get(0);
432
433 // Convert the tap position to distance to center in bitmap coordinates
434 float tempX = (tapX - mPlatform.mCurrentX) / b.mCurrentScale;
435 float tempY = (tapY - b.mCurrentY) / b.mCurrentScale;
436
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800437 int x = (int) (-tempX * targetScale + 0.5f);
438 int y = (int) (-tempY * targetScale + 0.5f);
Chih-Chung Changec412542011-09-26 17:34:06 +0800439
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800440 calculateStableBound(targetScale);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800441 int targetX = Utils.clamp(x, mBoundLeft, mBoundRight);
442 int targetY = Utils.clamp(y, mBoundTop, mBoundBottom);
443 targetScale = Utils.clamp(targetScale, b.mScaleMin, b.mScaleMax);
Chih-Chung Changec412542011-09-26 17:34:06 +0800444
445 startAnimation(targetX, targetY, targetScale, ANIM_KIND_ZOOM);
446 }
447
448 public void resetToFullView() {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800449 Box b = mBoxes.get(0);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800450 startAnimation(mPlatform.mDefaultX, 0, b.mScaleMin, ANIM_KIND_ZOOM);
Chih-Chung Changec412542011-09-26 17:34:06 +0800451 }
452
Chih-Chung Changec412542011-09-26 17:34:06 +0800453 public void beginScale(float focusX, float focusY) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800454 focusX -= mViewW / 2;
455 focusY -= mViewH / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800456 Box b = mBoxes.get(0);
457 Platform p = mPlatform;
Chih-Chung Changec412542011-09-26 17:34:06 +0800458 mInScale = true;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800459 mFocusX = (int) ((focusX - p.mCurrentX) / b.mCurrentScale + 0.5f);
460 mFocusY = (int) ((focusY - b.mCurrentY) / b.mCurrentScale + 0.5f);
Chih-Chung Changec412542011-09-26 17:34:06 +0800461 }
462
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800463 // Scales the image by the given factor.
464 // Returns an out-of-range indicator:
465 // 1 if the intended scale is too large for the stable range.
466 // 0 if the intended scale is in the stable range.
467 // -1 if the intended scale is too small for the stable range.
468 public int scaleBy(float s, float focusX, float focusY) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800469 focusX -= mViewW / 2;
470 focusY -= mViewH / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800471 Box b = mBoxes.get(0);
472 Platform p = mPlatform;
Chih-Chung Changec412542011-09-26 17:34:06 +0800473
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800474 // We want to keep the focus point (on the bitmap) the same as when we
475 // begin the scale guesture, that is,
Chih-Chung Chang676170e2011-09-30 18:33:17 +0800476 //
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800477 // (focusX' - currentX') / scale' = (focusX - currentX) / scale
Chih-Chung Chang676170e2011-09-30 18:33:17 +0800478 //
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800479 s *= getTargetScale(b);
480 int x = mFilmMode ? p.mCurrentX : (int) (focusX - s * mFocusX + 0.5f);
481 int y = mFilmMode ? b.mCurrentY : (int) (focusY - s * mFocusY + 0.5f);
Chih-Chung Chang676170e2011-09-30 18:33:17 +0800482 startAnimation(x, y, s, ANIM_KIND_SCALE);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800483 if (s < b.mScaleMin) return -1;
484 if (s > b.mScaleMax) return 1;
485 return 0;
Chih-Chung Changec412542011-09-26 17:34:06 +0800486 }
487
488 public void endScale() {
489 mInScale = false;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800490 snapAndRedraw();
Chih-Chung Changec412542011-09-26 17:34:06 +0800491 }
492
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800493 // Slide the focused box to the center of the view.
494 public void startHorizontalSlide() {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800495 Box b = mBoxes.get(0);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800496 startAnimation(mPlatform.mDefaultX, 0, b.mScaleMin, ANIM_KIND_SLIDE);
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800497 }
498
499 // Slide the focused box to the center of the view with the capture
500 // animation. In addition to the sliding, the animation will also scale the
501 // the focused box, the specified neighbor box, and the gap between the
502 // two. The specified offset should be 1 or -1.
503 public void startCaptureAnimationSlide(int offset) {
504 Box b = mBoxes.get(0);
505 Box n = mBoxes.get(offset); // the neighbor box
506 Gap g = mGaps.get(offset); // the gap between the two boxes
507
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800508 mPlatform.doAnimation(mPlatform.mDefaultX, mPlatform.mDefaultY,
509 ANIM_KIND_CAPTURE);
510 b.doAnimation(0, b.mScaleMin, ANIM_KIND_CAPTURE);
511 n.doAnimation(0, n.mScaleMin, ANIM_KIND_CAPTURE);
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800512 g.doAnimation(g.mDefaultSize, ANIM_KIND_CAPTURE);
513 redraw();
Chih-Chung Changec412542011-09-26 17:34:06 +0800514 }
515
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800516 public void startScroll(float dx, float dy) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800517 Box b = mBoxes.get(0);
518 Platform p = mPlatform;
519
520 int x = getTargetX(p) + (int) (dx + 0.5f);
521 int y = getTargetY(b) + (int) (dy + 0.5f);
522
523 if (mFilmMode) {
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800524 scrollToFilm(x, y);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800525 } else {
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800526 scrollToPage(x, y);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800527 }
Chih-Chung Chang676170e2011-09-30 18:33:17 +0800528 }
529
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800530 private void scrollToPage(int x, int y) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800531 Box b = mBoxes.get(0);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800532
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800533 calculateStableBound(b.mCurrentScale);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800534
535 // Vertical direction: If we have space to move in the vertical
536 // direction, we show the edge effect when scrolling reaches the edge.
537 if (mBoundTop != mBoundBottom) {
538 if (y < mBoundTop) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800539 mListener.onPull(mBoundTop - y, EdgeView.BOTTOM);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800540 } else if (y > mBoundBottom) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800541 mListener.onPull(y - mBoundBottom, EdgeView.TOP);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800542 }
543 }
544
545 y = Utils.clamp(y, mBoundTop, mBoundBottom);
546
547 // Horizontal direction: we show the edge effect when the scrolling
548 // tries to go left of the first image or go right of the last image.
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800549 if (!mHasPrev && x > mBoundRight) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800550 int pixels = x - mBoundRight;
551 mListener.onPull(pixels, EdgeView.LEFT);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800552 x = mBoundRight;
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800553 } else if (!mHasNext && x < mBoundLeft) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800554 int pixels = mBoundLeft - x;
555 mListener.onPull(pixels, EdgeView.RIGHT);
556 x = mBoundLeft;
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800557 }
558
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800559 startAnimation(x, y, b.mCurrentScale, ANIM_KIND_SCROLL);
560 }
561
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800562 private void scrollToFilm(int x, int y) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800563 Box b = mBoxes.get(0);
564
565 // Horizontal direction: we show the edge effect when the scrolling
566 // tries to go left of the first image or go right of the last image.
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800567 x -= mPlatform.mDefaultX;
568 if (!mHasPrev && x > 0) {
569 mListener.onPull(x, EdgeView.LEFT);
570 x = 0;
571 } else if (!mHasNext && x < 0) {
572 mListener.onPull(-x, EdgeView.RIGHT);
573 x = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800574 }
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800575 x += mPlatform.mDefaultX;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800576 startAnimation(x, y, b.mCurrentScale, ANIM_KIND_SCROLL);
Chih-Chung Chang532d93c2011-10-12 17:10:33 +0800577 }
578
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800579 public boolean fling(float velocityX, float velocityY) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800580 int vx = (int) (velocityX + 0.5f);
581 int vy = (int) (velocityY + 0.5f);
582 return mFilmMode ? flingFilm(vx, vy) : flingPage(vx, vy);
583 }
584
585 private boolean flingPage(int velocityX, int velocityY) {
586 Box b = mBoxes.get(0);
587 Platform p = mPlatform;
588
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800589 // We only want to do fling when the picture is zoomed-in.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800590 if (viewWiderThanScaledImage(b.mCurrentScale) &&
591 viewTallerThanScaledImage(b.mCurrentScale)) {
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800592 return false;
593 }
594
Yuli Huang2ce3c3b2012-02-23 22:26:12 +0800595 // We only allow flinging in the directions where it won't go over the
596 // picture.
597 int edges = getImageAtEdges();
598 if ((velocityX > 0 && (edges & IMAGE_AT_LEFT_EDGE) != 0) ||
599 (velocityX < 0 && (edges & IMAGE_AT_RIGHT_EDGE) != 0)) {
600 velocityX = 0;
601 }
602 if ((velocityY > 0 && (edges & IMAGE_AT_TOP_EDGE) != 0) ||
603 (velocityY < 0 && (edges & IMAGE_AT_BOTTOM_EDGE) != 0)) {
604 velocityY = 0;
605 }
Yuli Huang2ce3c3b2012-02-23 22:26:12 +0800606
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800607 if (velocityX == 0 && velocityY == 0) return false;
608
609 mPageScroller.fling(p.mCurrentX, b.mCurrentY, velocityX, velocityY,
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800610 mBoundLeft, mBoundRight, mBoundTop, mBoundBottom);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800611 int targetX = mPageScroller.getFinalX();
612 int targetY = mPageScroller.getFinalY();
613 ANIM_TIME[ANIM_KIND_FLING] = mPageScroller.getDuration();
614 startAnimation(targetX, targetY, b.mCurrentScale, ANIM_KIND_FLING);
Chih-Chung Changb3aab902011-10-03 21:11:39 +0800615 return true;
616 }
617
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800618 private boolean flingFilm(int velocityX, int velocityY) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800619 Box b = mBoxes.get(0);
620 Platform p = mPlatform;
Chih-Chung Changec412542011-09-26 17:34:06 +0800621
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800622 // If we are already at the edge, don't start the fling.
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800623 int defaultX = p.mDefaultX;
624 if ((!mHasPrev && p.mCurrentX >= defaultX)
625 || (!mHasNext && p.mCurrentX <= defaultX)) {
Chih-Chung Changec412542011-09-26 17:34:06 +0800626 return false;
627 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800628
629 if (velocityX == 0) return false;
630
631 mFilmScroller.fling(p.mCurrentX, 0, velocityX, 0,
632 Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
633 int targetX = mFilmScroller.getFinalX();
Chih-Chung Changc3b2d472012-04-19 20:14:11 +0800634 // This value doesn't matter because we use mFilmScroller.isFinished()
635 // to decide when to stop. We set this to 0 so it's faster for
636 // Animatable.advanceAnimation() to calculate the progress (always 1).
637 ANIM_TIME[ANIM_KIND_FLING] = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800638 startAnimation(targetX, b.mCurrentY, b.mCurrentScale, ANIM_KIND_FLING);
639 return true;
Chih-Chung Changec412542011-09-26 17:34:06 +0800640 }
641
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800642 ////////////////////////////////////////////////////////////////////////////
643 // Redraw
644 //
645 // If a method changes box positions directly, redraw()
646 // should be called.
647 //
648 // If a method may also cause a snapback to happen, snapAndRedraw() should
649 // be called.
650 //
651 // If a method starts an animation to change the position of focused box,
652 // startAnimation() should be called.
653 //
654 // If time advances to change the box position, advanceAnimation() should
655 // be called.
656 ////////////////////////////////////////////////////////////////////////////
657 private void redraw() {
658 layoutAndSetPosition();
659 mListener.invalidate();
660 }
Chih-Chung Changec412542011-09-26 17:34:06 +0800661
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800662 private void snapAndRedraw() {
663 mPlatform.startSnapback();
664 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
665 mBoxes.get(i).startSnapback();
666 }
667 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
668 mGaps.get(i).startSnapback();
669 }
Chih-Chung Changba12eae2012-05-07 02:29:37 +0800670 mFilmRatio.startSnapback();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800671 redraw();
672 }
Chih-Chung Chang534b12f2012-03-21 19:01:30 +0800673
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800674 private void startAnimation(int targetX, int targetY, float targetScale,
675 int kind) {
676 boolean changed = false;
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800677 changed |= mPlatform.doAnimation(targetX, mPlatform.mDefaultY, kind);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800678 changed |= mBoxes.get(0).doAnimation(targetY, targetScale, kind);
679 if (changed) redraw();
680 }
681
Chih-Chung Changb8be1e02012-04-17 20:35:14 +0800682 public void advanceAnimation() {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800683 boolean changed = false;
684 changed |= mPlatform.advanceAnimation();
685 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
686 changed |= mBoxes.get(i).advanceAnimation();
687 }
688 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
689 changed |= mGaps.get(i).advanceAnimation();
690 }
Chih-Chung Changba12eae2012-05-07 02:29:37 +0800691 changed |= mFilmRatio.advanceAnimation();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800692 if (changed) redraw();
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800693 }
694
Yuli Huangf320b842012-05-16 01:38:06 +0800695 public boolean inOpeningAnimation() {
696 return (mPlatform.mAnimationKind == ANIM_KIND_OPENING &&
697 mPlatform.mAnimationStartTime != NO_ANIMATION) ||
698 (mBoxes.get(0).mAnimationKind == ANIM_KIND_OPENING &&
699 mBoxes.get(0).mAnimationStartTime != NO_ANIMATION);
700 }
701
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800702 ////////////////////////////////////////////////////////////////////////////
703 // Layout
704 ////////////////////////////////////////////////////////////////////////////
705
706 // Returns the display width of this box.
707 private int widthOf(Box b) {
708 return (int) (b.mImageW * b.mCurrentScale + 0.5f);
709 }
710
711 // Returns the display height of this box.
712 private int heightOf(Box b) {
713 return (int) (b.mImageH * b.mCurrentScale + 0.5f);
714 }
715
716 // Returns the display width of this box, using the given scale.
717 private int widthOf(Box b, float scale) {
718 return (int) (b.mImageW * scale + 0.5f);
719 }
720
721 // Returns the display height of this box, using the given scale.
722 private int heightOf(Box b, float scale) {
723 return (int) (b.mImageH * scale + 0.5f);
724 }
725
726 // Convert the information in mPlatform and mBoxes to mRects, so the user
727 // can get the position of each box by getPosition().
728 //
729 // Note the loop index goes from inside-out because each box's X coordinate
730 // is relative to its anchor box (except the focused box).
731 private void layoutAndSetPosition() {
732 // layout box 0 (focused box)
733 convertBoxToRect(0);
734 for (int i = 1; i <= BOX_MAX; i++) {
735 // layout box i and -i
736 convertBoxToRect(i);
737 convertBoxToRect(-i);
738 }
739 //dumpState();
740 }
741
742 private void dumpState() {
743 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
744 Log.d(TAG, "Gap " + i + ": " + mGaps.get(i).mCurrentGap);
Chih-Chung Changec412542011-09-26 17:34:06 +0800745 }
746
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800747 dumpRect(0);
748 for (int i = 1; i <= BOX_MAX; i++) {
749 dumpRect(i);
750 dumpRect(-i);
Chih-Chung Changec412542011-09-26 17:34:06 +0800751 }
752
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800753 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
754 for (int j = i + 1; j <= BOX_MAX; j++) {
755 if (Rect.intersects(mRects.get(i), mRects.get(j))) {
756 Log.d(TAG, "rect " + i + " and rect " + j + "intersects!");
757 }
758 }
759 }
760 }
761
762 private void dumpRect(int i) {
763 StringBuilder sb = new StringBuilder();
764 Rect r = mRects.get(i);
765 sb.append("Rect " + i + ":");
766 sb.append("(");
767 sb.append(r.centerX());
768 sb.append(",");
769 sb.append(r.centerY());
770 sb.append(") [");
771 sb.append(r.width());
772 sb.append("x");
773 sb.append(r.height());
774 sb.append("]");
775 Log.d(TAG, sb.toString());
776 }
777
778 private void convertBoxToRect(int i) {
779 Box b = mBoxes.get(i);
780 Rect r = mRects.get(i);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800781 int y = b.mCurrentY + mPlatform.mCurrentY + mViewH / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800782 int w = widthOf(b);
783 int h = heightOf(b);
784 if (i == 0) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800785 int x = mPlatform.mCurrentX + mViewW / 2;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800786 r.left = x - w / 2;
787 r.right = r.left + w;
788 } else if (i > 0) {
789 Rect a = mRects.get(i - 1);
790 Gap g = mGaps.get(i - 1);
791 r.left = a.right + g.mCurrentGap;
792 r.right = r.left + w;
793 } else { // i < 0
794 Rect a = mRects.get(i + 1);
795 Gap g = mGaps.get(i);
796 r.right = a.left - g.mCurrentGap;
797 r.left = r.right - w;
798 }
799 r.top = y - h / 2;
800 r.bottom = r.top + h;
801 }
802
803 // Returns the position of a box.
804 public Rect getPosition(int index) {
805 return mRects.get(index);
806 }
807
808 ////////////////////////////////////////////////////////////////////////////
809 // Box management
810 ////////////////////////////////////////////////////////////////////////////
811
812 // Initialize the platform to be at the view center.
813 private void initPlatform() {
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800814 mPlatform.updateDefaultXY();
815 mPlatform.mCurrentX = mPlatform.mDefaultX;
816 mPlatform.mCurrentY = mPlatform.mDefaultY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800817 mPlatform.mAnimationStartTime = NO_ANIMATION;
818 }
819
820 // Initialize a box to have the size of the view.
821 private void initBox(int index) {
822 Box b = mBoxes.get(index);
823 b.mImageW = mViewW;
824 b.mImageH = mViewH;
825 b.mUseViewSize = true;
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800826 b.mScaleMin = getMinimalScale(b);
827 b.mScaleMax = getMaximalScale(b);
828 b.mCurrentY = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800829 b.mCurrentScale = b.mScaleMin;
830 b.mAnimationStartTime = NO_ANIMATION;
831 }
832
833 // Initialize a gap. This can only be called after the boxes around the gap
834 // has been initialized.
835 private void initGap(int index) {
836 Gap g = mGaps.get(index);
837 g.mDefaultSize = getDefaultGapSize(index);
838 g.mCurrentGap = g.mDefaultSize;
839 g.mAnimationStartTime = NO_ANIMATION;
840 }
841
842 private void initGap(int index, int size) {
843 Gap g = mGaps.get(index);
844 g.mDefaultSize = getDefaultGapSize(index);
845 g.mCurrentGap = size;
846 g.mAnimationStartTime = NO_ANIMATION;
847 }
848
849 private void debugMoveBox(int fromIndex[]) {
850 StringBuilder s = new StringBuilder("moveBox:");
851 for (int i = 0; i < fromIndex.length; i++) {
852 int j = fromIndex[i];
853 if (j == Integer.MAX_VALUE) {
854 s.append(" N");
855 } else {
856 s.append(" ");
857 s.append(fromIndex[i]);
858 }
859 }
860 Log.d(TAG, s.toString());
861 }
862
863 // Move the boxes: it may indicate focus change, box deleted, box appearing,
864 // box reordered, etc.
865 //
866 // Each element in the fromIndex array indicates where each box was in the
867 // old array. If the value is Integer.MAX_VALUE (pictured as N below), it
868 // means the box is new.
869 //
870 // For example:
871 // N N N N N N N -- all new boxes
872 // -3 -2 -1 0 1 2 3 -- nothing changed
873 // -2 -1 0 1 2 3 N -- focus goes to the next box
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800874 // N -3 -2 -1 0 1 2 -- focuse goes to the previous box
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800875 // -3 -2 -1 1 2 3 N -- the focused box was deleted.
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800876 //
877 // hasPrev/hasNext indicates if there are previous/next boxes for the
878 // focused box. constrained indicates whether the focused box should be put
879 // into the constrained frame.
880 public void moveBox(int fromIndex[], boolean hasPrev, boolean hasNext,
881 boolean constrained) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800882 //debugMoveBox(fromIndex);
Chih-Chung Changfb1a1552012-04-19 13:34:48 +0800883 mHasPrev = hasPrev;
884 mHasNext = hasNext;
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800885
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800886 RangeIntArray from = new RangeIntArray(fromIndex, -BOX_MAX, BOX_MAX);
887
888 // 1. Get the absolute X coordiates for the boxes.
889 layoutAndSetPosition();
890 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
891 Box b = mBoxes.get(i);
892 Rect r = mRects.get(i);
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800893 b.mAbsoluteX = r.centerX() - mViewW / 2;
Chih-Chung Changec412542011-09-26 17:34:06 +0800894 }
895
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800896 // 2. copy boxes and gaps to temporary storage.
897 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
898 mTempBoxes.put(i, mBoxes.get(i));
899 mBoxes.put(i, null);
900 }
901 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
902 mTempGaps.put(i, mGaps.get(i));
903 mGaps.put(i, null);
904 }
905
906 // 3. move back boxes that are used in the new array.
907 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
908 int j = from.get(i);
909 if (j == Integer.MAX_VALUE) continue;
910 mBoxes.put(i, mTempBoxes.get(j));
911 mTempBoxes.put(j, null);
912 }
913
914 // 4. move back gaps if both boxes around it are kept together.
915 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
916 int j = from.get(i);
917 if (j == Integer.MAX_VALUE) continue;
918 int k = from.get(i + 1);
919 if (k == Integer.MAX_VALUE) continue;
920 if (j + 1 == k) {
921 mGaps.put(i, mTempGaps.get(j));
922 mTempGaps.put(j, null);
923 }
924 }
925
926 // 5. recycle the boxes that are not used in the new array.
927 int k = -BOX_MAX;
928 for (int i = -BOX_MAX; i <= BOX_MAX; i++) {
929 if (mBoxes.get(i) != null) continue;
930 while (mTempBoxes.get(k) == null) {
931 k++;
932 }
933 mBoxes.put(i, mTempBoxes.get(k++));
934 initBox(i);
935 }
936
937 // 6. Now give the recycled box a reasonable absolute X position.
938 //
939 // First try to find the first and the last box which the absolute X
940 // position is known.
941 int first, last;
942 for (first = -BOX_MAX; first <= BOX_MAX; first++) {
943 if (from.get(first) != Integer.MAX_VALUE) break;
944 }
945 for (last = BOX_MAX; last >= -BOX_MAX; last--) {
946 if (from.get(last) != Integer.MAX_VALUE) break;
947 }
948 // If there is no box has known X position at all, make the focused one
949 // as known.
950 if (first > BOX_MAX) {
951 mBoxes.get(0).mAbsoluteX = mPlatform.mCurrentX;
952 first = last = 0;
953 }
954 // Now for those boxes between first and last, just assign the same
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800955 // position as the next box. (We can do better, but this should be
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800956 // rare). For the boxes before first or after last, we will use a new
957 // default gap size below.
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800958 for (int i = last - 1; i > first; i--) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800959 if (from.get(i) != Integer.MAX_VALUE) continue;
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800960 mBoxes.get(i).mAbsoluteX = mBoxes.get(i + 1).mAbsoluteX;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +0800961 }
962
963 // 7. recycle the gaps that are not used in the new array.
964 k = -BOX_MAX;
965 for (int i = -BOX_MAX; i < BOX_MAX; i++) {
966 if (mGaps.get(i) != null) continue;
967 while (mTempGaps.get(k) == null) {
968 k++;
969 }
970 mGaps.put(i, mTempGaps.get(k++));
971 Box a = mBoxes.get(i);
972 Box b = mBoxes.get(i + 1);
973 int wa = widthOf(a);
974 int wb = widthOf(b);
975 if (i >= first && i < last) {
976 int g = b.mAbsoluteX - a.mAbsoluteX - wb / 2 - (wa - wa / 2);
977 initGap(i, g);
978 } else {
979 initGap(i);
980 }
981 }
982
Chih-Chung Changc4791b72012-05-18 19:10:36 -0700983 // 8. calculate the new absolute X coordinates for those box before
984 // first or after last.
985 for (int i = first - 1; i >= -BOX_MAX; i--) {
986 Box a = mBoxes.get(i + 1);
987 Box b = mBoxes.get(i);
988 int wa = widthOf(a);
989 int wb = widthOf(b);
990 Gap g = mGaps.get(i);
991 b.mAbsoluteX = a.mAbsoluteX - wa / 2 - (wb - wb / 2) - g.mCurrentGap;
992 }
993
994 for (int i = last + 1; i <= BOX_MAX; i++) {
995 Box a = mBoxes.get(i - 1);
996 Box b = mBoxes.get(i);
997 int wa = widthOf(a);
998 int wb = widthOf(b);
999 Gap g = mGaps.get(i - 1);
1000 b.mAbsoluteX = a.mAbsoluteX + (wa - wa / 2) + wb / 2 + g.mCurrentGap;
1001 }
1002
1003 // 9. offset the Platform position
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001004 int dx = mBoxes.get(0).mAbsoluteX - mPlatform.mCurrentX;
1005 mPlatform.mCurrentX += dx;
1006 mPlatform.mFromX += dx;
1007 mPlatform.mToX += dx;
1008 mPlatform.mFlingOffset += dx;
1009
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001010 if (mConstrained != constrained) {
1011 mConstrained = constrained;
1012 mPlatform.updateDefaultXY();
1013 updateScaleAndGapLimit();
1014 }
1015
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001016 snapAndRedraw();
1017 }
1018
1019 ////////////////////////////////////////////////////////////////////////////
1020 // Public utilities
1021 ////////////////////////////////////////////////////////////////////////////
1022
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001023 public boolean isAtMinimalScale() {
1024 Box b = mBoxes.get(0);
1025 return isAlmostEqual(b.mCurrentScale, b.mScaleMin);
1026 }
1027
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001028 public boolean isCenter() {
1029 Box b = mBoxes.get(0);
1030 return mPlatform.mCurrentX == mPlatform.mDefaultX
1031 && b.mCurrentY == 0;
1032 }
1033
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001034 public int getImageWidth() {
1035 Box b = mBoxes.get(0);
1036 return b.mImageW;
1037 }
1038
1039 public int getImageHeight() {
1040 Box b = mBoxes.get(0);
1041 return b.mImageH;
1042 }
1043
1044 public float getImageScale() {
1045 Box b = mBoxes.get(0);
1046 return b.mCurrentScale;
1047 }
1048
1049 public int getImageAtEdges() {
1050 Box b = mBoxes.get(0);
1051 Platform p = mPlatform;
1052 calculateStableBound(b.mCurrentScale);
1053 int edges = 0;
1054 if (p.mCurrentX <= mBoundLeft) {
1055 edges |= IMAGE_AT_RIGHT_EDGE;
1056 }
1057 if (p.mCurrentX >= mBoundRight) {
1058 edges |= IMAGE_AT_LEFT_EDGE;
1059 }
1060 if (b.mCurrentY <= mBoundTop) {
1061 edges |= IMAGE_AT_BOTTOM_EDGE;
1062 }
1063 if (b.mCurrentY >= mBoundBottom) {
1064 edges |= IMAGE_AT_TOP_EDGE;
1065 }
1066 return edges;
1067 }
1068
Chih-Chung Chang17ffedd2012-05-03 18:22:59 +08001069 public boolean isScrolling() {
1070 return mPlatform.mAnimationStartTime != NO_ANIMATION
1071 && mPlatform.mCurrentX != mPlatform.mToX;
1072 }
1073
1074 public void stopScrolling() {
1075 if (mPlatform.mAnimationStartTime == NO_ANIMATION) return;
Angus Kong95c2aea2012-05-24 16:20:57 -07001076 if (mFilmMode) mFilmScroller.forceFinished(true);
Chih-Chung Chang17ffedd2012-05-03 18:22:59 +08001077 mPlatform.mFromX = mPlatform.mToX = mPlatform.mCurrentX;
1078 }
1079
Chih-Chung Changba12eae2012-05-07 02:29:37 +08001080 public float getFilmRatio() {
1081 return mFilmRatio.mCurrentRatio;
1082 }
1083
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001084 ////////////////////////////////////////////////////////////////////////////
1085 // Private utilities
1086 ////////////////////////////////////////////////////////////////////////////
1087
1088 private float getMinimalScale(Box b) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001089 float wFactor = 1.0f;
1090 float hFactor = 1.0f;
1091 int viewW, viewH;
1092
Chih-Chung Changb56ff732012-05-01 02:25:09 +08001093 if (!mFilmMode && mConstrained && !mConstrainedFrame.isEmpty()
1094 && b == mBoxes.get(0)) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001095 viewW = mConstrainedFrame.width();
1096 viewH = mConstrainedFrame.height();
1097 } else {
1098 viewW = mViewW;
1099 viewH = mViewH;
1100 }
1101
1102 if (mFilmMode) {
1103 if (mViewH > mViewW) { // portrait
1104 wFactor = FILM_MODE_PORTRAIT_WIDTH;
1105 hFactor = FILM_MODE_PORTRAIT_HEIGHT;
1106 } else { // landscape
1107 wFactor = FILM_MODE_LANDSCAPE_WIDTH;
1108 hFactor = FILM_MODE_LANDSCAPE_HEIGHT;
1109 }
1110 }
1111
1112 float s = Math.min(wFactor * viewW / b.mImageW,
1113 hFactor * viewH / b.mImageH);
1114 return Math.min(SCALE_LIMIT, s);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001115 }
1116
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001117 private float getMaximalScale(Box b) {
Chih-Chung Changba12eae2012-05-07 02:29:37 +08001118 if (mFilmMode) return getMinimalScale(b);
1119 if (mConstrained && !mConstrainedFrame.isEmpty()) return getMinimalScale(b);
1120 return SCALE_LIMIT;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001121 }
1122
1123 private static boolean isAlmostEqual(float a, float b) {
1124 float diff = a - b;
1125 return (diff < 0 ? -diff : diff) < 0.02f;
Chih-Chung Changec412542011-09-26 17:34:06 +08001126 }
1127
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001128 // Calculates the stable region of mPlatform.mCurrentX and
1129 // mBoxes.get(0).mCurrentY, where "stable" means
Chih-Chung Changb3aab902011-10-03 21:11:39 +08001130 //
1131 // (1) If the dimension of scaled image >= view dimension, we will not
1132 // see black region outside the image (at that dimension).
1133 // (2) If the dimension of scaled image < view dimension, we will center
1134 // the scaled image.
1135 //
1136 // We might temporarily go out of this stable during user interaction,
1137 // but will "snap back" after user stops interaction.
1138 //
1139 // The results are stored in mBound{Left/Right/Top/Bottom}.
1140 //
Chih-Chung Chang8f568da2012-01-05 12:00:53 +08001141 // An extra parameter "horizontalSlack" (which has the value of 0 usually)
1142 // is used to extend the stable region by some pixels on each side
1143 // horizontally.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001144 private void calculateStableBound(float scale, int horizontalSlack) {
1145 Box b = mBoxes.get(0);
Chih-Chung Chang8f568da2012-01-05 12:00:53 +08001146
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001147 // The width and height of the box in number of view pixels
1148 int w = widthOf(b, scale);
1149 int h = heightOf(b, scale);
1150
1151 // When the edge of the view is aligned with the edge of the box
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001152 mBoundLeft = (mViewW + 1) / 2 - (w + 1) / 2 - horizontalSlack;
1153 mBoundRight = w / 2 - mViewW / 2 + horizontalSlack;
1154 mBoundTop = (mViewH + 1) / 2 - (h + 1) / 2;
1155 mBoundBottom = h / 2 - mViewH / 2;
Chih-Chung Changb3aab902011-10-03 21:11:39 +08001156
1157 // If the scaled height is smaller than the view height,
1158 // force it to be in the center.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001159 if (viewTallerThanScaledImage(scale)) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001160 mBoundTop = mBoundBottom = 0;
Chih-Chung Changb3aab902011-10-03 21:11:39 +08001161 }
1162
1163 // Same for width
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001164 if (viewWiderThanScaledImage(scale)) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001165 mBoundLeft = mBoundRight = mPlatform.mDefaultX;
Chih-Chung Changb3aab902011-10-03 21:11:39 +08001166 }
1167 }
1168
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001169 private void calculateStableBound(float scale) {
1170 calculateStableBound(scale, 0);
1171 }
1172
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001173 private boolean viewTallerThanScaledImage(float scale) {
1174 return mViewH >= heightOf(mBoxes.get(0), scale);
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001175 }
1176
1177 private boolean viewWiderThanScaledImage(float scale) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001178 return mViewW >= widthOf(mBoxes.get(0), scale);
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001179 }
1180
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001181 private float getTargetScale(Box b) {
1182 return useCurrentValueAsTarget(b) ? b.mCurrentScale : b.mToScale;
Chih-Chung Changb3aab902011-10-03 21:11:39 +08001183 }
1184
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001185 private int getTargetX(Platform p) {
1186 return useCurrentValueAsTarget(p) ? p.mCurrentX : p.mToX;
Chih-Chung Changec412542011-09-26 17:34:06 +08001187 }
1188
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001189 private int getTargetY(Box b) {
1190 return useCurrentValueAsTarget(b) ? b.mCurrentY : b.mToY;
Chih-Chung Changec412542011-09-26 17:34:06 +08001191 }
1192
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001193 private boolean useCurrentValueAsTarget(Animatable a) {
1194 return a.mAnimationStartTime == NO_ANIMATION ||
1195 a.mAnimationKind == ANIM_KIND_SNAPBACK ||
1196 a.mAnimationKind == ANIM_KIND_FLING;
Chih-Chung Changec412542011-09-26 17:34:06 +08001197 }
1198
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001199 ////////////////////////////////////////////////////////////////////////////
1200 // Animatable: an thing which can do animation.
1201 ////////////////////////////////////////////////////////////////////////////
1202 private abstract static class Animatable {
1203 public long mAnimationStartTime;
1204 public int mAnimationKind;
1205 public int mAnimationDuration;
Chih-Chung Changec412542011-09-26 17:34:06 +08001206
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001207 // This should be overidden in subclass to change the animation values
1208 // give the progress value in [0, 1].
1209 protected abstract boolean interpolate(float progress);
1210 public abstract boolean startSnapback();
Chih-Chung Changec412542011-09-26 17:34:06 +08001211
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001212 // Returns true if the animation values changes, so things need to be
1213 // redrawn.
1214 public boolean advanceAnimation() {
1215 if (mAnimationStartTime == NO_ANIMATION) {
1216 return false;
1217 }
1218 if (mAnimationStartTime == LAST_ANIMATION) {
1219 mAnimationStartTime = NO_ANIMATION;
1220 return startSnapback();
1221 }
1222
1223 float progress;
1224 if (mAnimationDuration == 0) {
1225 progress = 1;
1226 } else {
1227 long now = AnimationTime.get();
1228 progress =
1229 (float) (now - mAnimationStartTime) / mAnimationDuration;
1230 }
1231
1232 if (progress >= 1) {
1233 progress = 1;
1234 } else {
1235 progress = applyInterpolationCurve(mAnimationKind, progress);
1236 }
1237
1238 boolean done = interpolate(progress);
1239
1240 if (done) {
1241 mAnimationStartTime = LAST_ANIMATION;
1242 }
1243
1244 return true;
Chih-Chung Changec412542011-09-26 17:34:06 +08001245 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001246
1247 private static float applyInterpolationCurve(int kind, float progress) {
1248 float f = 1 - progress;
1249 switch (kind) {
1250 case ANIM_KIND_SCROLL:
1251 case ANIM_KIND_FLING:
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001252 case ANIM_KIND_CAPTURE:
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001253 progress = 1 - f; // linear
1254 break;
1255 case ANIM_KIND_SCALE:
1256 progress = 1 - f * f; // quadratic
1257 break;
Yuli Huang5338d192012-05-17 11:32:15 +08001258 case ANIM_KIND_OPENING:
1259 progress = 1 - f * f * f; // x^3
1260 break;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001261 case ANIM_KIND_SNAPBACK:
1262 case ANIM_KIND_ZOOM:
1263 case ANIM_KIND_SLIDE:
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001264 progress = 1 - f * f * f * f * f; // x^5
1265 break;
1266 }
1267 return progress;
1268 }
Chih-Chung Changec412542011-09-26 17:34:06 +08001269 }
1270
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001271 ////////////////////////////////////////////////////////////////////////////
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001272 // Platform: captures the global X/Y movement.
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001273 ////////////////////////////////////////////////////////////////////////////
1274 private class Platform extends Animatable {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001275 public int mCurrentX, mFromX, mToX, mDefaultX;
1276 public int mCurrentY, mFromY, mToY, mDefaultY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001277 public int mFlingOffset;
1278
1279 @Override
1280 public boolean startSnapback() {
1281 if (mAnimationStartTime != NO_ANIMATION) return false;
1282 if (mAnimationKind == ANIM_KIND_SCROLL
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001283 && mListener.isHolding()) return false;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001284
1285 Box b = mBoxes.get(0);
1286 float scaleMin = mExtraScalingRange ?
1287 b.mScaleMin * SCALE_MIN_EXTRA : b.mScaleMin;
1288 float scaleMax = mExtraScalingRange ?
1289 b.mScaleMax * SCALE_MAX_EXTRA : b.mScaleMax;
1290 float scale = Utils.clamp(b.mCurrentScale, scaleMin, scaleMax);
1291 int x = mCurrentX;
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001292 int y = mDefaultY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001293 if (mFilmMode) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001294 int defaultX = mDefaultX;
1295 if (!mHasNext) x = Math.max(x, defaultX);
1296 if (!mHasPrev) x = Math.min(x, defaultX);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001297 } else {
1298 calculateStableBound(scale, HORIZONTAL_SLACK);
1299 x = Utils.clamp(x, mBoundLeft, mBoundRight);
1300 }
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001301 if (mCurrentX != x || mCurrentY != y) {
1302 return doAnimation(x, y, ANIM_KIND_SNAPBACK);
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001303 }
1304 return false;
1305 }
1306
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001307 // The updateDefaultXY() should be called whenever these variables
1308 // changes: (1) mConstrained (2) mConstrainedFrame (3) mViewW/H (4)
1309 // mFilmMode
1310 public void updateDefaultXY() {
1311 // We don't check mFilmMode and return 0 for mDefaultX. Because
1312 // otherwise if we decide to leave film mode because we are
1313 // centered, we will immediately back into film mode because we find
1314 // we are not centered.
1315 if (mConstrained && !mConstrainedFrame.isEmpty()) {
1316 mDefaultX = mConstrainedFrame.centerX() - mViewW / 2;
1317 mDefaultY = mFilmMode ? 0 :
1318 mConstrainedFrame.centerY() - mViewH / 2;
1319 } else {
1320 mDefaultX = 0;
1321 mDefaultY = 0;
1322 }
1323 }
1324
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001325 // Starts an animation for the platform.
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001326 private boolean doAnimation(int targetX, int targetY, int kind) {
1327 if (mCurrentX == targetX && mCurrentY == targetY) return false;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001328 mAnimationKind = kind;
1329 mFromX = mCurrentX;
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001330 mFromY = mCurrentY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001331 mToX = targetX;
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001332 mToY = targetY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001333 mAnimationStartTime = AnimationTime.startTime();
1334 mAnimationDuration = ANIM_TIME[kind];
1335 mFlingOffset = 0;
1336 advanceAnimation();
1337 return true;
1338 }
1339
1340 @Override
1341 protected boolean interpolate(float progress) {
1342 if (mAnimationKind == ANIM_KIND_FLING) {
1343 return mFilmMode
1344 ? interpolateFlingFilm(progress)
1345 : interpolateFlingPage(progress);
1346 } else {
1347 return interpolateLinear(progress);
1348 }
1349 }
1350
1351 private boolean interpolateFlingFilm(float progress) {
1352 mFilmScroller.computeScrollOffset();
1353 mCurrentX = mFilmScroller.getCurrX() + mFlingOffset;
1354
1355 int dir = EdgeView.INVALID_DIRECTION;
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001356 if (mCurrentX < mDefaultX) {
Chih-Chung Changfb1a1552012-04-19 13:34:48 +08001357 if (!mHasNext) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001358 dir = EdgeView.RIGHT;
1359 }
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001360 } else if (mCurrentX > mDefaultX) {
Chih-Chung Changfb1a1552012-04-19 13:34:48 +08001361 if (!mHasPrev) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001362 dir = EdgeView.LEFT;
1363 }
1364 }
1365 if (dir != EdgeView.INVALID_DIRECTION) {
1366 int v = (int) (mFilmScroller.getCurrVelocity() + 0.5f);
1367 mListener.onAbsorb(v, dir);
1368 mFilmScroller.forceFinished(true);
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001369 mCurrentX = mDefaultX;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001370 }
1371 return mFilmScroller.isFinished();
1372 }
1373
1374 private boolean interpolateFlingPage(float progress) {
1375 mPageScroller.computeScrollOffset(progress);
1376 Box b = mBoxes.get(0);
1377 calculateStableBound(b.mCurrentScale);
1378
1379 int oldX = mCurrentX;
1380 mCurrentX = mPageScroller.getCurrX();
1381
1382 // Check if we hit the edges; show edge effects if we do.
1383 if (oldX > mBoundLeft && mCurrentX == mBoundLeft) {
1384 int v = (int) (-mPageScroller.getCurrVelocityX() + 0.5f);
1385 mListener.onAbsorb(v, EdgeView.RIGHT);
1386 } else if (oldX < mBoundRight && mCurrentX == mBoundRight) {
1387 int v = (int) (mPageScroller.getCurrVelocityX() + 0.5f);
1388 mListener.onAbsorb(v, EdgeView.LEFT);
1389 }
1390
1391 return progress >= 1;
1392 }
1393
1394 private boolean interpolateLinear(float progress) {
1395 // Other animations
1396 if (progress >= 1) {
1397 mCurrentX = mToX;
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001398 mCurrentY = mToY;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001399 return true;
1400 } else {
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001401 if (mAnimationKind == ANIM_KIND_CAPTURE) {
1402 progress = CaptureAnimation.calculateSlide(progress);
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001403 }
1404 mCurrentX = (int) (mFromX + progress * (mToX - mFromX));
1405 mCurrentY = (int) (mFromY + progress * (mToY - mFromY));
1406 if (mAnimationKind == ANIM_KIND_CAPTURE) {
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001407 return false;
1408 } else {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001409 return (mCurrentX == mToX && mCurrentY == mToY);
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001410 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001411 }
1412 }
Chih-Chung Changec412542011-09-26 17:34:06 +08001413 }
1414
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001415 ////////////////////////////////////////////////////////////////////////////
1416 // Box: represents a rectangular area which shows a picture.
1417 ////////////////////////////////////////////////////////////////////////////
1418 private class Box extends Animatable {
1419 // Size of the bitmap
1420 public int mImageW, mImageH;
1421
1422 // This is true if we assume the image size is the same as view size
1423 // until we know the actual size of image. This is also used to
1424 // determine if there is an image ready to show.
1425 public boolean mUseViewSize;
1426
1427 // The minimum and maximum scale we allow for this box.
1428 public float mScaleMin, mScaleMax;
1429
1430 // The X/Y value indicates where the center of the box is on the view
1431 // coordinate. We always keep the mCurrent{X,Y,Scale} sync with the
1432 // actual values used currently. Note that the X values are implicitly
1433 // defined by Platform and Gaps.
1434 public int mCurrentY, mFromY, mToY;
1435 public float mCurrentScale, mFromScale, mToScale;
1436
1437 // The absolute X coordinate of the center of the box. This is only used
1438 // during moveBox().
1439 public int mAbsoluteX;
1440
1441 @Override
1442 public boolean startSnapback() {
1443 if (mAnimationStartTime != NO_ANIMATION) return false;
1444 if (mAnimationKind == ANIM_KIND_SCROLL
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001445 && mListener.isHolding()) return false;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001446 if (mInScale && this == mBoxes.get(0)) return false;
1447
1448 int y;
1449 float scale;
1450
1451 if (this == mBoxes.get(0)) {
1452 float scaleMin = mExtraScalingRange ?
1453 mScaleMin * SCALE_MIN_EXTRA : mScaleMin;
1454 float scaleMax = mExtraScalingRange ?
1455 mScaleMax * SCALE_MAX_EXTRA : mScaleMax;
1456 scale = Utils.clamp(mCurrentScale, scaleMin, scaleMax);
1457 if (mFilmMode) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001458 y = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001459 } else {
1460 calculateStableBound(scale, HORIZONTAL_SLACK);
1461 y = Utils.clamp(mCurrentY, mBoundTop, mBoundBottom);
1462 }
1463 } else {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001464 y = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001465 scale = mScaleMin;
1466 }
1467
1468 if (mCurrentY != y || mCurrentScale != scale) {
1469 return doAnimation(y, scale, ANIM_KIND_SNAPBACK);
1470 }
1471 return false;
1472 }
1473
1474 private boolean doAnimation(int targetY, float targetScale, int kind) {
1475 targetScale = Utils.clamp(targetScale,
1476 SCALE_MIN_EXTRA * mScaleMin,
1477 SCALE_MAX_EXTRA * mScaleMax);
1478
1479 // If the scaled height is smaller than the view height, force it to be
1480 // in the center. (We do this for height only, not width, because the
1481 // user may want to scroll to the previous/next image.)
1482 if (!mInScale && viewTallerThanScaledImage(targetScale)) {
Chih-Chung Changbd141b52012-04-26 10:10:49 +08001483 targetY = 0;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001484 }
1485
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001486 if (mCurrentY == targetY && mCurrentScale == targetScale
1487 && kind != ANIM_KIND_CAPTURE) {
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001488 return false;
1489 }
1490
1491 // Now starts an animation for the box.
1492 mAnimationKind = kind;
1493 mFromY = mCurrentY;
1494 mFromScale = mCurrentScale;
1495 mToY = targetY;
1496 mToScale = targetScale;
1497 mAnimationStartTime = AnimationTime.startTime();
1498 mAnimationDuration = ANIM_TIME[kind];
1499 advanceAnimation();
1500 return true;
1501 }
1502
1503 @Override
1504 protected boolean interpolate(float progress) {
1505 if (mAnimationKind == ANIM_KIND_FLING) {
1506 // Currently a Box can only be flung in page mode.
1507 return interpolateFlingPage(progress);
1508 } else {
1509 return interpolateLinear(progress);
1510 }
1511 }
1512
1513 private boolean interpolateFlingPage(float progress) {
1514 mPageScroller.computeScrollOffset(progress);
1515 calculateStableBound(mCurrentScale);
1516
1517 int oldY = mCurrentY;
1518 mCurrentY = mPageScroller.getCurrY();
1519
1520 // Check if we hit the edges; show edge effects if we do.
1521 if (oldY > mBoundTop && mCurrentY == mBoundTop) {
1522 int v = (int) (-mPageScroller.getCurrVelocityY() + 0.5f);
1523 mListener.onAbsorb(v, EdgeView.BOTTOM);
1524 } else if (oldY < mBoundBottom && mCurrentY == mBoundBottom) {
1525 int v = (int) (mPageScroller.getCurrVelocityY() + 0.5f);
1526 mListener.onAbsorb(v, EdgeView.TOP);
1527 }
1528
1529 return progress >= 1;
1530 }
1531
1532 private boolean interpolateLinear(float progress) {
1533 if (progress >= 1) {
1534 mCurrentY = mToY;
1535 mCurrentScale = mToScale;
1536 return true;
1537 } else {
1538 mCurrentY = (int) (mFromY + progress * (mToY - mFromY));
1539 mCurrentScale = mFromScale + progress * (mToScale - mFromScale);
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001540 if (mAnimationKind == ANIM_KIND_CAPTURE) {
1541 float f = CaptureAnimation.calculateScale(progress);
1542 mCurrentScale *= f;
1543 return false;
1544 } else {
1545 return (mCurrentY == mToY && mCurrentScale == mToScale);
1546 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001547 }
1548 }
Chih-Chung Changec412542011-09-26 17:34:06 +08001549 }
Chih-Chung Chang532d93c2011-10-12 17:10:33 +08001550
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001551 ////////////////////////////////////////////////////////////////////////////
1552 // Gap: represents a rectangular area which is between two boxes.
1553 ////////////////////////////////////////////////////////////////////////////
1554 private class Gap extends Animatable {
1555 // The default gap size between two boxes. The value may vary for
1556 // different image size of the boxes and for different modes (page or
1557 // film).
1558 public int mDefaultSize;
1559
1560 // The gap size between the two boxes.
1561 public int mCurrentGap, mFromGap, mToGap;
1562
1563 @Override
1564 public boolean startSnapback() {
1565 if (mAnimationStartTime != NO_ANIMATION) return false;
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001566 return doAnimation(mDefaultSize, ANIM_KIND_SNAPBACK);
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001567 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001568
1569 // Starts an animation for a gap.
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001570 public boolean doAnimation(int targetSize, int kind) {
1571 if (mCurrentGap == targetSize && kind != ANIM_KIND_CAPTURE) {
1572 return false;
1573 }
1574 mAnimationKind = kind;
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001575 mFromGap = mCurrentGap;
1576 mToGap = targetSize;
1577 mAnimationStartTime = AnimationTime.startTime();
1578 mAnimationDuration = ANIM_TIME[mAnimationKind];
1579 advanceAnimation();
1580 return true;
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001581 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001582
1583 @Override
1584 protected boolean interpolate(float progress) {
1585 if (progress >= 1) {
1586 mCurrentGap = mToGap;
1587 return true;
1588 } else {
1589 mCurrentGap = (int) (mFromGap + progress * (mToGap - mFromGap));
Chih-Chung Chang2c617382012-04-20 20:06:19 +08001590 if (mAnimationKind == ANIM_KIND_CAPTURE) {
1591 float f = CaptureAnimation.calculateScale(progress);
1592 mCurrentGap = (int) (mCurrentGap * f);
1593 return false;
1594 } else {
1595 return (mCurrentGap == mToGap);
1596 }
Chih-Chung Changb7ec5532012-04-03 12:21:16 +08001597 }
Yuli Huang2ce3c3b2012-02-23 22:26:12 +08001598 }
Chih-Chung Chang532d93c2011-10-12 17:10:33 +08001599 }
Chih-Chung Changba12eae2012-05-07 02:29:37 +08001600
1601 ////////////////////////////////////////////////////////////////////////////
1602 // FilmRatio: represents the progress of film mode change.
1603 ////////////////////////////////////////////////////////////////////////////
1604 private class FilmRatio extends Animatable {
1605 // The film ratio: 1 means switching to film mode is complete, 0 means
1606 // switching to page mode is complete.
1607 public float mCurrentRatio, mFromRatio, mToRatio;
1608
1609 @Override
1610 public boolean startSnapback() {
1611 float target = mFilmMode ? 1f : 0f;
1612 if (target == mToRatio) return false;
1613 return doAnimation(target, ANIM_KIND_SNAPBACK);
1614 }
1615
1616 // Starts an animation for the film ratio.
1617 private boolean doAnimation(float targetRatio, int kind) {
1618 mAnimationKind = kind;
1619 mFromRatio = mCurrentRatio;
1620 mToRatio = targetRatio;
1621 mAnimationStartTime = AnimationTime.startTime();
1622 mAnimationDuration = ANIM_TIME[mAnimationKind];
1623 advanceAnimation();
1624 return true;
1625 }
1626
1627 @Override
1628 protected boolean interpolate(float progress) {
1629 if (progress >= 1) {
1630 mCurrentRatio = mToRatio;
1631 return true;
1632 } else {
1633 mCurrentRatio = mFromRatio + progress * (mToRatio - mFromRatio);
1634 return (mCurrentRatio == mToRatio);
1635 }
1636 }
1637 }
Chih-Chung Changec412542011-09-26 17:34:06 +08001638}