My favorites
▼
|
Sign in
xlarge-demos
Sample apps for Android 3.0 tablet devices
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
PhotoAlbum
/
src
/
com
/
example
/
android
/
photoalbum
/
DeckLayout.java
r4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.photoalbum;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
/**
* Displays children as a deck. This layout handles gestures to show/hide
* the various cards of the deck.
*/
@SuppressWarnings({"UnusedDeclaration"})
public class DeckLayout extends FrameLayout {
private static final int INVALID_POINTER = -1;
private int mActivePointerId = INVALID_POINTER;
private boolean mIsTrackingGesture;
private float mMotionX;
private final int mTouchSlop;
private final int mMinimumVelocity;
private final int mMaximumVelocity;
private VelocityTracker mVelocityTracker;
private int mCurrentCard = 0;
private Bitmap mShadow;
private Rect mSrc;
private final Rect mDst = new Rect();
public DeckLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DeckLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = configuration.getScaledTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mShadow = BitmapFactory.decodeResource(context.getResources(), R.drawable.photo_shadow);
mSrc = new Rect(0, 0, mShadow.getWidth(), mShadow.getHeight());
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
final int count = getChildCount();
for (int i = 1; i < count; i++) {
final View child = getChildAt(i);
final int left = (int) (child.getX() + child.getWidth());
mDst.set(left, 0, left + mShadow.getWidth(), getHeight());
canvas.drawBitmap(mShadow, mSrc, mDst, null);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_MOVE && mIsTrackingGesture) {
return true;
}
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE: {
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
break;
}
final int pointerIndex = event.findPointerIndex(activePointerId);
final float x = event.getX(pointerIndex);
final int xDiff = (int) Math.abs(x - mMotionX);
if (xDiff > mTouchSlop) {
mIsTrackingGesture = true;
}
mMotionX = x;
} break;
case MotionEvent.ACTION_DOWN: {
if (mCurrentCard == getChildCount() - 1 && isInBottomChild(event)) {
break;
}
mActivePointerId = event.getPointerId(0);
mMotionX = event.getX(0);
} break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER;
mIsTrackingGesture = false;
} break;
case MotionEvent.ACTION_POINTER_UP: {
onSecondaryPointerUp(event);
} break;
}
return mIsTrackingGesture;
}
private boolean isInBottomChild(MotionEvent event) {
final float x = event.getX(0);
final float y = event.getY(0);
final View child = getChildAt(0);
return !(y < child.getTop() || y >= child.getBottom() ||
x < child.getLeft() || x >= child.getRight());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_UP: {
onSecondaryPointerUp(event);
} break;
case MotionEvent.ACTION_CANCEL: {
mVelocityTracker.clear();
mActivePointerId = INVALID_POINTER;
mIsTrackingGesture = false;
} break;
case MotionEvent.ACTION_UP: {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);
if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
revealCard(initialVelocity);
}
velocityTracker.clear();
mActivePointerId = INVALID_POINTER;
mIsTrackingGesture = false;
} break;
}
return true;
}
private void onSecondaryPointerUp(MotionEvent event) {
final int pointerIndex = event.getActionIndex();
final int pointerId = event.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mActivePointerId = event.getPointerId(newPointerIndex);
}
}
private void revealCard(int velocity) {
// Swipe to the left, show next card
if (velocity < 0) {
showNextCard();
} else {
showPreviousCard();
}
}
/**
* Shows the next card, making it visible in the process.
*/
private void showNextCard() {
if (mCurrentCard == getChildCount() - 1) return;
mCurrentCard++;
getChildAt(getChildCount() - 1 - mCurrentCard).setVisibility(VISIBLE);
for (int i = 0; i < mCurrentCard; i++) {
ObjectAnimator animator = ObjectAnimator.ofFloat(getChildAt(getChildCount() - 1 - i),
"x", (mCurrentCard - i) * -getWidth() / 3);
animator.setDuration(200);
animator.start();
}
}
/**
* Shows the previous card and hides the card that becomes invisible.
*/
private void showPreviousCard() {
if (mCurrentCard == 0) return;
mCurrentCard--;
ObjectAnimator animator = null;
for (int i = 0; i <= mCurrentCard; i++) {
animator = ObjectAnimator.ofFloat(getChildAt(getChildCount() - 1 - i),
"x", (mCurrentCard - i) * -getWidth() / 3);
animator.setDuration(200);
animator.start();
}
// Hides the bottom-most card
if (animator != null) {
final View previous = getChildAt(getChildCount() - 1 - mCurrentCard - 1);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
previous.setVisibility(INVISIBLE);
}
});
}
}
}
Show details
Hide details
Change log
r2
by romain...@android.com on Feb 22, 2011
Diff
Initial commit.
Go to:
/trunk/PhotoAlbum
...k/PhotoAlbum/AndroidManifest.xml
/trunk/PhotoAlbum/LICENSE_APACHE2
/trunk/PhotoAlbum/LICENSE_PHOTOS
/trunk/PhotoAlbum/README
/trunk/PhotoAlbum/res
/trunk/PhotoAlbum/res/drawable
/trunk/PhotoAlbum/res/drawable-mdpi
...able-mdpi/panel_background.9.png
...dpi/thumbnail_frame_normal.9.png
...pi/thumbnail_frame_pressed.9.png
...nk/PhotoAlbum/res/drawable-nodpi
...m/res/drawable-nodpi/photo_1.jpg
.../res/drawable-nodpi/photo_10.jpg
...rawable-nodpi/photo_10_small.jpg
...drawable-nodpi/photo_1_small.jpg
...m/res/drawable-nodpi/photo_2.jpg
...drawable-nodpi/photo_2_small.jpg
...m/res/drawable-nodpi/photo_3.jpg
...drawable-nodpi/photo_3_small.jpg
...m/res/drawable-nodpi/photo_4.jpg
...drawable-nodpi/photo_4_small.jpg
...m/res/drawable-nodpi/photo_5.jpg
...drawable-nodpi/photo_5_small.jpg
...m/res/drawable-nodpi/photo_6.jpg
...drawable-nodpi/photo_6_small.jpg
...m/res/drawable-nodpi/photo_7.jpg
...drawable-nodpi/photo_7_small.jpg
...m/res/drawable-nodpi/photo_8.jpg
...drawable-nodpi/photo_8_small.jpg
...m/res/drawable-nodpi/photo_9.jpg
...drawable-nodpi/photo_9_small.jpg
.../drawable-nodpi/photo_shadow.png
...res/drawable/thumbnail_frame.xml
/trunk/PhotoAlbum/res/layout
...um/res/layout/album_activity.xml
...oAlbum/res/layout/photo_info.xml
...oAlbum/res/layout/photo_item.xml
/trunk/PhotoAlbum/res/mipmap-hdpi
...map-hdpi/ic_launcher_gallery.png
/trunk/PhotoAlbum/res/mipmap-mdpi
...map-mdpi/ic_launcher_gallery.png
/trunk/PhotoAlbum/res/values
.../PhotoAlbum/res/values/attrs.xml
...PhotoAlbum/res/values/colors.xml
...oAlbum/res/values/dimensions.xml
...hotoAlbum/res/values/strings.xml
...PhotoAlbum/res/values/themes.xml
/trunk/PhotoAlbum/src
/trunk/PhotoAlbum/src/com
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 8125 bytes, 246 lines
View raw file
Powered by
Google Project Hosting