Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 17 | package com.android.camera.widget; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 18 | |
| 19 | import android.animation.Animator; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 20 | import android.animation.AnimatorSet; |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 21 | import android.animation.TimeInterpolator; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
| 23 | import android.content.Context; |
| 24 | import android.graphics.Canvas; |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 25 | import android.graphics.Point; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 27 | import android.graphics.RectF; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 28 | import android.net.Uri; |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 29 | import android.os.Handler; |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 30 | import android.os.SystemClock; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 31 | import android.util.AttributeSet; |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 32 | import android.util.DisplayMetrics; |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 34 | import android.view.MotionEvent; |
| 35 | import android.view.View; |
| 36 | import android.view.ViewGroup; |
| 37 | import android.view.animation.DecelerateInterpolator; |
| 38 | import android.widget.Scroller; |
| 39 | |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 40 | import com.android.camera.CameraActivity; |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 41 | import com.android.camera.debug.Log; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 42 | import com.android.camera.filmstrip.DataAdapter; |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 43 | import com.android.camera.filmstrip.FilmstripController; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 44 | import com.android.camera.filmstrip.ImageData; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 45 | import com.android.camera.ui.FilmstripGestureRecognizer; |
| 46 | import com.android.camera.ui.ZoomView; |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 47 | import com.android.camera.util.CameraUtil; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 48 | import com.android.camera2.R; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 49 | |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 50 | import java.util.ArrayDeque; |
| 51 | import java.util.ArrayList; |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 52 | import java.util.Arrays; |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 53 | import java.util.List; |
| 54 | import java.util.Queue; |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 55 | |
Angus Kong | b251025 | 2013-12-10 22:58:15 -0800 | [diff] [blame] | 56 | public class FilmstripView extends ViewGroup { |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 57 | private static final Log.Tag TAG = new Log.Tag("FilmstripView"); |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 58 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 59 | private static final int BUFFER_SIZE = 5; |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 60 | private static final int GEOMETRY_ADJUST_TIME_MS = 400; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 61 | private static final int SNAP_IN_CENTER_TIME_MS = 600; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 62 | private static final float FLING_COASTING_DURATION_S = 0.05f; |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 63 | private static final int ZOOM_ANIMATION_DURATION_MS = 200; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 64 | private static final int CAMERA_PREVIEW_SWIPE_THRESHOLD = 300; |
Angus Kong | 43596cd | 2013-12-04 23:38:00 -0800 | [diff] [blame] | 65 | private static final float FILM_STRIP_SCALE = 0.7f; |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 66 | private static final float FULL_SCREEN_SCALE = 1f; |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 67 | |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 68 | // The min velocity at which the user must have moved their finger in |
| 69 | // pixels per millisecond to count a vertical gesture as a promote/demote |
| 70 | // at short vertical distances. |
| 71 | private static final float PROMOTE_VELOCITY = 3.5f; |
| 72 | // The min distance relative to this view's height the user must have |
| 73 | // moved their finger to count a vertical gesture as a promote/demote if |
| 74 | // they moved their finger at least at PROMOTE_VELOCITY. |
| 75 | private static final float VELOCITY_PROMOTE_HEIGHT_RATIO = 1/10f; |
| 76 | // The min distance relative to this view's height the user must have |
| 77 | // moved their finger to count a vertical gesture as a promote/demote if |
| 78 | // they moved their finger at less than PROMOTE_VELOCITY. |
| 79 | private static final float PROMOTE_HEIGHT_RATIO = 1/2f; |
| 80 | |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 81 | private static final float TOLERANCE = 0.1f; |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 82 | // Only check for intercepting touch events within first 500ms |
| 83 | private static final int SWIPE_TIME_OUT = 500; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 84 | private static final int DECELERATION_FACTOR = 4; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 85 | |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 86 | private CameraActivity mActivity; |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 87 | private FilmstripGestureRecognizer mGestureRecognizer; |
| 88 | private FilmstripGestureRecognizer.Listener mGestureListener; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 89 | private DataAdapter mDataAdapter; |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 90 | private int mViewGapInPixel; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 91 | private final Rect mDrawArea = new Rect(); |
| 92 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 93 | private final int mCurrentItem = (BUFFER_SIZE - 1) / 2; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 94 | private float mScale; |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 95 | private MyController mController; |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 96 | private int mCenterX = -1; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 97 | private final ViewItem[] mViewItem = new ViewItem[BUFFER_SIZE]; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 98 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 99 | private FilmstripController.FilmstripListener mListener; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 100 | private ZoomView mZoomView = null; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 101 | |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 102 | private MotionEvent mDown; |
| 103 | private boolean mCheckToIntercept = true; |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 104 | private int mSlop; |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 105 | private TimeInterpolator mViewAnimInterpolator; |
| 106 | |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 107 | // This is true if and only if the user is scrolling, |
| 108 | private boolean mIsUserScrolling; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 109 | private int mDataIdOnUserScrolling; |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 110 | private float mOverScaleFactor = 1f; |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 111 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 112 | private boolean mFullScreenUIHidden = false; |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 113 | private SparseArray<Queue<View>> recycledViews = new SparseArray<Queue<View>>(); |
| 114 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 115 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 116 | /** |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 117 | * A helper class to tract and calculate the view coordination. |
| 118 | */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 119 | private class ViewItem { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 120 | private int mDataId; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 121 | /** The position of the left of the view in the whole filmstrip. */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 122 | private int mLeftPosition; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 123 | private final View mView; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 124 | private final ImageData mData; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 125 | private final RectF mViewArea; |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 126 | private boolean mMaximumBitmapRequested; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 127 | |
| 128 | private ValueAnimator mTranslationXAnimator; |
| 129 | private ValueAnimator mTranslationYAnimator; |
| 130 | private ValueAnimator mAlphaAnimator; |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 131 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Constructor. |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 134 | * |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 135 | * @param id The id of the data from |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 136 | * {@link com.android.camera.filmstrip.DataAdapter}. |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 137 | * @param v The {@code View} representing the data. |
| 138 | */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 139 | public ViewItem(int id, View v, ImageData data) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 140 | v.setPivotX(0f); |
| 141 | v.setPivotY(0f); |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 142 | mDataId = id; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 143 | mData = data; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 144 | mView = v; |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 145 | mMaximumBitmapRequested = false; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 146 | mLeftPosition = -1; |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 147 | mViewArea = new RectF(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 150 | public boolean isMaximumBitmapRequested() { |
| 151 | return mMaximumBitmapRequested; |
| 152 | } |
| 153 | |
| 154 | public void setMaximumBitmapRequested() { |
| 155 | mMaximumBitmapRequested = true; |
| 156 | } |
| 157 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Returns the data id from |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 160 | * {@link com.android.camera.filmstrip.DataAdapter}. |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 161 | */ |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 162 | public int getId() { |
| 163 | return mDataId; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 166 | /** |
| 167 | * Sets the data id from |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 168 | * {@link com.android.camera.filmstrip.DataAdapter}. |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 169 | */ |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 170 | public void setId(int id) { |
| 171 | mDataId = id; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 174 | /** Sets the left position of the view in the whole filmstrip. */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 175 | public void setLeftPosition(int pos) { |
| 176 | mLeftPosition = pos; |
| 177 | } |
| 178 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 179 | /** Returns the left position of the view in the whole filmstrip. */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 180 | public int getLeftPosition() { |
| 181 | return mLeftPosition; |
| 182 | } |
| 183 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 184 | /** Returns the translation of Y regarding the view scale. */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 185 | public float getTranslationY() { |
| 186 | return mView.getTranslationY() / mScale; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 189 | /** Returns the translation of X regarding the view scale. */ |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 190 | public float getTranslationX() { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 191 | return mView.getTranslationX() / mScale; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 194 | /** Sets the translation of Y regarding the view scale. */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 195 | public void setTranslationY(float transY) { |
| 196 | mView.setTranslationY(transY * mScale); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 199 | /** Sets the translation of X regarding the view scale. */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 200 | public void setTranslationX(float transX) { |
| 201 | mView.setTranslationX(transX * mScale); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 204 | /** Forwarding of {@link android.view.View#setAlpha(float)}. */ |
| 205 | public void setAlpha(float alpha) { |
| 206 | mView.setAlpha(alpha); |
| 207 | } |
| 208 | |
| 209 | /** Forwarding of {@link android.view.View#getAlpha()}. */ |
| 210 | public float getAlpha() { |
| 211 | return mView.getAlpha(); |
| 212 | } |
| 213 | |
| 214 | /** Forwarding of {@link android.view.View#getMeasuredWidth()}. */ |
| 215 | public int getMeasuredWidth() { |
| 216 | return mView.getMeasuredWidth(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Animates the X translation of the view. Note: the animated value is |
| 221 | * not set directly by {@link android.view.View#setTranslationX(float)} |
| 222 | * because the value might be changed during in {@code onLayout()}. |
| 223 | * The animated value of X translation is specially handled in {@code |
| 224 | * layoutIn()}. |
| 225 | * |
| 226 | * @param targetX The final value. |
| 227 | * @param duration_ms The duration of the animation. |
| 228 | * @param interpolator Time interpolator. |
| 229 | */ |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 230 | public void animateTranslationX( |
| 231 | float targetX, long duration_ms, TimeInterpolator interpolator) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 232 | if (mTranslationXAnimator == null) { |
| 233 | mTranslationXAnimator = new ValueAnimator(); |
| 234 | mTranslationXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 235 | @Override |
| 236 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 237 | // We invalidate the filmstrip view instead of setting the |
| 238 | // translation X because the translation X of the view is |
| 239 | // touched in onLayout(). See the documentation of |
| 240 | // animateTranslationX(). |
| 241 | invalidate(); |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | runAnimation(mTranslationXAnimator, getTranslationX(), targetX, duration_ms, |
| 246 | interpolator); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Animates the Y translation of the view. |
| 251 | * |
| 252 | * @param targetY The final value. |
| 253 | * @param duration_ms The duration of the animation. |
| 254 | * @param interpolator Time interpolator. |
| 255 | */ |
| 256 | public void animateTranslationY( |
| 257 | float targetY, long duration_ms, TimeInterpolator interpolator) { |
| 258 | if (mTranslationYAnimator == null) { |
| 259 | mTranslationYAnimator = new ValueAnimator(); |
| 260 | mTranslationYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 261 | @Override |
| 262 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 263 | setTranslationY((Float) valueAnimator.getAnimatedValue()); |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | runAnimation(mTranslationYAnimator, getTranslationY(), targetY, duration_ms, |
| 268 | interpolator); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Animates the alpha value of the view. |
| 273 | * |
| 274 | * @param targetAlpha The final value. |
| 275 | * @param duration_ms The duration of the animation. |
| 276 | * @param interpolator Time interpolator. |
| 277 | */ |
| 278 | public void animateAlpha(float targetAlpha, long duration_ms, |
| 279 | TimeInterpolator interpolator) { |
| 280 | if (mAlphaAnimator == null) { |
| 281 | mAlphaAnimator = new ValueAnimator(); |
| 282 | mAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 283 | @Override |
| 284 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 285 | ViewItem.this.setAlpha((Float) valueAnimator.getAnimatedValue()); |
| 286 | } |
| 287 | }); |
| 288 | } |
| 289 | runAnimation(mAlphaAnimator, getAlpha(), targetAlpha, duration_ms, interpolator); |
| 290 | } |
| 291 | |
| 292 | private void runAnimation(final ValueAnimator animator, final float startValue, |
| 293 | final float targetValue, final long duration_ms, |
| 294 | final TimeInterpolator interpolator) { |
| 295 | if (startValue == targetValue) { |
| 296 | return; |
| 297 | } |
| 298 | animator.setInterpolator(interpolator); |
| 299 | animator.setDuration(duration_ms); |
| 300 | animator.setFloatValues(startValue, targetValue); |
| 301 | animator.start(); |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 304 | /** Adjusts the translation of X regarding the view scale. */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 305 | public void translateXScaledBy(float transX) { |
| 306 | setTranslationX(getTranslationX() + transX * mScale); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Forwarding of {@link android.view.View#getHitRect(android.graphics.Rect)}. |
| 311 | */ |
| 312 | public void getHitRect(Rect rect) { |
| 313 | mView.getHitRect(rect); |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 316 | public int getCenterX() { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 317 | return mLeftPosition + mView.getMeasuredWidth() / 2; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 320 | /** Forwarding of {@link android.view.View#getVisibility()}. */ |
| 321 | public int getVisibility() { |
| 322 | return mView.getVisibility(); |
| 323 | } |
| 324 | |
| 325 | /** Forwarding of {@link android.view.View#setVisibility(int)}. */ |
| 326 | public void setVisibility(int visibility) { |
| 327 | mView.setVisibility(visibility); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Notifies the {@link com.android.camera.filmstrip.DataAdapter} to |
| 332 | * resize the view. |
| 333 | */ |
| 334 | public void resizeView(Context context, int w, int h) { |
| 335 | mDataAdapter.resizeView(context, mDataId, mView, w, h); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Adds the view of the data to the view hierarchy if necessary. |
| 340 | */ |
| 341 | public void addViewToHierarchy() { |
| 342 | if (indexOfChild(mView) < 0) { |
| 343 | mData.prepare(); |
| 344 | addView(mView); |
| 345 | } else { |
| 346 | setVisibility(View.VISIBLE); |
| 347 | setAlpha(1f); |
| 348 | setTranslationX(0); |
| 349 | setTranslationY(0); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Removes from the hierarchy. Keeps the view in the view hierarchy if |
| 355 | * view type is {@code VIEW_TYPE_STICKY} and set to invisible instead. |
| 356 | * |
| 357 | * @param force {@code true} to remove the view from the hierarchy |
| 358 | * regardless of the view type. |
| 359 | */ |
| 360 | public void removeViewFromHierarchy(boolean force) { |
| 361 | if (force || mData.getViewType() != ImageData.VIEW_TYPE_STICKY) { |
| 362 | removeView(mView); |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 363 | mData.recycle(mView); |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 364 | recycleView(mView, mDataId); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 365 | } else { |
| 366 | setVisibility(View.INVISIBLE); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Brings the view to front by |
| 372 | * {@link #bringChildToFront(android.view.View)} |
| 373 | */ |
| 374 | public void bringViewToFront() { |
| 375 | bringChildToFront(mView); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 378 | /** |
| 379 | * The visual x position of this view, in pixels. |
| 380 | */ |
| 381 | public float getX() { |
| 382 | return mView.getX(); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * The visual y position of this view, in pixels. |
| 387 | */ |
| 388 | public float getY() { |
| 389 | return mView.getY(); |
| 390 | } |
| 391 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 392 | /** |
| 393 | * Forwarding of {@link android.view.View#measure(int, int)}. |
| 394 | */ |
| 395 | public void measure(int widthSpec, int heightSpec) { |
| 396 | mView.measure(widthSpec, heightSpec); |
| 397 | } |
| 398 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 399 | private void layoutAt(int left, int top) { |
| 400 | mView.layout(left, top, left + mView.getMeasuredWidth(), |
| 401 | top + mView.getMeasuredHeight()); |
| 402 | } |
| 403 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 404 | /** |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 405 | * The bounding rect of the view. |
| 406 | */ |
| 407 | public RectF getViewRect() { |
| 408 | RectF r = new RectF(); |
| 409 | r.left = mView.getX(); |
| 410 | r.top = mView.getY(); |
| 411 | r.right = r.left + mView.getWidth() * mView.getScaleX(); |
| 412 | r.bottom = r.top + mView.getHeight() * mView.getScaleY(); |
| 413 | return r; |
| 414 | } |
| 415 | |
| 416 | /** |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 417 | * Layouts the view in the area assuming the center of the area is at a |
| 418 | * specific point of the whole filmstrip. |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 419 | * |
| 420 | * @param drawArea The area when filmstrip will show in. |
| 421 | * @param refCenter The absolute X coordination in the whole filmstrip |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 422 | * of the center of {@code drawArea}. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 423 | * @param scale The scale of the view on the filmstrip. |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 424 | */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 425 | public void layoutWithTranslationX(Rect drawArea, int refCenter, float scale) { |
| 426 | final float translationX = |
| 427 | ((mTranslationXAnimator != null && mTranslationXAnimator.isRunning()) ? |
| 428 | (Float) mTranslationXAnimator.getAnimatedValue() : 0); |
| 429 | int left = |
| 430 | (int) (drawArea.centerX() + (mLeftPosition - refCenter + translationX) * scale); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 431 | int top = (int) (drawArea.centerY() - (mView.getMeasuredHeight() / 2) * scale); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 432 | layoutAt(left, top); |
| 433 | mView.setScaleX(scale); |
| 434 | mView.setScaleY(scale); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 435 | |
| 436 | // update mViewArea for touch detection. |
| 437 | int l = mView.getLeft(); |
| 438 | int t = mView.getTop(); |
| 439 | mViewArea.set(l, t, |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 440 | l + mView.getMeasuredWidth() * scale, |
| 441 | t + mView.getMeasuredHeight() * scale); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 444 | /** Returns true if the point is in the view. */ |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 445 | public boolean areaContains(float x, float y) { |
| 446 | return mViewArea.contains(x, y); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 447 | } |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 448 | |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Return the width of the view. |
| 451 | */ |
| 452 | public int getWidth() { |
| 453 | return mView.getWidth(); |
| 454 | } |
| 455 | |
Sam Judd | 29d9b3c | 2014-03-12 13:22:37 -0700 | [diff] [blame] | 456 | /** |
| 457 | * Returns the position of the left edge of the view area content is drawn in. |
| 458 | */ |
| 459 | public int getDrawAreaLeft() { |
| 460 | return Math.round(mViewArea.left); |
| 461 | } |
| 462 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 463 | public void copyAttributes(ViewItem item) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 464 | setLeftPosition(item.getLeftPosition()); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 465 | // X |
| 466 | setTranslationX(item.getTranslationX()); |
| 467 | if (item.mTranslationXAnimator != null) { |
| 468 | mTranslationXAnimator = item.mTranslationXAnimator; |
| 469 | mTranslationXAnimator.removeAllUpdateListeners(); |
| 470 | mTranslationXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 471 | @Override |
| 472 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 473 | // We invalidate the filmstrip view instead of setting the |
| 474 | // translation X because the translation X of the view is |
| 475 | // touched in onLayout(). See the documentation of |
| 476 | // animateTranslationX(). |
| 477 | invalidate(); |
| 478 | } |
| 479 | }); |
| 480 | } |
| 481 | // Y |
| 482 | setTranslationY(item.getTranslationY()); |
| 483 | if (item.mTranslationYAnimator != null) { |
| 484 | mTranslationYAnimator = item.mTranslationYAnimator; |
| 485 | mTranslationYAnimator.removeAllUpdateListeners(); |
| 486 | mTranslationYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 487 | @Override |
| 488 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 489 | setTranslationY((Float) valueAnimator.getAnimatedValue()); |
| 490 | } |
| 491 | }); |
| 492 | } |
| 493 | // Alpha |
| 494 | setAlpha(item.getAlpha()); |
| 495 | if (item.mAlphaAnimator != null) { |
| 496 | mAlphaAnimator = item.mAlphaAnimator; |
| 497 | mAlphaAnimator.removeAllUpdateListeners(); |
| 498 | mAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 499 | @Override |
| 500 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 501 | ViewItem.this.setAlpha((Float) valueAnimator.getAnimatedValue()); |
| 502 | } |
| 503 | }); |
| 504 | } |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 505 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 506 | |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 507 | /** |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 508 | * Apply a scale factor (i.e. {@code postScale}) on top of current scale at |
| 509 | * pivot point ({@code focusX}, {@code focusY}). Visually it should be the |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 510 | * same as post concatenating current view's matrix with specified scale. |
| 511 | */ |
| 512 | void postScale(float focusX, float focusY, float postScale, int viewportWidth, |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 513 | int viewportHeight) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 514 | float transX = mView.getTranslationX(); |
| 515 | float transY = mView.getTranslationY(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 516 | // Pivot point is top left of the view, so we need to translate |
| 517 | // to scale around focus point |
| 518 | transX -= (focusX - getX()) * (postScale - 1f); |
| 519 | transY -= (focusY - getY()) * (postScale - 1f); |
| 520 | float scaleX = mView.getScaleX() * postScale; |
| 521 | float scaleY = mView.getScaleY() * postScale; |
| 522 | updateTransform(transX, transY, scaleX, scaleY, viewportWidth, |
| 523 | viewportHeight); |
| 524 | } |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 525 | |
| 526 | void updateTransform(float transX, float transY, float scaleX, float scaleY, |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 527 | int viewportWidth, int viewportHeight) { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 528 | float left = transX + mView.getLeft(); |
| 529 | float top = transY + mView.getTop(); |
| 530 | RectF r = ZoomView.adjustToFitInBounds(new RectF(left, top, |
| 531 | left + mView.getWidth() * scaleX, |
| 532 | top + mView.getHeight() * scaleY), |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 533 | viewportWidth, viewportHeight); |
| 534 | mView.setScaleX(scaleX); |
| 535 | mView.setScaleY(scaleY); |
| 536 | transX = r.left - mView.getLeft(); |
| 537 | transY = r.top - mView.getTop(); |
| 538 | mView.setTranslationX(transX); |
| 539 | mView.setTranslationY(transY); |
| 540 | } |
| 541 | |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 542 | void resetTransform() { |
| 543 | mView.setScaleX(FULL_SCREEN_SCALE); |
| 544 | mView.setScaleY(FULL_SCREEN_SCALE); |
| 545 | mView.setTranslationX(0f); |
| 546 | mView.setTranslationY(0f); |
| 547 | } |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 548 | |
| 549 | @Override |
| 550 | public String toString() { |
| 551 | return "DataID = " + mDataId + "\n\t left = " + mLeftPosition |
| 552 | + "\n\t viewArea = " + mViewArea |
| 553 | + "\n\t centerX = " + getCenterX() |
| 554 | + "\n\t view MeasuredSize = " |
| 555 | + mView.getMeasuredWidth() + ',' + mView.getMeasuredHeight() |
| 556 | + "\n\t view Size = " + mView.getWidth() + ',' + mView.getHeight() |
| 557 | + "\n\t view scale = " + mView.getScaleX(); |
| 558 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 561 | /** Constructor. */ |
| 562 | public FilmstripView(Context context) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 563 | super(context); |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 564 | init((CameraActivity) context); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 567 | /** Constructor. */ |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 568 | public FilmstripView(Context context, AttributeSet attrs) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 569 | super(context, attrs); |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 570 | init((CameraActivity) context); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 573 | /** Constructor. */ |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 574 | public FilmstripView(Context context, AttributeSet attrs, int defStyle) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 575 | super(context, attrs, defStyle); |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 576 | init((CameraActivity) context); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 579 | private void init(CameraActivity cameraActivity) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 580 | setWillNotDraw(false); |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 581 | mActivity = cameraActivity; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 582 | mScale = 1.0f; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 583 | mDataIdOnUserScrolling = 0; |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 584 | mController = new MyController(cameraActivity); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 585 | mViewAnimInterpolator = new DecelerateInterpolator(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 586 | mZoomView = new ZoomView(cameraActivity); |
| 587 | mZoomView.setVisibility(GONE); |
| 588 | addView(mZoomView); |
| 589 | |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 590 | mGestureListener = new MyGestureReceiver(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 591 | mGestureRecognizer = |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 592 | new FilmstripGestureRecognizer(cameraActivity, mGestureListener); |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 593 | mSlop = (int) getContext().getResources().getDimension(R.dimen.pie_touch_slop); |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 594 | DisplayMetrics metrics = new DisplayMetrics(); |
| 595 | mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics); |
| 596 | // Allow over scaling because on high density screens, pixels are too |
| 597 | // tiny to clearly see the details at 1:1 zoom. We should not scale |
| 598 | // beyond what 1:1 would look like on a medium density screen, as |
| 599 | // scaling beyond that would only yield blur. |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 600 | mOverScaleFactor = (float) metrics.densityDpi / (float) DisplayMetrics.DENSITY_HIGH; |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 601 | if (mOverScaleFactor < 1f) { |
| 602 | mOverScaleFactor = 1f; |
| 603 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 606 | private void recycleView(View view, int dataId) { |
| 607 | final int viewType = mDataAdapter.getItemViewType(dataId); |
| 608 | Queue<View> recycledViewsForType = recycledViews.get(viewType); |
| 609 | if (recycledViewsForType == null) { |
| 610 | recycledViewsForType = new ArrayDeque<View>(); |
| 611 | recycledViews.put(viewType, recycledViewsForType); |
| 612 | } |
| 613 | recycledViewsForType.offer(view); |
| 614 | } |
| 615 | |
| 616 | private View getRecycledView(int dataId) { |
| 617 | final int viewType = mDataAdapter.getItemViewType(dataId); |
| 618 | Queue<View> recycledViewsForType = recycledViews.get(viewType); |
| 619 | View result = null; |
| 620 | if (recycledViewsForType != null) { |
| 621 | result = recycledViewsForType.poll(); |
| 622 | } |
| 623 | return result; |
| 624 | } |
| 625 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 626 | /** |
| 627 | * Returns the controller. |
| 628 | * |
| 629 | * @return The {@code Controller}. |
| 630 | */ |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 631 | public FilmstripController getController() { |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 632 | return mController; |
| 633 | } |
| 634 | |
Sam Judd | 29d9b3c | 2014-03-12 13:22:37 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Returns the draw area width of the current item. |
| 637 | */ |
| 638 | public int getCurrentItemLeft() { |
| 639 | return mViewItem[mCurrentItem].getDrawAreaLeft(); |
| 640 | } |
| 641 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 642 | private void setListener(FilmstripController.FilmstripListener l) { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 643 | mListener = l; |
| 644 | } |
| 645 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 646 | private void setViewGap(int viewGap) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 647 | mViewGapInPixel = viewGap; |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 650 | /** |
| 651 | * Checks if the data is at the center. |
| 652 | * |
| 653 | * @param id The id of the data to check. |
| 654 | * @return {@code True} if the data is currently at the center. |
| 655 | */ |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 656 | private boolean isDataAtCenter(int id) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 657 | if (mViewItem[mCurrentItem] == null) { |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 658 | return false; |
| 659 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 660 | if (mViewItem[mCurrentItem].getId() == id |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 661 | && isCurrentItemCentered()) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 662 | return true; |
| 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
Angus Kong | 653c43b | 2013-08-21 18:28:43 -0700 | [diff] [blame] | 667 | private void measureViewItem(ViewItem item, int boundWidth, int boundHeight) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 668 | int id = item.getId(); |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 669 | ImageData imageData = mDataAdapter.getImageData(id); |
| 670 | if (imageData == null) { |
Erin Dahlgren | f9ef1ae | 2013-10-14 17:35:23 -0700 | [diff] [blame] | 671 | Log.e(TAG, "trying to measure a null item"); |
| 672 | return; |
| 673 | } |
| 674 | |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 675 | Point dim = CameraUtil.resizeToFill(imageData.getWidth(), imageData.getHeight(), |
| 676 | imageData.getRotation(), boundWidth, boundHeight); |
Angus Kong | 653c43b | 2013-08-21 18:28:43 -0700 | [diff] [blame] | 677 | |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 678 | item.measure(MeasureSpec.makeMeasureSpec(dim.x, MeasureSpec.EXACTLY), |
| 679 | MeasureSpec.makeMeasureSpec(dim.y, MeasureSpec.EXACTLY)); |
Angus Kong | 653c43b | 2013-08-21 18:28:43 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 682 | @Override |
| 683 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 684 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 685 | |
| 686 | int boundWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 687 | int boundHeight = MeasureSpec.getSize(heightMeasureSpec); |
Angus Kong | b477b42 | 2013-07-09 09:30:32 -0700 | [diff] [blame] | 688 | if (boundWidth == 0 || boundHeight == 0) { |
| 689 | // Either width or height is unknown, can't measure children yet. |
| 690 | return; |
| 691 | } |
| 692 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 693 | for (ViewItem item : mViewItem) { |
Angus Kong | 653c43b | 2013-08-21 18:28:43 -0700 | [diff] [blame] | 694 | if (item != null) { |
| 695 | measureViewItem(item, boundWidth, boundHeight); |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 696 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 697 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 698 | clampCenterX(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 699 | // Measure zoom view |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 700 | mZoomView.measure(MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.EXACTLY), |
| 701 | MeasureSpec.makeMeasureSpec(heightMeasureSpec, MeasureSpec.EXACTLY)); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | private int findTheNearestView(int pointX) { |
| 705 | |
| 706 | int nearest = 0; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 707 | // Find the first non-null ViewItem. |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 708 | while (nearest < BUFFER_SIZE |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 709 | && (mViewItem[nearest] == null || mViewItem[nearest].getLeftPosition() == -1)) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 710 | nearest++; |
| 711 | } |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 712 | // No existing available ViewItem |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 713 | if (nearest == BUFFER_SIZE) { |
| 714 | return -1; |
| 715 | } |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 716 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 717 | int min = Math.abs(pointX - mViewItem[nearest].getCenterX()); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 718 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 719 | for (int itemID = nearest + 1; itemID < BUFFER_SIZE && mViewItem[itemID] != null; itemID++) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 720 | // Not measured yet. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 721 | if (mViewItem[itemID].getLeftPosition() == -1) |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 722 | continue; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 723 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 724 | int c = mViewItem[itemID].getCenterX(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 725 | int dist = Math.abs(pointX - c); |
| 726 | if (dist < min) { |
| 727 | min = dist; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 728 | nearest = itemID; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | return nearest; |
| 732 | } |
| 733 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 734 | private ViewItem buildItemFromData(int dataID) { |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 735 | ImageData data = mDataAdapter.getImageData(dataID); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 736 | if (data == null) { |
| 737 | return null; |
| 738 | } |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 739 | |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 740 | int width = Math.round(mScale * getWidth()); |
| 741 | int height = Math.round(mScale * getHeight()); |
| 742 | mDataAdapter.suggestViewSizeBound(width, height); |
| 743 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 744 | data.prepare(); |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 745 | View recycled = getRecycledView(dataID); |
| 746 | View v = mDataAdapter.getView(mActivity, recycled, dataID); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 747 | if (v == null) { |
| 748 | return null; |
| 749 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 750 | ViewItem item = new ViewItem(dataID, v, data); |
| 751 | item.addViewToHierarchy(); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 752 | return item; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 755 | private void checkItemAtMaxSize() { |
| 756 | ViewItem item = mViewItem[mCurrentItem]; |
| 757 | if (item.isMaximumBitmapRequested()) { |
| 758 | return; |
| 759 | }; |
| 760 | item.setMaximumBitmapRequested(); |
| 761 | // Request full size bitmap, or max that DataAdapter will create. |
| 762 | int id = item.getId(); |
| 763 | int h = mDataAdapter.getImageData(id).getHeight(); |
| 764 | int w = mDataAdapter.getImageData(id).getWidth(); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 765 | item.resizeView(mActivity, w, h); |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 768 | private void removeItem(int itemID) { |
| 769 | if (itemID >= mViewItem.length || mViewItem[itemID] == null) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 770 | return; |
| 771 | } |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 772 | ImageData data = mDataAdapter.getImageData(mViewItem[itemID].getId()); |
Erin Dahlgren | f9ef1ae | 2013-10-14 17:35:23 -0700 | [diff] [blame] | 773 | if (data == null) { |
| 774 | Log.e(TAG, "trying to remove a null item"); |
| 775 | return; |
| 776 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 777 | mViewItem[itemID].removeViewFromHierarchy(false); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 778 | mViewItem[itemID] = null; |
Angus Kong | 750e8ec | 2013-05-06 10:42:28 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 781 | /** |
| 782 | * We try to keep the one closest to the center of the screen at position |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 783 | * mCurrentItem. |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 784 | */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 785 | private void stepIfNeeded() { |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 786 | if (!inFilmstrip() && !inFullScreen()) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 787 | // The good timing to step to the next view is when everything is |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 788 | // not in transition. |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 789 | return; |
| 790 | } |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 791 | final int nearest = findTheNearestView(mCenterX); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 792 | // no change made. |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 793 | if (nearest == -1 || nearest == mCurrentItem) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 794 | return; |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 795 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 796 | |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 797 | int prevDataId = (mViewItem[mCurrentItem] == null ? -1 : mViewItem[mCurrentItem].getId()); |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 798 | final int adjust = nearest - mCurrentItem; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 799 | if (adjust > 0) { |
| 800 | for (int k = 0; k < adjust; k++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 801 | removeItem(k); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 802 | } |
| 803 | for (int k = 0; k + adjust < BUFFER_SIZE; k++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 804 | mViewItem[k] = mViewItem[k + adjust]; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 805 | } |
| 806 | for (int k = BUFFER_SIZE - adjust; k < BUFFER_SIZE; k++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 807 | mViewItem[k] = null; |
| 808 | if (mViewItem[k - 1] != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 809 | mViewItem[k] = buildItemFromData(mViewItem[k - 1].getId() + 1); |
Angus Kong | 750e8ec | 2013-05-06 10:42:28 -0700 | [diff] [blame] | 810 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 811 | } |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 812 | adjustChildZOrder(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 813 | } else { |
| 814 | for (int k = BUFFER_SIZE - 1; k >= BUFFER_SIZE + adjust; k--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 815 | removeItem(k); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 816 | } |
| 817 | for (int k = BUFFER_SIZE - 1; k + adjust >= 0; k--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 818 | mViewItem[k] = mViewItem[k + adjust]; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 819 | } |
| 820 | for (int k = -1 - adjust; k >= 0; k--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 821 | mViewItem[k] = null; |
| 822 | if (mViewItem[k + 1] != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 823 | mViewItem[k] = buildItemFromData(mViewItem[k + 1].getId() - 1); |
Angus Kong | 750e8ec | 2013-05-06 10:42:28 -0700 | [diff] [blame] | 824 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 827 | invalidate(); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 828 | if (mListener != null) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 829 | mListener.onDataFocusChanged(prevDataId, mViewItem[mCurrentItem].getId()); |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 830 | final int firstVisible = mViewItem[mCurrentItem].getId() - 2; |
| 831 | final int visibleItemCount = firstVisible + BUFFER_SIZE; |
| 832 | final int totalItemCount = mDataAdapter.getTotalNumber(); |
| 833 | mListener.onScroll(firstVisible, visibleItemCount, totalItemCount); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 834 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 837 | /** |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 838 | * Check the bounds of {@code mCenterX}. Always call this function after: 1. |
| 839 | * Any changes to {@code mCenterX}. 2. Any size change of the view items. |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 840 | * |
| 841 | * @return Whether clamp happened. |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 842 | */ |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 843 | private boolean clampCenterX() { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 844 | ViewItem curr = mViewItem[mCurrentItem]; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 845 | if (curr == null) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 846 | return false; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 847 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 848 | |
Angus Kong | 5fbe134 | 2013-09-10 15:42:46 -0700 | [diff] [blame] | 849 | boolean stopScroll = false; |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 850 | if (curr.getId() == 1 && mCenterX < curr.getCenterX() && mDataIdOnUserScrolling > 1 && |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 851 | mDataAdapter.getImageData(0).getViewType() == ImageData.VIEW_TYPE_STICKY && |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 852 | mController.isScrolling()) { |
Angus Kong | 5fbe134 | 2013-09-10 15:42:46 -0700 | [diff] [blame] | 853 | stopScroll = true; |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 854 | } else { |
| 855 | if (curr.getId() == 0 && mCenterX < curr.getCenterX()) { |
| 856 | // Stop at the first ViewItem. |
| 857 | stopScroll = true; |
| 858 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 859 | } |
| 860 | if (curr.getId() == mDataAdapter.getTotalNumber() - 1 |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 861 | && mCenterX > curr.getCenterX()) { |
Angus Kong | 5fbe134 | 2013-09-10 15:42:46 -0700 | [diff] [blame] | 862 | // Stop at the end. |
| 863 | stopScroll = true; |
| 864 | } |
| 865 | |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 866 | if (stopScroll) { |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 867 | mCenterX = curr.getCenterX(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 868 | } |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 869 | |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 870 | return stopScroll; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 873 | /** |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 874 | * Reorders the child views to be consistent with their data ID. This method |
| 875 | * should be called after adding/removing views. |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 876 | */ |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 877 | private void adjustChildZOrder() { |
| 878 | for (int i = BUFFER_SIZE - 1; i >= 0; i--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 879 | if (mViewItem[i] == null) |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 880 | continue; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 881 | mViewItem[i].bringViewToFront(); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 882 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 883 | // ZoomView is a special case to always be in the front. |
| 884 | bringChildToFront(mZoomView); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 887 | /** |
Angus Kong | 32226b9 | 2013-11-13 08:50:51 -0800 | [diff] [blame] | 888 | * Returns the ID of the current item, or -1 if there is no data. |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 889 | */ |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 890 | private int getCurrentId() { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 891 | ViewItem current = mViewItem[mCurrentItem]; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 892 | if (current == null) { |
| 893 | return -1; |
| 894 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 895 | return current.getId(); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /** |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 899 | * Keep the current item in the center. This functions does not check if the |
| 900 | * current item is null. |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 901 | */ |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 902 | private void snapInCenter() { |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 903 | final ViewItem currItem = mViewItem[mCurrentItem]; |
| 904 | if (currItem == null) { |
| 905 | return; |
| 906 | } |
| 907 | final int currentViewCenter = currItem.getCenterX(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 908 | if (mController.isScrolling() || mIsUserScrolling |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 909 | || isCurrentItemCentered()) { |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 910 | return; |
| 911 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 912 | |
| 913 | int snapInTime = (int) (SNAP_IN_CENTER_TIME_MS |
| 914 | * ((float) Math.abs(mCenterX - currentViewCenter)) |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 915 | / mDrawArea.width()); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 916 | mController.scrollToPosition(currentViewCenter, |
| 917 | snapInTime, false); |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 918 | if (isViewTypeSticky(currItem) && !mController.isScaling() && mScale != FULL_SCREEN_SCALE) { |
Doris Liu | 742cd5b | 2013-09-12 16:17:43 -0700 | [diff] [blame] | 919 | // Now going to full screen camera |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 920 | mController.goToFullScreen(); |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 924 | /** |
| 925 | * Translates the {@link ViewItem} on the left of the current one to match |
| 926 | * the full-screen layout. In full-screen, we show only one {@link ViewItem} |
| 927 | * which occupies the whole screen. The other left ones are put on the left |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 928 | * side in full scales. Does nothing if there's no next item. |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 929 | * |
| 930 | * @param currItem The item ID of the current one to be translated. |
| 931 | * @param drawAreaWidth The width of the current draw area. |
| 932 | * @param scaleFraction A {@code float} between 0 and 1. 0 if the current |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 933 | * scale is {@code FILM_STRIP_SCALE}. 1 if the current scale is |
| 934 | * {@code FULL_SCREEN_SCALE}. |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 935 | */ |
| 936 | private void translateLeftViewItem( |
| 937 | int currItem, int drawAreaWidth, float scaleFraction) { |
| 938 | if (currItem < 0 || currItem > BUFFER_SIZE - 1) { |
| 939 | Log.e(TAG, "currItem id out of bound."); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | final ViewItem curr = mViewItem[currItem]; |
| 944 | final ViewItem next = mViewItem[currItem + 1]; |
| 945 | if (curr == null || next == null) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 946 | Log.e(TAG, "Invalid view item (curr or next == null). curr = " |
| 947 | + currItem); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | |
| 951 | final int currCenterX = curr.getCenterX(); |
| 952 | final int nextCenterX = next.getCenterX(); |
| 953 | final int translate = (int) ((nextCenterX - drawAreaWidth |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 954 | - currCenterX) * scaleFraction); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 955 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 956 | curr.layoutWithTranslationX(mDrawArea, mCenterX, mScale); |
| 957 | curr.setAlpha(1f); |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 958 | curr.setVisibility(VISIBLE); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 959 | |
| 960 | if (inFullScreen()) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 961 | curr.setTranslationX(translate * (mCenterX - currCenterX) / (nextCenterX - currCenterX)); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 962 | } else { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 963 | curr.setTranslationX(translate); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Fade out the {@link ViewItem} on the right of the current one in |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 969 | * full-screen layout. Does nothing if there's no previous item. |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 970 | * |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 971 | * @param currItemId The ID of the item to fade. |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 972 | */ |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 973 | private void fadeAndScaleRightViewItem(int currItemId) { |
| 974 | if (currItemId < 1 || currItemId > BUFFER_SIZE) { |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 975 | Log.e(TAG, "currItem id out of bound."); |
| 976 | return; |
| 977 | } |
| 978 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 979 | final ViewItem currItem = mViewItem[currItemId]; |
| 980 | final ViewItem prevItem = mViewItem[currItemId - 1]; |
| 981 | if (currItem == null || prevItem == null) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 982 | Log.e(TAG, "Invalid view item (curr or prev == null). curr = " |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 983 | + currItemId); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 987 | if (currItemId > mCurrentItem + 1) { |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 988 | // Every item not right next to the mCurrentItem is invisible. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 989 | currItem.setVisibility(INVISIBLE); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 990 | return; |
| 991 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 992 | final int prevCenterX = prevItem.getCenterX(); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 993 | if (mCenterX <= prevCenterX) { |
| 994 | // Shortcut. If the position is at the center of the previous one, |
| 995 | // set to invisible too. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 996 | currItem.setVisibility(INVISIBLE); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 997 | return; |
| 998 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 999 | final int currCenterX = currItem.getCenterX(); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1000 | final float fadeDownFraction = |
| 1001 | ((float) mCenterX - prevCenterX) / (currCenterX - prevCenterX); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1002 | currItem.layoutWithTranslationX(mDrawArea, currCenterX, |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1003 | FILM_STRIP_SCALE + (1f - FILM_STRIP_SCALE) * fadeDownFraction); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1004 | currItem.setAlpha(fadeDownFraction); |
| 1005 | currItem.setTranslationX(0); |
| 1006 | currItem.setVisibility(VISIBLE); |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 1009 | private void layoutViewItems(boolean layoutChanged) { |
Angus Kong | 5fbe134 | 2013-09-10 15:42:46 -0700 | [diff] [blame] | 1010 | if (mViewItem[mCurrentItem] == null || |
| 1011 | mDrawArea.width() == 0 || |
| 1012 | mDrawArea.height() == 0) { |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 1013 | return; |
| 1014 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1015 | |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 1016 | // If the layout changed, we need to adjust the current position so |
| 1017 | // that if an item is centered before the change, it's still centered. |
| 1018 | if (layoutChanged) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1019 | mViewItem[mCurrentItem].setLeftPosition( |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1020 | mCenterX - mViewItem[mCurrentItem].getMeasuredWidth() / 2); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1023 | if (inZoomView()) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1024 | return; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1025 | } |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1026 | /** |
| 1027 | * Transformed scale fraction between 0 and 1. 0 if the scale is |
Sascha Haeberling | 88ef766 | 2013-08-15 17:19:22 -0700 | [diff] [blame] | 1028 | * {@link FILM_STRIP_SCALE}. 1 if the scale is {@link FULL_SCREEN_SCALE} |
| 1029 | * . |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1030 | */ |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1031 | final float scaleFraction = mViewAnimInterpolator.getInterpolation( |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1032 | (mScale - FILM_STRIP_SCALE) / (FULL_SCREEN_SCALE - FILM_STRIP_SCALE)); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1033 | final int fullScreenWidth = mDrawArea.width() + mViewGapInPixel; |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1034 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1035 | // Decide the position for all view items on the left and the right |
| 1036 | // first. |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 1037 | |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1038 | // Left items. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1039 | for (int itemID = mCurrentItem - 1; itemID >= 0; itemID--) { |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1040 | final ViewItem curr = mViewItem[itemID]; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1041 | if (curr == null) { |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1042 | break; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1043 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1044 | |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1045 | // First, layout relatively to the next one. |
| 1046 | final int currLeft = mViewItem[itemID + 1].getLeftPosition() |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1047 | - curr.getMeasuredWidth() - mViewGapInPixel; |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1048 | curr.setLeftPosition(currLeft); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1049 | } |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1050 | // Right items. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1051 | for (int itemID = mCurrentItem + 1; itemID < BUFFER_SIZE; itemID++) { |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1052 | final ViewItem curr = mViewItem[itemID]; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1053 | if (curr == null) { |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1054 | break; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1055 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1056 | |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1057 | // First, layout relatively to the previous one. |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1058 | final ViewItem prev = mViewItem[itemID - 1]; |
| 1059 | final int currLeft = |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1060 | prev.getLeftPosition() + prev.getMeasuredWidth() |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1061 | + mViewGapInPixel; |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1062 | curr.setLeftPosition(currLeft); |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1065 | // Special case for the one immediately on the right of the camera |
| 1066 | // preview. |
| 1067 | boolean immediateRight = |
| 1068 | (mViewItem[mCurrentItem].getId() == 1 && |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1069 | mDataAdapter.getImageData(0).getViewType() == ImageData.VIEW_TYPE_STICKY); |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1070 | |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1071 | // Layout the current ViewItem first. |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1072 | if (immediateRight) { |
| 1073 | // Just do a simple layout without any special translation or |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1074 | // fading. The implementation in Gallery does not push the first |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1075 | // photo to the bottom of the camera preview. Simply place the |
| 1076 | // photo on the right of the preview. |
| 1077 | final ViewItem currItem = mViewItem[mCurrentItem]; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1078 | currItem.layoutWithTranslationX(mDrawArea, mCenterX, mScale); |
| 1079 | currItem.setTranslationX(0f); |
| 1080 | currItem.setAlpha(1f); |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1081 | } else if (scaleFraction == 1f) { |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1082 | final ViewItem currItem = mViewItem[mCurrentItem]; |
| 1083 | final int currCenterX = currItem.getCenterX(); |
| 1084 | if (mCenterX < currCenterX) { |
| 1085 | // In full-screen and mCenterX is on the left of the center, |
| 1086 | // we draw the current one to "fade down". |
| 1087 | fadeAndScaleRightViewItem(mCurrentItem); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1088 | } else if (mCenterX > currCenterX) { |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1089 | // In full-screen and mCenterX is on the right of the center, |
| 1090 | // we draw the current one translated. |
| 1091 | translateLeftViewItem(mCurrentItem, fullScreenWidth, scaleFraction); |
| 1092 | } else { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1093 | currItem.layoutWithTranslationX(mDrawArea, mCenterX, mScale); |
| 1094 | currItem.setTranslationX(0f); |
| 1095 | currItem.setAlpha(1f); |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } else { |
| 1098 | final ViewItem currItem = mViewItem[mCurrentItem]; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1099 | // The normal filmstrip has no translation for the current item. If |
| 1100 | // it has translation before, gradually set it to zero. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1101 | currItem.setTranslationX(currItem.getTranslationX() * scaleFraction); |
| 1102 | currItem.layoutWithTranslationX(mDrawArea, mCenterX, mScale); |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1103 | if (mViewItem[mCurrentItem - 1] == null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1104 | currItem.setAlpha(1f); |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1105 | } else { |
| 1106 | final int currCenterX = currItem.getCenterX(); |
| 1107 | final int prevCenterX = mViewItem[mCurrentItem - 1].getCenterX(); |
| 1108 | final float fadeDownFraction = |
| 1109 | ((float) mCenterX - prevCenterX) / (currCenterX - prevCenterX); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1110 | currItem.setAlpha( |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1111 | (1 - fadeDownFraction) * (1 - scaleFraction) + fadeDownFraction); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | // Layout the rest dependent on the current scale. |
| 1116 | |
| 1117 | // Items on the left |
| 1118 | for (int itemID = mCurrentItem - 1; itemID >= 0; itemID--) { |
| 1119 | final ViewItem curr = mViewItem[itemID]; |
| 1120 | if (curr == null) { |
| 1121 | break; |
| 1122 | } |
| 1123 | translateLeftViewItem(itemID, fullScreenWidth, scaleFraction); |
| 1124 | } |
| 1125 | |
| 1126 | // Items on the right |
| 1127 | for (int itemID = mCurrentItem + 1; itemID < BUFFER_SIZE; itemID++) { |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1128 | final ViewItem curr = mViewItem[itemID]; |
Angus Kong | d028dd4 | 2013-09-13 18:09:18 -0700 | [diff] [blame] | 1129 | if (curr == null) { |
| 1130 | break; |
| 1131 | } |
| 1132 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1133 | curr.layoutWithTranslationX(mDrawArea, mCenterX, mScale); |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1134 | if (curr.getId() == 1 && isViewTypeSticky(curr)) { |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1135 | // Special case for the one next to the camera preview. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1136 | curr.setAlpha(1f); |
Angus Kong | da568b2 | 2013-10-08 20:58:33 -0700 | [diff] [blame] | 1137 | continue; |
| 1138 | } |
| 1139 | |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1140 | if (scaleFraction == 1) { |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 1141 | // It's in full-screen mode. |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1142 | fadeAndScaleRightViewItem(itemID); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1143 | } else { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1144 | if (curr.getVisibility() == INVISIBLE) { |
| 1145 | curr.setVisibility(VISIBLE); |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1146 | } |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1147 | if (itemID == mCurrentItem + 1) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1148 | curr.setAlpha(1f - scaleFraction); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1149 | } else { |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1150 | if (scaleFraction == 0f) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1151 | curr.setAlpha(1f); |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1152 | } else { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1153 | curr.setVisibility(INVISIBLE); |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1154 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1155 | } |
Angus Kong | 80af7dd | 2013-08-22 21:00:19 -0700 | [diff] [blame] | 1156 | curr.setTranslationX( |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1157 | (mViewItem[mCurrentItem].getLeftPosition() - curr.getLeftPosition()) * |
| 1158 | scaleFraction); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | stepIfNeeded(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1163 | } |
Angus Kong | aa2a243 | 2013-08-21 11:03:38 -0700 | [diff] [blame] | 1164 | |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1165 | private boolean isViewTypeSticky(ViewItem item) { |
| 1166 | if (item == null) { |
| 1167 | return false; |
| 1168 | } |
| 1169 | return mDataAdapter.getImageData(item.getId()).getViewType() == |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1170 | ImageData.VIEW_TYPE_STICKY; |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1173 | @Override |
| 1174 | public void onDraw(Canvas c) { |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 1175 | // TODO: remove layoutViewItems() here. |
| 1176 | layoutViewItems(false); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1177 | super.onDraw(c); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | @Override |
| 1181 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1182 | mDrawArea.left = l; |
| 1183 | mDrawArea.top = t; |
| 1184 | mDrawArea.right = r; |
| 1185 | mDrawArea.bottom = b; |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1186 | mZoomView.layout(mDrawArea.left, mDrawArea.top, mDrawArea.right, mDrawArea.bottom); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1187 | // TODO: Need a more robust solution to decide when to re-layout |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1188 | // If in the middle of zooming, only re-layout when the layout has |
| 1189 | // changed. |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1190 | if (!inZoomView() || changed) { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1191 | resetZoomView(); |
Angus Kong | f1582c9 | 2013-10-12 18:52:33 -0700 | [diff] [blame] | 1192 | layoutViewItems(changed); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1193 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 1196 | /** |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1197 | * Clears the translation and scale that has been set on the view, cancels |
| 1198 | * any loading request for image partial decoding, and hides zoom view. This |
| 1199 | * is needed for when there is a layout change (e.g. when users re-enter the |
| 1200 | * app, or rotate the device, etc). |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 1201 | */ |
| 1202 | private void resetZoomView() { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1203 | if (!inZoomView()) { |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 1204 | return; |
| 1205 | } |
| 1206 | ViewItem current = mViewItem[mCurrentItem]; |
| 1207 | if (current == null) { |
| 1208 | return; |
| 1209 | } |
| 1210 | mScale = FULL_SCREEN_SCALE; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 1211 | mController.cancelZoomAnimation(); |
| 1212 | mController.cancelFlingAnimation(); |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 1213 | current.resetTransform(); |
| 1214 | mController.cancelLoadingZoomedImage(); |
| 1215 | mZoomView.setVisibility(GONE); |
Doris Liu | 03b7541 | 2013-09-09 17:26:14 -0700 | [diff] [blame] | 1216 | mController.setSurroundingViewsVisible(true); |
Doris Liu | f9ddb60 | 2013-08-30 16:20:58 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Doris Liu | b0288ec | 2013-11-04 14:03:28 -0800 | [diff] [blame] | 1219 | private void hideZoomView() { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1220 | if (inZoomView()) { |
Doris Liu | b0288ec | 2013-11-04 14:03:28 -0800 | [diff] [blame] | 1221 | mController.cancelLoadingZoomedImage(); |
| 1222 | mZoomView.setVisibility(GONE); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 1226 | private void slideViewBack(ViewItem item) { |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1227 | item.animateTranslationX(0, GEOMETRY_ADJUST_TIME_MS, mViewAnimInterpolator); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1228 | item.animateTranslationY(0, GEOMETRY_ADJUST_TIME_MS, mViewAnimInterpolator); |
| 1229 | item.animateAlpha(1f, GEOMETRY_ADJUST_TIME_MS, mViewAnimInterpolator); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1232 | private void animateItemRemoval(int dataID, final ImageData data) { |
Angus Kong | c6953a4 | 2013-12-16 14:48:01 -0800 | [diff] [blame] | 1233 | if (mScale > FULL_SCREEN_SCALE) { |
| 1234 | resetZoomView(); |
| 1235 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1236 | int removedItemId = findItemByDataID(dataID); |
Angus Kong | c31569c | 2013-05-30 15:18:50 -0700 | [diff] [blame] | 1237 | |
| 1238 | // adjust the data id to be consistent |
| 1239 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1240 | if (mViewItem[i] == null || mViewItem[i].getId() <= dataID) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1241 | continue; |
| 1242 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1243 | mViewItem[i].setId(mViewItem[i].getId() - 1); |
Angus Kong | c31569c | 2013-05-30 15:18:50 -0700 | [diff] [blame] | 1244 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1245 | if (removedItemId == -1) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1246 | return; |
| 1247 | } |
Angus Kong | c31569c | 2013-05-30 15:18:50 -0700 | [diff] [blame] | 1248 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1249 | final ViewItem removedItem = mViewItem[removedItemId]; |
| 1250 | final int offsetX = removedItem.getMeasuredWidth() + mViewGapInPixel; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1251 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1252 | for (int i = removedItemId + 1; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1253 | if (mViewItem[i] != null) { |
| 1254 | mViewItem[i].setLeftPosition(mViewItem[i].getLeftPosition() - offsetX); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1258 | if (removedItemId >= mCurrentItem |
| 1259 | && mViewItem[removedItemId].getId() < mDataAdapter.getTotalNumber()) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1260 | // Fill the removed item by left shift when the current one or |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1261 | // anyone on the right is removed, and there's more data on the |
| 1262 | // right available. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1263 | for (int i = removedItemId; i < BUFFER_SIZE - 1; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1264 | mViewItem[i] = mViewItem[i + 1]; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // pull data out from the DataAdapter for the last one. |
| 1268 | int curr = BUFFER_SIZE - 1; |
| 1269 | int prev = curr - 1; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1270 | if (mViewItem[prev] != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1271 | mViewItem[curr] = buildItemFromData(mViewItem[prev].getId() + 1); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1272 | } |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1273 | |
Angus Kong | aa2a243 | 2013-08-21 11:03:38 -0700 | [diff] [blame] | 1274 | // The animation part. |
| 1275 | if (inFullScreen()) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1276 | mViewItem[mCurrentItem].setVisibility(VISIBLE); |
Angus Kong | aa2a243 | 2013-08-21 11:03:38 -0700 | [diff] [blame] | 1277 | ViewItem nextItem = mViewItem[mCurrentItem + 1]; |
| 1278 | if (nextItem != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1279 | nextItem.setVisibility(INVISIBLE); |
Angus Kong | aa2a243 | 2013-08-21 11:03:38 -0700 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Angus Kong | 0980fe8 | 2013-05-28 17:48:04 -0700 | [diff] [blame] | 1283 | // Translate the views to their original places. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1284 | for (int i = removedItemId; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1285 | if (mViewItem[i] != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1286 | mViewItem[i].setTranslationX(offsetX); |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | // The end of the filmstrip might have been changed. |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 1291 | // The mCenterX might be out of the bound. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1292 | ViewItem currItem = mViewItem[mCurrentItem]; |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1293 | if (currItem.getId() == mDataAdapter.getTotalNumber() - 1 |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1294 | && mCenterX > currItem.getCenterX()) { |
| 1295 | int adjustDiff = currItem.getCenterX() - mCenterX; |
| 1296 | mCenterX = currItem.getCenterX(); |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1297 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1298 | if (mViewItem[i] != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1299 | mViewItem[i].translateXScaledBy(adjustDiff); |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | } |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1303 | } else { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1304 | // fill the removed place by right shift |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 1305 | mCenterX -= offsetX; |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1306 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1307 | for (int i = removedItemId; i > 0; i--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1308 | mViewItem[i] = mViewItem[i - 1]; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | // pull data out from the DataAdapter for the first one. |
| 1312 | int curr = 0; |
| 1313 | int next = curr + 1; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1314 | if (mViewItem[next] != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1315 | mViewItem[curr] = buildItemFromData(mViewItem[next].getId() - 1); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1316 | } |
Angus Kong | 0980fe8 | 2013-05-28 17:48:04 -0700 | [diff] [blame] | 1317 | |
| 1318 | // Translate the views to their original places. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1319 | for (int i = removedItemId; i >= 0; i--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1320 | if (mViewItem[i] != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1321 | mViewItem[i].setTranslationX(-offsetX); |
Angus Kong | 0980fe8 | 2013-05-28 17:48:04 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1326 | int transY = getHeight() / 8; |
| 1327 | if (removedItem.getTranslationY() < 0) { |
| 1328 | transY = -transY; |
| 1329 | } |
| 1330 | removedItem.animateTranslationY(removedItem.getTranslationY() + transY, |
| 1331 | GEOMETRY_ADJUST_TIME_MS, mViewAnimInterpolator); |
| 1332 | removedItem.animateAlpha(0f, GEOMETRY_ADJUST_TIME_MS, mViewAnimInterpolator); |
| 1333 | postDelayed(new Runnable() { |
| 1334 | @Override |
| 1335 | public void run() { |
| 1336 | removedItem.removeViewFromHierarchy(false); |
| 1337 | } |
| 1338 | }, GEOMETRY_ADJUST_TIME_MS); |
| 1339 | |
| 1340 | adjustChildZOrder(); |
| 1341 | invalidate(); |
| 1342 | |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1343 | // Now, slide every one back. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1344 | if (mViewItem[mCurrentItem] == null) { |
| 1345 | return; |
| 1346 | } |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1347 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1348 | if (mViewItem[i] != null |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1349 | && mViewItem[i].getTranslationX() != 0f) { |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 1350 | slideViewBack(mViewItem[i]); |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1353 | if (isCurrentItemCentered() && isViewTypeSticky(mViewItem[mCurrentItem])) { |
Angus Kong | 6ddd31b | 2013-10-08 22:34:02 -0700 | [diff] [blame] | 1354 | // Special case for scrolling onto the camera preview after removal. |
| 1355 | mController.goToFullScreen(); |
| 1356 | } |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1359 | // returns -1 on failure. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1360 | private int findItemByDataID(int dataID) { |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1361 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1362 | if (mViewItem[i] != null |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1363 | && mViewItem[i].getId() == dataID) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1364 | return i; |
| 1365 | } |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1366 | } |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
| 1370 | private void updateInsertion(int dataID) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1371 | int insertedItemId = findItemByDataID(dataID); |
| 1372 | if (insertedItemId == -1) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1373 | // Not in the current item buffers. Check if it's inserted |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1374 | // at the end. |
| 1375 | if (dataID == mDataAdapter.getTotalNumber() - 1) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1376 | int prev = findItemByDataID(dataID - 1); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1377 | if (prev >= 0 && prev < BUFFER_SIZE - 1) { |
| 1378 | // The previous data is in the buffer and we still |
| 1379 | // have room for the inserted data. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1380 | insertedItemId = prev + 1; |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | // adjust the data id to be consistent |
| 1386 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1387 | if (mViewItem[i] == null || mViewItem[i].getId() < dataID) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1388 | continue; |
| 1389 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1390 | mViewItem[i].setId(mViewItem[i].getId() + 1); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1391 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1392 | if (insertedItemId == -1) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1393 | return; |
| 1394 | } |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1395 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1396 | final ImageData data = mDataAdapter.getImageData(dataID); |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 1397 | Point dim = CameraUtil |
| 1398 | .resizeToFill(data.getWidth(), data.getHeight(), data.getRotation(), |
| 1399 | getMeasuredWidth(), getMeasuredHeight()); |
| 1400 | final int offsetX = dim.x + mViewGapInPixel; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1401 | ViewItem viewItem = buildItemFromData(dataID); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1402 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1403 | if (insertedItemId >= mCurrentItem) { |
| 1404 | if (insertedItemId == mCurrentItem) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1405 | viewItem.setLeftPosition(mViewItem[mCurrentItem].getLeftPosition()); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1406 | } |
| 1407 | // Shift right to make rooms for newly inserted item. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1408 | removeItem(BUFFER_SIZE - 1); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1409 | for (int i = BUFFER_SIZE - 1; i > insertedItemId; i--) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1410 | mViewItem[i] = mViewItem[i - 1]; |
| 1411 | if (mViewItem[i] != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1412 | mViewItem[i].setTranslationX(-offsetX); |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 1413 | slideViewBack(mViewItem[i]); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | } else { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1417 | // Shift left. Put the inserted data on the left instead of the |
| 1418 | // found position. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1419 | --insertedItemId; |
| 1420 | if (insertedItemId < 0) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1421 | return; |
| 1422 | } |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1423 | removeItem(0); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1424 | for (int i = 1; i <= insertedItemId; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1425 | if (mViewItem[i] != null) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1426 | mViewItem[i].setTranslationX(offsetX); |
Angus Kong | edbba62 | 2013-09-12 14:49:48 -0700 | [diff] [blame] | 1427 | slideViewBack(mViewItem[i]); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1428 | mViewItem[i - 1] = mViewItem[i]; |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1433 | mViewItem[insertedItemId] = viewItem; |
| 1434 | viewItem.setAlpha(0f); |
| 1435 | viewItem.setTranslationY(getHeight() / 8); |
| 1436 | slideViewBack(viewItem); |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 1437 | adjustChildZOrder(); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1438 | invalidate(); |
| 1439 | } |
| 1440 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1441 | private void setDataAdapter(DataAdapter adapter) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1442 | mDataAdapter = adapter; |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 1443 | int maxEdge = (int) ((float) Math.max(this.getHeight(), this.getWidth()) |
| 1444 | * FILM_STRIP_SCALE); |
| 1445 | mDataAdapter.suggestViewSizeBound(maxEdge, maxEdge); |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1446 | mDataAdapter.setListener(new DataAdapter.Listener() { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1447 | @Override |
| 1448 | public void onDataLoaded() { |
| 1449 | reload(); |
| 1450 | } |
| 1451 | |
| 1452 | @Override |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1453 | public void onDataUpdated(DataAdapter.UpdateReporter reporter) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1454 | update(reporter); |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1458 | public void onDataInserted(int dataId, ImageData data) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1459 | if (mViewItem[mCurrentItem] == null) { |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1460 | // empty now, simply do a reload. |
| 1461 | reload(); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1462 | } else { |
| 1463 | updateInsertion(dataId); |
Angus Kong | e78541f | 2013-05-28 12:30:33 -0700 | [diff] [blame] | 1464 | } |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1465 | if (mListener != null) { |
| 1466 | mListener.onDataFocusChanged(dataId, getCurrentId()); |
| 1467 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | @Override |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1471 | public void onDataRemoved(int dataId, ImageData data) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1472 | animateItemRemoval(dataId, data); |
| 1473 | if (mListener != null) { |
| 1474 | mListener.onDataFocusChanged(dataId, getCurrentId()); |
| 1475 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1476 | } |
| 1477 | }); |
| 1478 | } |
| 1479 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1480 | private boolean inFilmstrip() { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1481 | return (mScale == FILM_STRIP_SCALE); |
| 1482 | } |
| 1483 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1484 | private boolean inFullScreen() { |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 1485 | return (mScale == FULL_SCREEN_SCALE); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1488 | private boolean inZoomView() { |
| 1489 | return (mScale > FULL_SCREEN_SCALE); |
| 1490 | } |
| 1491 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1492 | private boolean isCameraPreview() { |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1493 | return isViewTypeSticky(mViewItem[mCurrentItem]); |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1496 | private boolean inCameraFullscreen() { |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 1497 | return isDataAtCenter(0) && inFullScreen() |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1498 | && (isViewTypeSticky(mViewItem[mCurrentItem])); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | @Override |
| 1502 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Angus Kong | 7e9e4b7 | 2014-03-06 17:03:31 -0800 | [diff] [blame] | 1503 | if (mController.isScrolling()) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1504 | return true; |
| 1505 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1506 | |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 1507 | if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 1508 | mCheckToIntercept = true; |
| 1509 | mDown = MotionEvent.obtain(ev); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1510 | ViewItem viewItem = mViewItem[mCurrentItem]; |
Doris Liu | f7758b6 | 2013-06-06 16:54:18 -0700 | [diff] [blame] | 1511 | // Do not intercept touch if swipe is not enabled |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1512 | if (viewItem != null && !mDataAdapter.canSwipeInFullScreen(viewItem.getId())) { |
Doris Liu | f7758b6 | 2013-06-06 16:54:18 -0700 | [diff] [blame] | 1513 | mCheckToIntercept = false; |
| 1514 | } |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 1515 | return false; |
| 1516 | } else if (ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) { |
| 1517 | // Do not intercept touch once child is in zoom mode |
| 1518 | mCheckToIntercept = false; |
| 1519 | return false; |
| 1520 | } else { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1521 | if (!mCheckToIntercept) { |
| 1522 | return false; |
| 1523 | } |
| 1524 | if (ev.getEventTime() - ev.getDownTime() > SWIPE_TIME_OUT) { |
| 1525 | return false; |
| 1526 | } |
Doris Liu | fb57df1 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 1527 | int deltaX = (int) (ev.getX() - mDown.getX()); |
| 1528 | int deltaY = (int) (ev.getY() - mDown.getY()); |
| 1529 | if (ev.getActionMasked() == MotionEvent.ACTION_MOVE |
| 1530 | && deltaX < mSlop * (-1)) { |
| 1531 | // intercept left swipe |
| 1532 | if (Math.abs(deltaX) >= Math.abs(deltaY) * 2) { |
| 1533 | return true; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | return false; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | @Override |
| 1541 | public boolean onTouchEvent(MotionEvent ev) { |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 1542 | return mGestureRecognizer.onTouchEvent(ev); |
| 1543 | } |
| 1544 | |
| 1545 | FilmstripGestureRecognizer.Listener getGestureListener() { |
| 1546 | return mGestureListener; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1549 | private void updateViewItem(int itemID) { |
| 1550 | ViewItem item = mViewItem[itemID]; |
| 1551 | if (item == null) { |
| 1552 | Log.e(TAG, "trying to update an null item"); |
| 1553 | return; |
| 1554 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1555 | item.removeViewFromHierarchy(true); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1556 | |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1557 | ViewItem newItem = buildItemFromData(item.getId()); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1558 | if (newItem == null) { |
| 1559 | Log.e(TAG, "new item is null"); |
| 1560 | // keep using the old data. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1561 | item.addViewToHierarchy(); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1562 | return; |
| 1563 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1564 | newItem.copyAttributes(item); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1565 | mViewItem[itemID] = newItem; |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1566 | |
| 1567 | boolean stopScroll = clampCenterX(); |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1568 | if (stopScroll) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1569 | mController.stopScrolling(true); |
| 1570 | } |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 1571 | adjustChildZOrder(); |
| 1572 | invalidate(); |
Angus Kong | 8a2350a | 2013-12-16 15:02:34 -0800 | [diff] [blame] | 1573 | if (mListener != null) { |
| 1574 | mListener.onDataUpdated(newItem.getId()); |
| 1575 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1578 | /** Some of the data is changed. */ |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1579 | private void update(DataAdapter.UpdateReporter reporter) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1580 | // No data yet. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1581 | if (mViewItem[mCurrentItem] == null) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1582 | reload(); |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | // Check the current one. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1587 | ViewItem curr = mViewItem[mCurrentItem]; |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1588 | int dataId = curr.getId(); |
| 1589 | if (reporter.isDataRemoved(dataId)) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1590 | reload(); |
| 1591 | return; |
| 1592 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1593 | if (reporter.isDataUpdated(dataId)) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1594 | updateViewItem(mCurrentItem); |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1595 | final ImageData data = mDataAdapter.getImageData(dataId); |
Angus Kong | a5d6b24 | 2013-09-30 15:54:46 -0700 | [diff] [blame] | 1596 | if (!mIsUserScrolling && !mController.isScrolling()) { |
| 1597 | // If there is no scrolling at all, adjust mCenterX to place |
| 1598 | // the current item at the center. |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 1599 | Point dim = CameraUtil.resizeToFill(data.getWidth(), data.getHeight(), |
| 1600 | data.getRotation(), getMeasuredWidth(), getMeasuredHeight()); |
| 1601 | mCenterX = curr.getLeftPosition() + dim.x / 2; |
Angus Kong | a5d6b24 | 2013-09-30 15:54:46 -0700 | [diff] [blame] | 1602 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Check left |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1606 | for (int i = mCurrentItem - 1; i >= 0; i--) { |
| 1607 | curr = mViewItem[i]; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1608 | if (curr != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1609 | dataId = curr.getId(); |
| 1610 | if (reporter.isDataRemoved(dataId) || reporter.isDataUpdated(dataId)) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1611 | updateViewItem(i); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } else { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1614 | ViewItem next = mViewItem[i + 1]; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1615 | if (next != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1616 | mViewItem[i] = buildItemFromData(next.getId() - 1); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1617 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | // Check right |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1622 | for (int i = mCurrentItem + 1; i < BUFFER_SIZE; i++) { |
| 1623 | curr = mViewItem[i]; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1624 | if (curr != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1625 | dataId = curr.getId(); |
| 1626 | if (reporter.isDataRemoved(dataId) || reporter.isDataUpdated(dataId)) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1627 | updateViewItem(i); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1628 | } |
| 1629 | } else { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1630 | ViewItem prev = mViewItem[i - 1]; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1631 | if (prev != null) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1632 | mViewItem[i] = buildItemFromData(prev.getId() + 1); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1633 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1634 | } |
| 1635 | } |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 1636 | adjustChildZOrder(); |
Erin Dahlgren | b7a52a4 | 2013-10-01 11:00:12 -0700 | [diff] [blame] | 1637 | // Request a layout to find the measured width/height of the view first. |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1638 | requestLayout(); |
Erin Dahlgren | b7a52a4 | 2013-10-01 11:00:12 -0700 | [diff] [blame] | 1639 | // Update photo sphere visibility after metadata fully written. |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1642 | /** |
| 1643 | * The whole data might be totally different. Flush all and load from the |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1644 | * start. Filmstrip will be centered on the first item, i.e. the camera |
| 1645 | * preview. |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1646 | */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1647 | private void reload() { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1648 | mController.stopScrolling(true); |
| 1649 | mController.stopScale(); |
| 1650 | mDataIdOnUserScrolling = 0; |
ztenghui | 56e44af | 2013-10-15 15:31:00 -0700 | [diff] [blame] | 1651 | |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1652 | int prevId = -1; |
| 1653 | if (mViewItem[mCurrentItem] != null) { |
| 1654 | prevId = mViewItem[mCurrentItem].getId(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1655 | } |
ztenghui | 56e44af | 2013-10-15 15:31:00 -0700 | [diff] [blame] | 1656 | |
| 1657 | // Remove all views from the mViewItem buffer, except the camera view. |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1658 | for (int i = 0; i < mViewItem.length; i++) { |
| 1659 | if (mViewItem[i] == null) { |
| 1660 | continue; |
| 1661 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 1662 | mViewItem[i].removeViewFromHierarchy(false); |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | // Clear out the mViewItems and rebuild with camera in the center. |
| 1666 | Arrays.fill(mViewItem, null); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1667 | int dataNumber = mDataAdapter.getTotalNumber(); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1668 | if (dataNumber == 0) { |
| 1669 | return; |
| 1670 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1671 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1672 | mViewItem[mCurrentItem] = buildItemFromData(0); |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1673 | if (mViewItem[mCurrentItem] == null) { |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 1674 | return; |
| 1675 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1676 | mViewItem[mCurrentItem].setLeftPosition(0); |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1677 | for (int i = mCurrentItem + 1; i < BUFFER_SIZE; i++) { |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 1678 | mViewItem[i] = buildItemFromData(mViewItem[i - 1].getId() + 1); |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1679 | if (mViewItem[i] == null) { |
| 1680 | break; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1683 | |
| 1684 | // Ensure that the views in mViewItem will layout the first in the |
| 1685 | // center of the display upon a reload. |
| 1686 | mCenterX = -1; |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 1687 | mScale = FILM_STRIP_SCALE; |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 1688 | |
Angus Kong | 53aedc0 | 2013-10-10 16:46:04 -0700 | [diff] [blame] | 1689 | adjustChildZOrder(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1690 | invalidate(); |
Doris Liu | 742cd5b | 2013-09-12 16:17:43 -0700 | [diff] [blame] | 1691 | |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1692 | if (mListener != null) { |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 1693 | mListener.onDataReloaded(); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1694 | mListener.onDataFocusChanged(prevId, mViewItem[mCurrentItem].getId()); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1695 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1698 | private void promoteData(int itemID, int dataID) { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1699 | if (mListener != null) { |
Angus Kong | 26795a9 | 2014-02-20 09:18:09 -0800 | [diff] [blame] | 1700 | mListener.onFocusedDataPromoted(dataID); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 1704 | private void demoteData(int itemID, int dataID) { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1705 | if (mListener != null) { |
Angus Kong | 26795a9 | 2014-02-20 09:18:09 -0800 | [diff] [blame] | 1706 | mListener.onFocusedDataDemoted(dataID); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1710 | private void onEnterFilmstrip() { |
| 1711 | if (mListener != null) { |
| 1712 | mListener.onEnterFilmstrip(getCurrentId()); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | private void onLeaveFilmstrip() { |
| 1717 | if (mListener != null) { |
| 1718 | mListener.onLeaveFilmstrip(getCurrentId()); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | private void onEnterFullScreen() { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1723 | mFullScreenUIHidden = false; |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1724 | if (mListener != null) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1725 | mListener.onEnterFullScreenUiShown(getCurrentId()); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | private void onLeaveFullScreen() { |
| 1730 | if (mListener != null) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1731 | mListener.onLeaveFullScreenUiShown(getCurrentId()); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | private void onEnterFullScreenUiHidden() { |
| 1736 | mFullScreenUIHidden = true; |
| 1737 | if (mListener != null) { |
| 1738 | mListener.onEnterFullScreenUiHidden(getCurrentId()); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | private void onLeaveFullScreenUiHidden() { |
| 1743 | mFullScreenUIHidden = false; |
| 1744 | if (mListener != null) { |
| 1745 | mListener.onLeaveFullScreenUiHidden(getCurrentId()); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | private void onEnterZoomView() { |
| 1750 | if (mListener != null) { |
| 1751 | mListener.onEnterZoomView(getCurrentId()); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1755 | private void onLeaveZoomView() { |
| 1756 | mController.setSurroundingViewsVisible(true); |
| 1757 | } |
| 1758 | |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1759 | /** |
| 1760 | * MyController controls all the geometry animations. It passively tells the |
| 1761 | * geometry information on demand. |
| 1762 | */ |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1763 | private class MyController implements FilmstripController { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1764 | |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 1765 | private final ValueAnimator mScaleAnimator; |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1766 | private ValueAnimator mZoomAnimator; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 1767 | private AnimatorSet mFlingAnimator; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1768 | |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1769 | private final MyScroller mScroller; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1770 | private boolean mCanStopScroll; |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1771 | |
| 1772 | private final MyScroller.Listener mScrollerListener = |
| 1773 | new MyScroller.Listener() { |
| 1774 | @Override |
| 1775 | public void onScrollUpdate(int currX, int currY) { |
| 1776 | mCenterX = currX; |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1777 | |
| 1778 | boolean stopScroll = clampCenterX(); |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1779 | if (stopScroll) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1780 | mController.stopScrolling(true); |
| 1781 | } |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1782 | invalidate(); |
| 1783 | } |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 1784 | |
| 1785 | @Override |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1786 | public void onScrollEnd() { |
| 1787 | mCanStopScroll = true; |
| 1788 | if (mViewItem[mCurrentItem] == null) { |
| 1789 | return; |
| 1790 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1791 | snapInCenter(); |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 1792 | if (isCurrentItemCentered() |
| 1793 | && isViewTypeSticky(mViewItem[mCurrentItem])) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1794 | // Special case for the scrolling end on the camera |
| 1795 | // preview. |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1796 | goToFullScreen(); |
| 1797 | } |
| 1798 | } |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1799 | }; |
| 1800 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1801 | private final ValueAnimator.AnimatorUpdateListener mScaleAnimatorUpdateListener = |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1802 | new ValueAnimator.AnimatorUpdateListener() { |
| 1803 | @Override |
| 1804 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1805 | if (mViewItem[mCurrentItem] == null) { |
| 1806 | return; |
| 1807 | } |
| 1808 | mScale = (Float) animation.getAnimatedValue(); |
| 1809 | invalidate(); |
| 1810 | } |
| 1811 | }; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1812 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1813 | MyController(Context context) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1814 | TimeInterpolator decelerateInterpolator = new DecelerateInterpolator(1.5f); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1815 | mScroller = new MyScroller(mActivity, |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1816 | new Handler(mActivity.getMainLooper()), |
| 1817 | mScrollerListener, decelerateInterpolator); |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 1818 | mCanStopScroll = true; |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 1819 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1820 | mScaleAnimator = new ValueAnimator(); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 1821 | mScaleAnimator.addUpdateListener(mScaleAnimatorUpdateListener); |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 1822 | mScaleAnimator.setInterpolator(decelerateInterpolator); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1823 | mScaleAnimator.addListener(new Animator.AnimatorListener() { |
| 1824 | @Override |
| 1825 | public void onAnimationStart(Animator animator) { |
| 1826 | if (mScale == FULL_SCREEN_SCALE) { |
| 1827 | onLeaveFullScreen(); |
| 1828 | } else { |
| 1829 | if (mScale == FILM_STRIP_SCALE) { |
| 1830 | onLeaveFilmstrip(); |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | @Override |
| 1836 | public void onAnimationEnd(Animator animator) { |
| 1837 | if (mScale == FULL_SCREEN_SCALE) { |
| 1838 | onEnterFullScreen(); |
| 1839 | } else { |
| 1840 | if (mScale == FILM_STRIP_SCALE) { |
| 1841 | onEnterFilmstrip(); |
| 1842 | } |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | @Override |
| 1847 | public void onAnimationCancel(Animator animator) { |
| 1848 | |
| 1849 | } |
| 1850 | |
| 1851 | @Override |
| 1852 | public void onAnimationRepeat(Animator animator) { |
| 1853 | |
| 1854 | } |
| 1855 | }); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1858 | @Override |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1859 | public void setImageGap(int imageGap) { |
| 1860 | FilmstripView.this.setViewGap(imageGap); |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | @Override |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1864 | public int getCurrentId() { |
| 1865 | return FilmstripView.this.getCurrentId(); |
| 1866 | } |
| 1867 | |
| 1868 | @Override |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 1869 | public void setDataAdapter(DataAdapter adapter) { |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 1870 | FilmstripView.this.setDataAdapter(adapter); |
| 1871 | } |
| 1872 | |
| 1873 | @Override |
| 1874 | public boolean inFilmstrip() { |
| 1875 | return FilmstripView.this.inFilmstrip(); |
| 1876 | } |
| 1877 | |
| 1878 | @Override |
| 1879 | public boolean inFullScreen() { |
| 1880 | return FilmstripView.this.inFullScreen(); |
| 1881 | } |
| 1882 | |
| 1883 | @Override |
| 1884 | public boolean isCameraPreview() { |
| 1885 | return FilmstripView.this.isCameraPreview(); |
| 1886 | } |
| 1887 | |
| 1888 | @Override |
| 1889 | public boolean inCameraFullscreen() { |
| 1890 | return FilmstripView.this.inCameraFullscreen(); |
| 1891 | } |
| 1892 | |
| 1893 | @Override |
| 1894 | public void setListener(FilmstripListener l) { |
| 1895 | FilmstripView.this.setListener(l); |
| 1896 | } |
| 1897 | |
| 1898 | @Override |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1899 | public boolean isScrolling() { |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 1900 | return !mScroller.isFinished(); |
| 1901 | } |
| 1902 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1903 | @Override |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 1904 | public boolean isScaling() { |
| 1905 | return mScaleAnimator.isRunning(); |
Angus Kong | af24d28 | 2013-05-20 16:27:02 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1908 | private int estimateMinX(int dataID, int leftPos, int viewWidth) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1909 | return leftPos - (dataID + 100) * (viewWidth + mViewGapInPixel); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1912 | private int estimateMaxX(int dataID, int leftPos, int viewWidth) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 1913 | return leftPos |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1914 | + (mDataAdapter.getTotalNumber() - dataID + 100) |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1915 | * (viewWidth + mViewGapInPixel); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1918 | /** Zoom all the way in or out on the image at the given pivot point. */ |
| 1919 | private void zoomAt(final ViewItem current, final float focusX, final float focusY) { |
| 1920 | // End previous zoom animation, if any |
| 1921 | if (mZoomAnimator != null) { |
| 1922 | mZoomAnimator.end(); |
| 1923 | } |
| 1924 | // Calculate end scale |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 1925 | final float maxScale = getCurrentDataMaxScale(false); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1926 | final float endScale = mScale < maxScale - maxScale * TOLERANCE |
| 1927 | ? maxScale : FULL_SCREEN_SCALE; |
| 1928 | |
| 1929 | mZoomAnimator = new ValueAnimator(); |
| 1930 | mZoomAnimator.setFloatValues(mScale, endScale); |
| 1931 | mZoomAnimator.setDuration(ZOOM_ANIMATION_DURATION_MS); |
| 1932 | mZoomAnimator.addListener(new Animator.AnimatorListener() { |
| 1933 | @Override |
| 1934 | public void onAnimationStart(Animator animation) { |
| 1935 | if (mScale == FULL_SCREEN_SCALE) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1936 | if (mFullScreenUIHidden) { |
| 1937 | onLeaveFullScreenUiHidden(); |
| 1938 | } else { |
| 1939 | onLeaveFullScreen(); |
| 1940 | } |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1941 | setSurroundingViewsVisible(false); |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1942 | } else if (inZoomView()) { |
| 1943 | onLeaveZoomView(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1944 | } |
| 1945 | cancelLoadingZoomedImage(); |
| 1946 | } |
| 1947 | |
| 1948 | @Override |
| 1949 | public void onAnimationEnd(Animator animation) { |
| 1950 | // Make sure animation ends up having the correct scale even |
| 1951 | // if it is cancelled before it finishes |
| 1952 | if (mScale != endScale) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1953 | current.postScale(focusX, focusY, endScale / mScale, mDrawArea.width(), |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1954 | mDrawArea.height()); |
| 1955 | mScale = endScale; |
| 1956 | } |
| 1957 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1958 | if (inFullScreen()) { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1959 | setSurroundingViewsVisible(true); |
| 1960 | mZoomView.setVisibility(GONE); |
| 1961 | current.resetTransform(); |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 1962 | onEnterFullScreenUiHidden(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1963 | } else { |
| 1964 | mController.loadZoomedImage(); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1965 | onEnterZoomView(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 1966 | } |
| 1967 | mZoomAnimator = null; |
| 1968 | } |
| 1969 | |
| 1970 | @Override |
| 1971 | public void onAnimationCancel(Animator animation) { |
| 1972 | // Do nothing. |
| 1973 | } |
| 1974 | |
| 1975 | @Override |
| 1976 | public void onAnimationRepeat(Animator animation) { |
| 1977 | // Do nothing. |
| 1978 | } |
| 1979 | }); |
| 1980 | |
| 1981 | mZoomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 1982 | @Override |
| 1983 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1984 | float newScale = (Float) animation.getAnimatedValue(); |
| 1985 | float postScale = newScale / mScale; |
| 1986 | mScale = newScale; |
| 1987 | current.postScale(focusX, focusY, postScale, mDrawArea.width(), |
| 1988 | mDrawArea.height()); |
| 1989 | } |
| 1990 | }); |
| 1991 | mZoomAnimator.start(); |
| 1992 | } |
| 1993 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 1994 | @Override |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1995 | public void scroll(float deltaX) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 1996 | if (!stopScrolling(false)) { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 1997 | return; |
| 1998 | } |
| 1999 | mCenterX += deltaX; |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 2000 | |
| 2001 | boolean stopScroll = clampCenterX(); |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 2002 | if (stopScroll) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2003 | mController.stopScrolling(true); |
| 2004 | } |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2005 | invalidate(); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | @Override |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2009 | public void fling(float velocityX) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2010 | if (!stopScrolling(false)) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2013 | final ViewItem item = mViewItem[mCurrentItem]; |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 2014 | if (item == null) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2017 | |
| 2018 | float scaledVelocityX = velocityX / mScale; |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2019 | if (inFullScreen() && isViewTypeSticky(item) && scaledVelocityX < 0) { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2020 | // Swipe left in camera preview. |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2021 | goToFilmstrip(); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | int w = getWidth(); |
| 2025 | // Estimation of possible length on the left. To ensure the |
| 2026 | // velocity doesn't become too slow eventually, we add a huge number |
| 2027 | // to the estimated maximum. |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 2028 | int minX = estimateMinX(item.getId(), item.getLeftPosition(), w); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2029 | // Estimation of possible length on the right. Likewise, exaggerate |
| 2030 | // the possible maximum too. |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 2031 | int maxX = estimateMaxX(item.getId(), item.getLeftPosition(), w); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2032 | mScroller.fling(mCenterX, 0, (int) -velocityX, 0, minX, maxX, 0, 0); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 2035 | void flingInsideZoomView(float velocityX, float velocityY) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2036 | if (!inZoomView()) { |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2037 | return; |
| 2038 | } |
| 2039 | |
| 2040 | final ViewItem current = mViewItem[mCurrentItem]; |
| 2041 | if (current == null) { |
| 2042 | return; |
| 2043 | } |
| 2044 | |
| 2045 | final int factor = DECELERATION_FACTOR; |
| 2046 | // Deceleration curve for distance: |
| 2047 | // S(t) = s + (e - s) * (1 - (1 - t/T) ^ factor) |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2048 | // Need to find the ending distance (e), so that the starting |
| 2049 | // velocity is the velocity of fling. |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2050 | // Velocity is the derivative of distance |
| 2051 | // V(t) = (e - s) * factor * (-1) * (1 - t/T) ^ (factor - 1) * (-1/T) |
| 2052 | // = (e - s) * factor * (1 - t/T) ^ (factor - 1) / T |
| 2053 | // Since V(0) = V0, we have e = T / factor * V0 + s |
| 2054 | |
| 2055 | // Duration T should be long enough so that at the end of the fling, |
| 2056 | // image moves at 1 pixel/s for about P = 50ms = 0.05s |
| 2057 | // i.e. V(T - P) = 1 |
| 2058 | // V(T - P) = V0 * (1 - (T -P) /T) ^ (factor - 1) = 1 |
| 2059 | // T = P * V0 ^ (1 / (factor -1)) |
| 2060 | |
| 2061 | final float velocity = Math.max(Math.abs(velocityX), Math.abs(velocityY)); |
| 2062 | // Dynamically calculate duration |
| 2063 | final float duration = (float) (FLING_COASTING_DURATION_S |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2064 | * Math.pow(velocity, (1f / (factor - 1f)))); |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2065 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2066 | final float translationX = current.getTranslationX() * mScale; |
| 2067 | final float translationY = current.getTranslationY() * mScale; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2068 | |
| 2069 | final ValueAnimator decelerationX = ValueAnimator.ofFloat(translationX, |
| 2070 | translationX + duration / factor * velocityX); |
| 2071 | final ValueAnimator decelerationY = ValueAnimator.ofFloat(translationY, |
| 2072 | translationY + duration / factor * velocityY); |
| 2073 | |
| 2074 | decelerationY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 2075 | @Override |
| 2076 | public void onAnimationUpdate(ValueAnimator animation) { |
| 2077 | float transX = (Float) decelerationX.getAnimatedValue(); |
| 2078 | float transY = (Float) decelerationY.getAnimatedValue(); |
| 2079 | |
| 2080 | current.updateTransform(transX, transY, mScale, |
| 2081 | mScale, mDrawArea.width(), mDrawArea.height()); |
| 2082 | } |
| 2083 | }); |
| 2084 | |
| 2085 | mFlingAnimator = new AnimatorSet(); |
| 2086 | mFlingAnimator.play(decelerationX).with(decelerationY); |
| 2087 | mFlingAnimator.setDuration((int) (duration * 1000)); |
| 2088 | mFlingAnimator.setInterpolator(new TimeInterpolator() { |
| 2089 | @Override |
| 2090 | public float getInterpolation(float input) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2091 | return (float) (1.0f - Math.pow((1.0f - input), factor)); |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2092 | } |
| 2093 | }); |
| 2094 | mFlingAnimator.addListener(new Animator.AnimatorListener() { |
| 2095 | private boolean mCancelled = false; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2096 | |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2097 | @Override |
| 2098 | public void onAnimationStart(Animator animation) { |
| 2099 | |
| 2100 | } |
| 2101 | |
| 2102 | @Override |
| 2103 | public void onAnimationEnd(Animator animation) { |
| 2104 | if (!mCancelled) { |
| 2105 | loadZoomedImage(); |
| 2106 | } |
| 2107 | mFlingAnimator = null; |
| 2108 | } |
| 2109 | |
| 2110 | @Override |
| 2111 | public void onAnimationCancel(Animator animation) { |
| 2112 | mCancelled = true; |
| 2113 | } |
| 2114 | |
| 2115 | @Override |
| 2116 | public void onAnimationRepeat(Animator animation) { |
| 2117 | |
| 2118 | } |
| 2119 | }); |
| 2120 | mFlingAnimator.start(); |
| 2121 | } |
| 2122 | |
| 2123 | @Override |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2124 | public boolean stopScrolling(boolean forced) { |
| 2125 | if (!isScrolling()) { |
| 2126 | return true; |
| 2127 | } else if (!mCanStopScroll && !forced) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2128 | return false; |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 2129 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2130 | mScroller.forceFinished(true); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2131 | return true; |
| 2132 | } |
| 2133 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2134 | private void stopScale() { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2135 | mScaleAnimator.cancel(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2138 | @Override |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2139 | public void scrollToPosition(int position, int duration, boolean interruptible) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2140 | if (mViewItem[mCurrentItem] == null) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2141 | return; |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 2142 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2143 | mCanStopScroll = interruptible; |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2144 | mScroller.startScroll(mCenterX, 0, position - mCenterX, 0, duration); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2147 | @Override |
| 2148 | public boolean goToNextItem() { |
Alan Newberger | 8099a37 | 2014-03-24 17:17:38 -0700 | [diff] [blame] | 2149 | return goToItem(mCurrentItem + 1); |
| 2150 | } |
| 2151 | |
| 2152 | @Override |
| 2153 | public boolean goToPreviousItem() { |
| 2154 | return goToItem(mCurrentItem - 1); |
| 2155 | } |
| 2156 | |
| 2157 | private boolean goToItem(int itemIndex) { |
| 2158 | final ViewItem nextItem = mViewItem[itemIndex]; |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2159 | if (nextItem == null) { |
| 2160 | return false; |
| 2161 | } |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2162 | stopScrolling(true); |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2163 | scrollToPosition(nextItem.getCenterX(), GEOMETRY_ADJUST_TIME_MS * 2, false); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2164 | |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2165 | if (isViewTypeSticky(mViewItem[mCurrentItem])) { |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2166 | // Special case when moving from camera preview. |
| 2167 | scaleTo(FILM_STRIP_SCALE, GEOMETRY_ADJUST_TIME_MS); |
| 2168 | } |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
| 2171 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2172 | private void scaleTo(float scale, int duration) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 2173 | if (mViewItem[mCurrentItem] == null) { |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 2174 | return; |
| 2175 | } |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2176 | stopScale(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2177 | mScaleAnimator.setDuration(duration); |
| 2178 | mScaleAnimator.setFloatValues(mScale, scale); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2179 | mScaleAnimator.start(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2182 | @Override |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2183 | public void goToFilmstrip() { |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2184 | if (mViewItem[mCurrentItem] == null) { |
| 2185 | return; |
| 2186 | } |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2187 | if (mScale == FILM_STRIP_SCALE) { |
| 2188 | return; |
| 2189 | } |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2190 | scaleTo(FILM_STRIP_SCALE, GEOMETRY_ADJUST_TIME_MS); |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2191 | |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2192 | final ViewItem currItem = mViewItem[mCurrentItem]; |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2193 | final ViewItem nextItem = mViewItem[mCurrentItem + 1]; |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2194 | if (currItem.getId() == 0 && isViewTypeSticky(currItem) && nextItem != null) { |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2195 | // Deal with the special case of swiping in camera preview. |
| 2196 | scrollToPosition(nextItem.getCenterX(), GEOMETRY_ADJUST_TIME_MS, false); |
| 2197 | } |
| 2198 | |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2199 | if (mScale == FILM_STRIP_SCALE) { |
| 2200 | onLeaveFilmstrip(); |
Doris Liu | a52f04c | 2013-06-06 11:39:58 -0700 | [diff] [blame] | 2201 | } |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | @Override |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2205 | public void goToFullScreen() { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2206 | if (inFullScreen()) { |
| 2207 | return; |
| 2208 | } |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2209 | scaleTo(FULL_SCREEN_SCALE, GEOMETRY_ADJUST_TIME_MS); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2212 | private void cancelFlingAnimation() { |
| 2213 | // Cancels flinging for zoomed images |
| 2214 | if (isFlingAnimationRunning()) { |
| 2215 | mFlingAnimator.cancel(); |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | private void cancelZoomAnimation() { |
| 2220 | if (isZoomAnimationRunning()) { |
| 2221 | mZoomAnimator.cancel(); |
| 2222 | } |
| 2223 | } |
| 2224 | |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2225 | private void setSurroundingViewsVisible(boolean visible) { |
| 2226 | // Hide everything on the left |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2227 | // TODO: Need to find a better way to toggle the visibility of views |
| 2228 | // around the current view. |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2229 | for (int i = 0; i < mCurrentItem; i++) { |
| 2230 | if (i == mCurrentItem || mViewItem[i] == null) { |
| 2231 | continue; |
| 2232 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2233 | mViewItem[i].setVisibility(visible ? VISIBLE : INVISIBLE); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 2237 | private Uri getCurrentUri() { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2238 | ViewItem curr = mViewItem[mCurrentItem]; |
| 2239 | if (curr == null) { |
| 2240 | return Uri.EMPTY; |
| 2241 | } |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 2242 | return mDataAdapter.getImageData(curr.getId()).getUri(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | /** |
| 2246 | * Here we only support up to 1:1 image zoom (i.e. a 100% view of the |
| 2247 | * actual pixels). The max scale that we can apply on the view should |
| 2248 | * make the view same size as the image, in pixels. |
| 2249 | */ |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 2250 | private float getCurrentDataMaxScale(boolean allowOverScale) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2251 | ViewItem curr = mViewItem[mCurrentItem]; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2252 | ImageData imageData = mDataAdapter.getImageData(curr.getId()); |
| 2253 | if (curr == null || !imageData |
| 2254 | .isUIActionSupported(ImageData.ACTION_ZOOM)) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2255 | return FULL_SCREEN_SCALE; |
| 2256 | } |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2257 | float imageWidth = imageData.getWidth(); |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 2258 | if (imageData.getRotation() == 90 |
| 2259 | || imageData.getRotation() == 270) { |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2260 | imageWidth = imageData.getHeight(); |
ztenghui | 06578b5 | 2013-10-07 14:54:55 -0700 | [diff] [blame] | 2261 | } |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 2262 | float scale = imageWidth / curr.getWidth(); |
| 2263 | if (allowOverScale) { |
| 2264 | // In addition to the scale we apply to the view for 100% view |
| 2265 | // (i.e. each pixel on screen corresponds to a pixel in image) |
| 2266 | // we allow scaling beyond that for better detail viewing. |
| 2267 | scale *= mOverScaleFactor; |
| 2268 | } |
| 2269 | return scale; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | private void loadZoomedImage() { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2273 | if (!inZoomView()) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2274 | return; |
| 2275 | } |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2276 | ViewItem curr = mViewItem[mCurrentItem]; |
Doris Liu | 87fc5e1 | 2013-09-16 13:38:24 -0700 | [diff] [blame] | 2277 | if (curr == null) { |
| 2278 | return; |
| 2279 | } |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2280 | ImageData imageData = mDataAdapter.getImageData(curr.getId()); |
| 2281 | if (!imageData.isUIActionSupported(ImageData.ACTION_ZOOM)) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2282 | return; |
| 2283 | } |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 2284 | Uri uri = getCurrentUri(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2285 | RectF viewRect = curr.getViewRect(); |
| 2286 | if (uri == null || uri == Uri.EMPTY) { |
| 2287 | return; |
| 2288 | } |
Angus Kong | c195e7a | 2014-02-20 16:56:37 -0800 | [diff] [blame] | 2289 | int orientation = imageData.getRotation(); |
Doris Liu | 87fc5e1 | 2013-09-16 13:38:24 -0700 | [diff] [blame] | 2290 | mZoomView.loadBitmap(uri, orientation, viewRect); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | private void cancelLoadingZoomedImage() { |
| 2294 | mZoomView.cancelPartialDecodingTask(); |
Angus Kong | 6d23f4c | 2013-05-22 16:26:07 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | @Override |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 2298 | public void goToFirstItem() { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2299 | if (mViewItem[mCurrentItem] == null) { |
| 2300 | return; |
| 2301 | } |
Doris Liu | 03b7541 | 2013-09-09 17:26:14 -0700 | [diff] [blame] | 2302 | resetZoomView(); |
Alan Newberger | 3f969c1 | 2013-08-23 10:10:30 -0700 | [diff] [blame] | 2303 | // TODO: animate to camera if it is still in the mViewItem buffer |
| 2304 | // versus a full reload which will perform an immediate transition |
| 2305 | reload(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2308 | public boolean inZoomView() { |
| 2309 | return FilmstripView.this.inZoomView(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2310 | } |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2311 | |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2312 | public boolean isFlingAnimationRunning() { |
| 2313 | return mFlingAnimator != null && mFlingAnimator.isRunning(); |
| 2314 | } |
| 2315 | |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2316 | public boolean isZoomAnimationRunning() { |
| 2317 | return mZoomAnimator != null && mZoomAnimator.isRunning(); |
| 2318 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2321 | private boolean isCurrentItemCentered() { |
| 2322 | return mViewItem[mCurrentItem].getCenterX() == mCenterX; |
| 2323 | } |
| 2324 | |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2325 | private static class MyScroller { |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2326 | public interface Listener { |
| 2327 | public void onScrollUpdate(int currX, int currY); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2328 | |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2329 | public void onScrollEnd(); |
| 2330 | } |
| 2331 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2332 | private final Handler mHandler; |
| 2333 | private final Listener mListener; |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2334 | |
| 2335 | private final Scroller mScroller; |
| 2336 | |
| 2337 | private final ValueAnimator mXScrollAnimator; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2338 | private final Runnable mScrollChecker = new Runnable() { |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2339 | @Override |
| 2340 | public void run() { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2341 | boolean newPosition = mScroller.computeScrollOffset(); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2342 | if (!newPosition) { |
| 2343 | mListener.onScrollEnd(); |
| 2344 | return; |
| 2345 | } |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2346 | mListener.onScrollUpdate(mScroller.getCurrX(), mScroller.getCurrY()); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2347 | mHandler.removeCallbacks(this); |
| 2348 | mHandler.post(this); |
| 2349 | } |
| 2350 | }; |
| 2351 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2352 | private final ValueAnimator.AnimatorUpdateListener mXScrollAnimatorUpdateListener = |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2353 | new ValueAnimator.AnimatorUpdateListener() { |
| 2354 | @Override |
| 2355 | public void onAnimationUpdate(ValueAnimator animation) { |
| 2356 | mListener.onScrollUpdate((Integer) animation.getAnimatedValue(), 0); |
| 2357 | } |
| 2358 | }; |
| 2359 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2360 | private final Animator.AnimatorListener mXScrollAnimatorListener = |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2361 | new Animator.AnimatorListener() { |
| 2362 | @Override |
| 2363 | public void onAnimationCancel(Animator animation) { |
| 2364 | // Do nothing. |
| 2365 | } |
| 2366 | |
| 2367 | @Override |
| 2368 | public void onAnimationEnd(Animator animation) { |
| 2369 | mListener.onScrollEnd(); |
| 2370 | } |
| 2371 | |
| 2372 | @Override |
| 2373 | public void onAnimationRepeat(Animator animation) { |
| 2374 | // Do nothing. |
| 2375 | } |
| 2376 | |
| 2377 | @Override |
| 2378 | public void onAnimationStart(Animator animation) { |
| 2379 | // Do nothing. |
| 2380 | } |
| 2381 | }; |
| 2382 | |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2383 | public MyScroller(Context ctx, Handler handler, Listener listener, |
| 2384 | TimeInterpolator interpolator) { |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2385 | mHandler = handler; |
| 2386 | mListener = listener; |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2387 | mScroller = new Scroller(ctx); |
| 2388 | mXScrollAnimator = new ValueAnimator(); |
| 2389 | mXScrollAnimator.addUpdateListener(mXScrollAnimatorUpdateListener); |
| 2390 | mXScrollAnimator.addListener(mXScrollAnimatorListener); |
| 2391 | mXScrollAnimator.setInterpolator(interpolator); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2392 | } |
| 2393 | |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2394 | public void fling( |
| 2395 | int startX, int startY, |
| 2396 | int velocityX, int velocityY, |
| 2397 | int minX, int maxX, |
| 2398 | int minY, int maxY) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2399 | mScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2400 | runChecker(); |
| 2401 | } |
| 2402 | |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2403 | public void startScroll(int startX, int startY, int dx, int dy) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2404 | mScroller.startScroll(startX, startY, dx, dy); |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2405 | runChecker(); |
| 2406 | } |
| 2407 | |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2408 | /** Only starts and updates scroll in x-axis. */ |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2409 | public void startScroll(int startX, int startY, int dx, int dy, int duration) { |
Angus Kong | 1f36cf1 | 2013-10-01 16:58:52 -0700 | [diff] [blame] | 2410 | mXScrollAnimator.cancel(); |
| 2411 | mXScrollAnimator.setDuration(duration); |
| 2412 | mXScrollAnimator.setIntValues(startX, startX + dx); |
| 2413 | mXScrollAnimator.start(); |
| 2414 | } |
| 2415 | |
| 2416 | public boolean isFinished() { |
| 2417 | return (mScroller.isFinished() && !mXScrollAnimator.isRunning()); |
| 2418 | } |
| 2419 | |
| 2420 | public void forceFinished(boolean finished) { |
| 2421 | mScroller.forceFinished(finished); |
| 2422 | if (finished) { |
| 2423 | mXScrollAnimator.cancel(); |
| 2424 | } |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | private void runChecker() { |
| 2428 | if (mHandler == null || mListener == null) { |
| 2429 | return; |
| 2430 | } |
| 2431 | mHandler.removeCallbacks(mScrollChecker); |
| 2432 | mHandler.post(mScrollChecker); |
| 2433 | } |
| 2434 | } |
| 2435 | |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 2436 | private class MyGestureReceiver implements FilmstripGestureRecognizer.Listener { |
Angus Kong | 69f0964 | 2013-12-13 18:25:22 -0800 | [diff] [blame] | 2437 | |
| 2438 | private static final int SCROLL_DIR_NONE = 0; |
| 2439 | private static final int SCROLL_DIR_VERTICAL = 1; |
| 2440 | private static final int SCROLL_DIR_HORIZONTAL = 2; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2441 | // Indicating the current trend of scaling is up (>1) or down (<1). |
| 2442 | private float mScaleTrend; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2443 | private float mMaxScale; |
Angus Kong | 69f0964 | 2013-12-13 18:25:22 -0800 | [diff] [blame] | 2444 | private int mScrollingDirection = SCROLL_DIR_NONE; |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2445 | private long mLastDownTime; |
| 2446 | private float mLastDownY; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2447 | |
| 2448 | @Override |
| 2449 | public boolean onSingleTapUp(float x, float y) { |
ztenghui | 8566dd7 | 2013-09-12 14:56:56 -0700 | [diff] [blame] | 2450 | ViewItem centerItem = mViewItem[mCurrentItem]; |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 2451 | if (inFilmstrip()) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 2452 | if (centerItem != null && centerItem.areaContains(x, y)) { |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2453 | mController.goToFullScreen(); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2454 | return true; |
| 2455 | } |
| 2456 | } else if (inFullScreen()) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2457 | if (mFullScreenUIHidden) { |
| 2458 | onLeaveFullScreenUiHidden(); |
| 2459 | onEnterFullScreen(); |
| 2460 | } else { |
| 2461 | onLeaveFullScreen(); |
| 2462 | onEnterFullScreenUiHidden(); |
| 2463 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2464 | return true; |
| 2465 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2466 | return false; |
| 2467 | } |
| 2468 | |
| 2469 | @Override |
Angus Kong | 7e1c5fe | 2013-08-14 16:44:51 -0700 | [diff] [blame] | 2470 | public boolean onDoubleTap(float x, float y) { |
Doris Liu | 5326f02 | 2013-09-27 14:12:00 -0700 | [diff] [blame] | 2471 | ViewItem current = mViewItem[mCurrentItem]; |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2472 | if (current == null) { |
| 2473 | return false; |
| 2474 | } |
| 2475 | if (inFilmstrip()) { |
Doris Liu | 5326f02 | 2013-09-27 14:12:00 -0700 | [diff] [blame] | 2476 | mController.goToFullScreen(); |
| 2477 | return true; |
| 2478 | } else if (mScale < FULL_SCREEN_SCALE || inCameraFullscreen()) { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2479 | return false; |
| 2480 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2481 | if (!mController.stopScrolling(false)) { |
| 2482 | return false; |
Doris Liu | e1d5ba4 | 2013-09-10 12:44:20 -0700 | [diff] [blame] | 2483 | } |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2484 | if (inFullScreen()) { |
| 2485 | mController.zoomAt(current, x, y); |
| 2486 | checkItemAtMaxSize(); |
| 2487 | return true; |
| 2488 | } else if (mScale > FULL_SCREEN_SCALE) { |
| 2489 | // In zoom view. |
| 2490 | mController.zoomAt(current, x, y); |
| 2491 | } |
| 2492 | return false; |
Angus Kong | 7e1c5fe | 2013-08-14 16:44:51 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | @Override |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2496 | public boolean onDown(float x, float y) { |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2497 | mLastDownTime = SystemClock.uptimeMillis(); |
| 2498 | mLastDownY = y; |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2499 | mController.cancelFlingAnimation(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2500 | if (!mController.stopScrolling(false)) { |
| 2501 | return false; |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2502 | } |
Doris Liu | b0288ec | 2013-11-04 14:03:28 -0800 | [diff] [blame] | 2503 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2504 | return true; |
| 2505 | } |
| 2506 | |
| 2507 | @Override |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2508 | public boolean onUp(float x, float y) { |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2509 | ViewItem currItem = mViewItem[mCurrentItem]; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2510 | if (currItem == null) { |
| 2511 | return false; |
| 2512 | } |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2513 | if (mController.isZoomAnimationRunning() || mController.isFlingAnimationRunning()) { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2514 | return false; |
| 2515 | } |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2516 | if (inZoomView()) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2517 | mController.loadZoomedImage(); |
| 2518 | return true; |
| 2519 | } |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2520 | float promoteHeight = getHeight() * PROMOTE_HEIGHT_RATIO; |
| 2521 | float velocityPromoteHeight = getHeight() * VELOCITY_PROMOTE_HEIGHT_RATIO; |
Angus Kong | 47721fa | 2013-08-14 15:24:00 -0700 | [diff] [blame] | 2522 | mIsUserScrolling = false; |
Angus Kong | 69f0964 | 2013-12-13 18:25:22 -0800 | [diff] [blame] | 2523 | mScrollingDirection = SCROLL_DIR_NONE; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2524 | // Finds items promoted/demoted. |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2525 | float speedY = Math.abs(y - mLastDownY) |
| 2526 | / (SystemClock.uptimeMillis() - mLastDownTime); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2527 | for (int i = 0; i < BUFFER_SIZE; i++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 2528 | if (mViewItem[i] == null) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2529 | continue; |
| 2530 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2531 | float transY = mViewItem[i].getTranslationY(); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2532 | if (transY == 0) { |
| 2533 | continue; |
| 2534 | } |
Angus Kong | 563a289 | 2013-09-09 17:15:01 -0700 | [diff] [blame] | 2535 | int id = mViewItem[i].getId(); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2536 | |
| 2537 | if (mDataAdapter.getImageData(id) |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2538 | .isUIActionSupported(ImageData.ACTION_DEMOTE) |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2539 | && ((transY > promoteHeight) |
| 2540 | || (transY > velocityPromoteHeight && speedY > PROMOTE_VELOCITY))) { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2541 | demoteData(i, id); |
| 2542 | } else if (mDataAdapter.getImageData(id) |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2543 | .isUIActionSupported(ImageData.ACTION_PROMOTE) |
Sam Judd | bcd1152 | 2014-03-12 15:48:18 -0700 | [diff] [blame] | 2544 | && (transY < -promoteHeight |
| 2545 | || (transY < -velocityPromoteHeight && speedY > PROMOTE_VELOCITY))) { |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2546 | promoteData(i, id); |
| 2547 | } else { |
Angus Kong | 9f02c87 | 2013-05-20 13:46:14 -0700 | [diff] [blame] | 2548 | // put the view back. |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2549 | slideViewBack(mViewItem[i]); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2552 | |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2553 | // The data might be changed. Re-check. |
| 2554 | currItem = mViewItem[mCurrentItem]; |
| 2555 | if (currItem == null) { |
| 2556 | return true; |
| 2557 | } |
| 2558 | |
Doris Liu | 5326f02 | 2013-09-27 14:12:00 -0700 | [diff] [blame] | 2559 | int currId = currItem.getId(); |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2560 | if (mCenterX > currItem.getCenterX() + CAMERA_PREVIEW_SWIPE_THRESHOLD && currId == 0 && |
| 2561 | isViewTypeSticky(currItem) && mDataIdOnUserScrolling == 0) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2562 | mController.goToFilmstrip(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2563 | // Special case to go from camera preview to the next photo. |
| 2564 | if (mViewItem[mCurrentItem + 1] != null) { |
| 2565 | mController.scrollToPosition( |
| 2566 | mViewItem[mCurrentItem + 1].getCenterX(), |
| 2567 | GEOMETRY_ADJUST_TIME_MS, false); |
| 2568 | } else { |
| 2569 | // No next photo. |
| 2570 | snapInCenter(); |
| 2571 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2572 | } |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2573 | if (isCurrentItemCentered() && currId == 0 && isViewTypeSticky(currItem)) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2574 | mController.goToFullScreen(); |
| 2575 | } else { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2576 | if (mDataIdOnUserScrolling == 0 && currId != 0) { |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2577 | // Special case to go to filmstrip when the user scroll away |
| 2578 | // from the camera preview and the current one is not the |
| 2579 | // preview anymore. |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2580 | mController.goToFilmstrip(); |
Doris Liu | 5326f02 | 2013-09-27 14:12:00 -0700 | [diff] [blame] | 2581 | mDataIdOnUserScrolling = currId; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2582 | } |
| 2583 | snapInCenter(); |
| 2584 | } |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2585 | return false; |
| 2586 | } |
| 2587 | |
| 2588 | @Override |
Angus Kong | 26795a9 | 2014-02-20 09:18:09 -0800 | [diff] [blame] | 2589 | public void onLongPress(float x, float y) { |
| 2590 | final int dataId = getCurrentId(); |
| 2591 | if (dataId == -1) { |
| 2592 | return; |
| 2593 | } |
| 2594 | mListener.onFocusedDataLongPressed(dataId); |
| 2595 | } |
| 2596 | |
| 2597 | @Override |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2598 | public boolean onScroll(float x, float y, float dx, float dy) { |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 2599 | final ViewItem currItem = mViewItem[mCurrentItem]; |
ztenghui | 7be0e2b | 2013-10-15 14:13:26 -0700 | [diff] [blame] | 2600 | if (currItem == null) { |
| 2601 | return false; |
| 2602 | } |
Seth Raphael | 455ba5a | 2014-02-13 15:10:06 -0800 | [diff] [blame] | 2603 | if (inFullScreen() && !mDataAdapter.canSwipeInFullScreen(currItem.getId())) { |
Angus Kong | 3d0b691 | 2013-08-09 13:05:55 -0700 | [diff] [blame] | 2604 | return false; |
| 2605 | } |
Doris Liu | b0288ec | 2013-11-04 14:03:28 -0800 | [diff] [blame] | 2606 | hideZoomView(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2607 | // When image is zoomed in to be bigger than the screen |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2608 | if (inZoomView()) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2609 | ViewItem curr = mViewItem[mCurrentItem]; |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2610 | float transX = curr.getTranslationX() * mScale - dx; |
| 2611 | float transY = curr.getTranslationY() * mScale - dy; |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2612 | curr.updateTransform(transX, transY, mScale, mScale, mDrawArea.width(), |
| 2613 | mDrawArea.height()); |
| 2614 | return true; |
| 2615 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2616 | int deltaX = (int) (dx / mScale); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2617 | // Forces the current scrolling to stop. |
| 2618 | mController.stopScrolling(true); |
| 2619 | if (!mIsUserScrolling) { |
| 2620 | mIsUserScrolling = true; |
| 2621 | mDataIdOnUserScrolling = mViewItem[mCurrentItem].getId(); |
| 2622 | } |
Angus Kong | 6284815 | 2013-11-08 17:25:29 -0800 | [diff] [blame] | 2623 | if (inFilmstrip()) { |
Angus Kong | 69f0964 | 2013-12-13 18:25:22 -0800 | [diff] [blame] | 2624 | // Disambiguate horizontal/vertical first. |
| 2625 | if (mScrollingDirection == SCROLL_DIR_NONE) { |
| 2626 | mScrollingDirection = (Math.abs(dx) > Math.abs(dy)) ? SCROLL_DIR_HORIZONTAL : |
| 2627 | SCROLL_DIR_VERTICAL; |
| 2628 | } |
| 2629 | if (mScrollingDirection == SCROLL_DIR_HORIZONTAL) { |
| 2630 | if (mCenterX == currItem.getCenterX() && currItem.getId() == 0 && dx < 0) { |
| 2631 | // Already at the beginning, don't process the swipe. |
| 2632 | mIsUserScrolling = false; |
| 2633 | mScrollingDirection = SCROLL_DIR_NONE; |
| 2634 | return false; |
| 2635 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2636 | mController.scroll(deltaX); |
| 2637 | } else { |
| 2638 | // Vertical part. Promote or demote. |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2639 | int hit = 0; |
| 2640 | Rect hitRect = new Rect(); |
| 2641 | for (; hit < BUFFER_SIZE; hit++) { |
Angus Kong | 8969a67 | 2013-08-19 23:25:48 -0700 | [diff] [blame] | 2642 | if (mViewItem[hit] == null) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2643 | continue; |
| 2644 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2645 | mViewItem[hit].getHitRect(hitRect); |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2646 | if (hitRect.contains((int) x, (int) y)) { |
| 2647 | break; |
| 2648 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2649 | } |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2650 | if (hit == BUFFER_SIZE) { |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 2651 | // Hit none. |
| 2652 | return true; |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2653 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2654 | |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2655 | ImageData data = mDataAdapter.getImageData(mViewItem[hit].getId()); |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2656 | float transY = mViewItem[hit].getTranslationY() - dy / mScale; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2657 | if (!data.isUIActionSupported(ImageData.ACTION_DEMOTE) && |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 2658 | transY > 0f) { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2659 | transY = 0f; |
| 2660 | } |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 2661 | if (!data.isUIActionSupported(ImageData.ACTION_PROMOTE) && |
Angus Kong | c02b13a | 2013-11-12 11:50:57 -0800 | [diff] [blame] | 2662 | transY < 0f) { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2663 | transY = 0f; |
| 2664 | } |
Angus Kong | 1f9db2d | 2014-01-09 00:56:35 -0800 | [diff] [blame] | 2665 | mViewItem[hit].setTranslationY(transY); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2666 | } |
| 2667 | } else if (inFullScreen()) { |
Angus Kong | 166e36f | 2013-12-03 08:54:42 -0800 | [diff] [blame] | 2668 | if (mViewItem[mCurrentItem] == null || (deltaX < 0 && mCenterX <= |
| 2669 | currItem.getCenterX() && currItem.getId() == 0)) { |
| 2670 | return false; |
| 2671 | } |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 2672 | // Multiplied by 1.2 to make it more easy to swipe. |
| 2673 | mController.scroll((int) (deltaX * 1.2)); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2674 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2675 | invalidate(); |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2676 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2677 | return true; |
| 2678 | } |
| 2679 | |
| 2680 | @Override |
| 2681 | public boolean onFling(float velocityX, float velocityY) { |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2682 | final ViewItem currItem = mViewItem[mCurrentItem]; |
| 2683 | if (currItem == null) { |
| 2684 | return false; |
| 2685 | } |
ztenghui | 7be0e2b | 2013-10-15 14:13:26 -0700 | [diff] [blame] | 2686 | if (!mDataAdapter.canSwipeInFullScreen(currItem.getId())) { |
| 2687 | return false; |
| 2688 | } |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2689 | if (inZoomView()) { |
Doris Liu | d70f0fb | 2013-10-04 13:22:09 -0700 | [diff] [blame] | 2690 | // Fling within the zoomed image |
| 2691 | mController.flingInsideZoomView(velocityX, velocityY); |
| 2692 | return true; |
| 2693 | } |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 2694 | if (Math.abs(velocityX) < Math.abs(velocityY)) { |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2695 | // ignore vertical fling. |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 2696 | return true; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2697 | } |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 2698 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2699 | // In full-screen, fling of a velocity above a threshold should go |
| 2700 | // to the next/prev photos |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2701 | if (mScale == FULL_SCREEN_SCALE) { |
| 2702 | int currItemCenterX = currItem.getCenterX(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2703 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2704 | if (velocityX > 0) { // left |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2705 | if (mCenterX > currItemCenterX) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2706 | // The visually previous item is actually the current |
| 2707 | // item. |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2708 | mController.scrollToPosition( |
| 2709 | currItemCenterX, GEOMETRY_ADJUST_TIME_MS, true); |
| 2710 | return true; |
| 2711 | } |
| 2712 | ViewItem prevItem = mViewItem[mCurrentItem - 1]; |
| 2713 | if (prevItem == null) { |
| 2714 | return false; |
| 2715 | } |
| 2716 | mController.scrollToPosition( |
| 2717 | prevItem.getCenterX(), GEOMETRY_ADJUST_TIME_MS, true); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2718 | } else { // right |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2719 | if (mController.stopScrolling(false)) { |
| 2720 | if (mCenterX < currItemCenterX) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2721 | // The visually next item is actually the current |
| 2722 | // item. |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2723 | mController.scrollToPosition( |
| 2724 | currItemCenterX, GEOMETRY_ADJUST_TIME_MS, true); |
| 2725 | return true; |
| 2726 | } |
| 2727 | final ViewItem nextItem = mViewItem[mCurrentItem + 1]; |
| 2728 | if (nextItem == null) { |
| 2729 | return false; |
| 2730 | } |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2731 | mController.scrollToPosition( |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2732 | nextItem.getCenterX(), GEOMETRY_ADJUST_TIME_MS, true); |
Angus Kong | ec2fb47 | 2013-12-10 01:06:43 -0800 | [diff] [blame] | 2733 | if (isViewTypeSticky(currItem)) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2734 | mController.goToFilmstrip(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2735 | } |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2736 | } |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2737 | } |
Angus Kong | 025136b | 2013-08-21 00:38:05 -0700 | [diff] [blame] | 2738 | } |
Angus Kong | 17e669d | 2013-09-05 16:12:22 -0700 | [diff] [blame] | 2739 | |
| 2740 | if (mScale == FILM_STRIP_SCALE) { |
| 2741 | mController.fling(velocityX); |
| 2742 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2743 | return true; |
| 2744 | } |
| 2745 | |
| 2746 | @Override |
| 2747 | public boolean onScaleBegin(float focusX, float focusY) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2748 | if (inCameraFullscreen()) { |
| 2749 | return false; |
| 2750 | } |
Doris Liu | b0288ec | 2013-11-04 14:03:28 -0800 | [diff] [blame] | 2751 | |
| 2752 | hideZoomView(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2753 | mScaleTrend = 1f; |
Doris Liu | 79dcb12 | 2013-09-16 12:21:21 -0700 | [diff] [blame] | 2754 | // If the image is smaller than screen size, we should allow to zoom |
| 2755 | // in to full screen size |
Doris Liu | 3179f6a | 2013-10-08 16:54:22 -0700 | [diff] [blame] | 2756 | mMaxScale = Math.max(mController.getCurrentDataMaxScale(true), FULL_SCREEN_SCALE); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2757 | return true; |
| 2758 | } |
| 2759 | |
| 2760 | @Override |
| 2761 | public boolean onScale(float focusX, float focusY, float scale) { |
Sascha Haeberling | f1f5186 | 2013-07-31 11:28:21 -0700 | [diff] [blame] | 2762 | if (inCameraFullscreen()) { |
| 2763 | return false; |
| 2764 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2765 | |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2766 | mScaleTrend = mScaleTrend * 0.3f + scale * 0.7f; |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2767 | float newScale = mScale * scale; |
| 2768 | if (mScale < FULL_SCREEN_SCALE && newScale < FULL_SCREEN_SCALE) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2769 | if (newScale <= FILM_STRIP_SCALE) { |
| 2770 | newScale = FILM_STRIP_SCALE; |
| 2771 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2772 | // Scaled view is smaller than or equal to screen size both |
| 2773 | // before and after scaling |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2774 | if (mScale != newScale) { |
| 2775 | if (mScale == FILM_STRIP_SCALE) { |
| 2776 | onLeaveFilmstrip(); |
| 2777 | } |
| 2778 | if (newScale == FILM_STRIP_SCALE) { |
| 2779 | onEnterFilmstrip(); |
| 2780 | } |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2781 | } |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2782 | mScale = newScale; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2783 | invalidate(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2784 | } else if (mScale < FULL_SCREEN_SCALE && newScale >= FULL_SCREEN_SCALE) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2785 | // Going from smaller than screen size to bigger than or equal |
| 2786 | // to screen size |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2787 | if (mScale == FILM_STRIP_SCALE) { |
| 2788 | onLeaveFilmstrip(); |
| 2789 | } |
Angus Kong | 4ff5a1a | 2013-08-14 16:07:54 -0700 | [diff] [blame] | 2790 | mScale = FULL_SCREEN_SCALE; |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2791 | onEnterFullScreen(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2792 | mController.setSurroundingViewsVisible(false); |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2793 | invalidate(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2794 | } else if (mScale >= FULL_SCREEN_SCALE && newScale < FULL_SCREEN_SCALE) { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 2795 | // Going from bigger than or equal to screen size to smaller |
| 2796 | // than screen size |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2797 | if (inFullScreen()) { |
| 2798 | if (mFullScreenUIHidden) { |
| 2799 | onLeaveFullScreenUiHidden(); |
| 2800 | } else { |
| 2801 | onLeaveFullScreen(); |
| 2802 | } |
| 2803 | } else { |
| 2804 | onLeaveZoomView(); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2805 | } |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2806 | mScale = newScale; |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2807 | onEnterFilmstrip(); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 2808 | invalidate(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2809 | } else { |
| 2810 | // Scaled view bigger than or equal to screen size both before |
| 2811 | // and after scaling |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2812 | if (!inZoomView()) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2813 | mController.setSurroundingViewsVisible(false); |
| 2814 | } |
| 2815 | ViewItem curr = mViewItem[mCurrentItem]; |
| 2816 | // Make sure the image is not overly scaled |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2817 | newScale = Math.min(newScale, mMaxScale); |
Doris Liu | 87fc5e1 | 2013-09-16 13:38:24 -0700 | [diff] [blame] | 2818 | if (newScale == mScale) { |
| 2819 | return true; |
| 2820 | } |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2821 | float postScale = newScale / mScale; |
| 2822 | curr.postScale(focusX, focusY, postScale, mDrawArea.width(), mDrawArea.height()); |
| 2823 | mScale = newScale; |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2824 | if (mScale == FULL_SCREEN_SCALE) { |
| 2825 | onEnterFullScreen(); |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2826 | } else { |
| 2827 | onEnterZoomView(); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2828 | } |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 2829 | checkItemAtMaxSize(); |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 2830 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2831 | return true; |
| 2832 | } |
| 2833 | |
| 2834 | @Override |
| 2835 | public void onScaleEnd() { |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2836 | if (mScale > FULL_SCREEN_SCALE + TOLERANCE) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2837 | return; |
| 2838 | } |
| 2839 | mController.setSurroundingViewsVisible(true); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2840 | if (mScale <= FILM_STRIP_SCALE + TOLERANCE) { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2841 | mController.goToFilmstrip(); |
Doris Liu | 121022c | 2013-09-03 16:03:16 -0700 | [diff] [blame] | 2842 | } else if (mScaleTrend > 1f || mScale > FULL_SCREEN_SCALE - TOLERANCE) { |
Angus Kong | 4567160 | 2014-01-13 15:06:17 -0800 | [diff] [blame] | 2843 | if (inZoomView()) { |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2844 | mScale = FULL_SCREEN_SCALE; |
Doris Liu | e1d5ba4 | 2013-09-10 12:44:20 -0700 | [diff] [blame] | 2845 | resetZoomView(); |
Doris Liu | 8de1311 | 2013-08-23 13:35:24 -0700 | [diff] [blame] | 2846 | } |
Angus Kong | 734598c | 2013-08-21 15:25:05 -0700 | [diff] [blame] | 2847 | mController.goToFullScreen(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2848 | } else { |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 2849 | mController.goToFilmstrip(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2850 | } |
Angus Kong | a6a6a73 | 2013-06-18 16:47:57 -0700 | [diff] [blame] | 2851 | mScaleTrend = 1f; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2852 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 2853 | } |
| 2854 | } |