blob: c53e1aedffdea4bdecd259e5c4ac497e9641a5f9 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.ui;
18
19import com.android.gallery3d.R;
20import com.android.gallery3d.anim.AlphaAnimation;
Owen Lina2fba682011-08-17 22:07:43 +080021import com.android.gallery3d.app.AlbumDataAdapter;
22import com.android.gallery3d.app.GalleryActivity;
23import com.android.gallery3d.data.MediaSet;
24
25import android.content.Context;
26import android.view.MotionEvent;
27import android.view.View.MeasureSpec;
28
29public class FilmStripView extends GLView implements SlotView.Listener,
30 ScrollBarView.Listener, UserInteractionListener {
31 @SuppressWarnings("unused")
32 private static final String TAG = "FilmStripView";
33
34 private static final int HIDE_ANIMATION_DURATION = 300; // 0.3 sec
35
36 public interface Listener {
37 void onSlotSelected(int slotIndex);
38 }
39
40 private int mTopMargin, mMidMargin, mBottomMargin;
41 private int mContentSize, mBarSize, mGripSize;
42 private AlbumView mAlbumView;
43 private ScrollBarView mScrollBarView;
44 private AlbumDataAdapter mAlbumDataAdapter;
45 private StripDrawer mStripDrawer;
46 private Listener mListener;
47 private UserInteractionListener mUIListener;
Owen Lina2fba682011-08-17 22:07:43 +080048 private NinePatchTexture mBackgroundTexture;
49
50 // The layout of FileStripView is
51 // topMargin
52 // ----+----+
53 // / +----+--\
54 // contentSize | | thumbSize
55 // \ +----+--/
56 // ----+----+
57 // midMargin
58 // ----+----+
59 // / +----+--\
60 // barSize | | gripSize
61 // \ +----+--/
62 // ----+----+
63 // bottomMargin
64 public FilmStripView(GalleryActivity activity, MediaSet mediaSet,
65 int topMargin, int midMargin, int bottomMargin, int contentSize,
66 int thumbSize, int barSize, int gripSize, int gripWidth) {
67 mTopMargin = topMargin;
68 mMidMargin = midMargin;
69 mBottomMargin = bottomMargin;
70 mContentSize = contentSize;
71 mBarSize = barSize;
72 mGripSize = gripSize;
73
74 mStripDrawer = new StripDrawer((Context) activity);
75 mAlbumView = new AlbumView(activity, thumbSize, thumbSize, thumbSize);
76 mAlbumView.setOverscrollEffect(SlotView.OVERSCROLL_SYSTEM);
77 mAlbumView.setSelectionDrawer(mStripDrawer);
78 mAlbumView.setListener(this);
79 mAlbumView.setUserInteractionListener(this);
80 mAlbumDataAdapter = new AlbumDataAdapter(activity, mediaSet);
81 addComponent(mAlbumView);
82 mScrollBarView = new ScrollBarView(activity.getAndroidContext(),
83 mGripSize, gripWidth);
84 mScrollBarView.setListener(this);
85 addComponent(mScrollBarView);
86
87 mAlbumView.setModel(mAlbumDataAdapter);
88 mBackgroundTexture = new NinePatchTexture(activity.getAndroidContext(),
89 R.drawable.navstrip_translucent);
Owen Lina2fba682011-08-17 22:07:43 +080090 }
91
92 public void setListener(Listener listener) {
93 mListener = listener;
94 }
95
96 public void setUserInteractionListener(UserInteractionListener listener) {
97 mUIListener = listener;
98 }
99
Owen Lina2fba682011-08-17 22:07:43 +0800100 public void show() {
Owen Linace280a2011-08-30 10:38:59 +0800101 if (getVisibility() == GLView.VISIBLE) return;
102 startAnimation(null);
103 setVisibility(GLView.VISIBLE);
Owen Lina2fba682011-08-17 22:07:43 +0800104 }
105
106 public void hide() {
Owen Linace280a2011-08-30 10:38:59 +0800107 if (getVisibility() == GLView.INVISIBLE) return;
108 AlphaAnimation animation = new AlphaAnimation(1, 0);
109 animation.setDuration(HIDE_ANIMATION_DURATION);
110 startAnimation(animation);
111 setVisibility(GLView.INVISIBLE);
Owen Lina2fba682011-08-17 22:07:43 +0800112 }
113
114 @Override
115 protected void onVisibilityChanged(int visibility) {
116 super.onVisibilityChanged(visibility);
117 if (visibility == GLView.VISIBLE) {
118 onUserInteraction();
119 }
120 }
121
122 @Override
123 protected void onMeasure(int widthSpec, int heightSpec) {
124 int height = mTopMargin + mContentSize + mMidMargin + mBarSize + mBottomMargin;
125 MeasureHelper.getInstance(this)
126 .setPreferredContentSize(MeasureSpec.getSize(widthSpec), height)
127 .measure(widthSpec, heightSpec);
128 }
129
130 @Override
131 protected void onLayout(
132 boolean changed, int left, int top, int right, int bottom) {
133 if (!changed) return;
134 mAlbumView.layout(0, mTopMargin, right - left, mTopMargin + mContentSize);
135 int barStart = mTopMargin + mContentSize + mMidMargin;
136 mScrollBarView.layout(0, barStart, right - left, barStart + mBarSize);
137 int width = right - left;
138 int height = bottom - top;
139 }
140
141 @Override
142 protected boolean onTouch(MotionEvent event) {
143 // consume all touch events on the "gray area", so they don't go to
144 // the photo view below. (otherwise you can scroll the picture through
145 // it).
146 return true;
147 }
148
149 @Override
150 protected boolean dispatchTouchEvent(MotionEvent event) {
Owen Lina2fba682011-08-17 22:07:43 +0800151 switch (event.getAction()) {
152 case MotionEvent.ACTION_DOWN:
153 case MotionEvent.ACTION_MOVE:
154 onUserInteractionBegin();
155 break;
156 case MotionEvent.ACTION_UP:
157 case MotionEvent.ACTION_CANCEL:
158 onUserInteractionEnd();
159 break;
160 }
161
162 return super.dispatchTouchEvent(event);
163 }
164
165 @Override
166 protected void render(GLCanvas canvas) {
Owen Lina2fba682011-08-17 22:07:43 +0800167 mBackgroundTexture.draw(canvas, 0, 0, getWidth(), getHeight());
168 super.render(canvas);
Owen Lina2fba682011-08-17 22:07:43 +0800169 }
170
171 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800172 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800173 public void onSingleTapUp(int slotIndex) {
174 mAlbumView.setFocusIndex(slotIndex);
175 mListener.onSlotSelected(slotIndex);
176 }
177
178 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800179 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800180 public void onLongTap(int slotIndex) {
181 onSingleTapUp(slotIndex);
182 }
183
184 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800185 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800186 public void onUserInteractionBegin() {
187 mUIListener.onUserInteractionBegin();
188 }
189
190 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800191 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800192 public void onUserInteractionEnd() {
193 mUIListener.onUserInteractionEnd();
194 }
195
196 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800197 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800198 public void onUserInteraction() {
199 mUIListener.onUserInteraction();
200 }
201
202 // Called by AlbumView
Owen Linace280a2011-08-30 10:38:59 +0800203 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800204 public void onScrollPositionChanged(int position, int total) {
205 mScrollBarView.setContentPosition(position, total);
206 }
207
208 // Called by ScrollBarView
Owen Linace280a2011-08-30 10:38:59 +0800209 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800210 public void onScrollBarPositionChanged(int position) {
211 mAlbumView.setScrollPosition(position);
212 }
213
214 public void setFocusIndex(int slotIndex) {
215 mAlbumView.setFocusIndex(slotIndex);
216 mAlbumView.makeSlotVisible(slotIndex);
217 }
218
219 public void setStartIndex(int slotIndex) {
220 mAlbumView.setStartIndex(slotIndex);
221 }
222
223 public void pause() {
224 mAlbumView.pause();
225 mAlbumDataAdapter.pause();
226 }
227
228 public void resume() {
229 mAlbumView.resume();
230 mAlbumDataAdapter.resume();
231 }
232}