OSDN Git Service

T28367
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / IconFrameLayout.java
1 package com.yuji.ef;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Context;
7 import android.graphics.Color;
8 import android.graphics.Point;
9 import android.graphics.PorterDuff.Mode;
10 import android.graphics.Rect;
11 import android.util.AttributeSet;
12 import android.view.MotionEvent;
13 import android.view.View;
14 import android.view.View.OnLongClickListener;
15 import android.view.animation.Animation;
16 import android.view.animation.AnimationUtils;
17 import android.view.animation.Animation.AnimationListener;
18 import android.widget.FrameLayout;
19
20 public class IconFrameLayout extends FrameLayout implements OnLongClickListener {
21         private LabelIconView target = null;
22         private LabelIconView dest = null;
23         private List<LabelIconView> list = new ArrayList<LabelIconView>();
24         
25         private Animation anime;
26         private int startX;
27         private int startY;
28         private int currentX;
29         private int currentY;
30         private int offsetX;
31         private int offsetY;
32         private boolean longClickFlg = false;
33         private IconScrollView scrollView;
34         private Context context = this.getContext();
35
36         public IconFrameLayout(Context context) {
37                 super(context);
38         }
39
40         public IconFrameLayout(Context context, AttributeSet attrs) {
41                 super(context, attrs);
42         }
43
44         public IconFrameLayout(Context context, AttributeSet attrs, int defStyle) {
45                 super(context, attrs, defStyle);
46         }
47
48         @Override
49         protected void onLayout(boolean changed, int left, int top, int right,
50                         int bottom) {
51                 super.onLayout(changed, left, top, right, bottom);
52
53                 int size = this.getChildCount();
54                 for (int i = 0; i < size; i++) {
55                         LabelIconView vv = (LabelIconView) this.getChildAt(i);
56                         int x2 = vv.getInitX();
57                         int y2 = vv.getInitY();
58                         // vv.init(x2, y2);
59                         vv.layout(x2, y2, x2 + vv.getWidth(), y2 + vv.getHeight());
60                 }
61         }
62
63         public void addView(LabelIconView child) {
64                 super.addView(child);
65                 list.add(child);
66         }
67
68         public void removeView(LabelIconView child) {
69                 super.removeView(child);
70                 list.remove(child);
71         }
72
73         public void moveTop(LabelIconView child){
74                 this.removeView(child);
75                 this.addView(child);
76         }
77         
78         private LabelIconView getView(int x, int y, LabelIconView v, List<LabelIconView> list, boolean flag) {
79                 Rect rect = new Rect();
80
81                 if (v != null) {
82                         if (flag){
83                                 v.getHitRect(rect);             
84                                 if (rect.contains(x, y)) {
85                                         return v;
86                                 }
87                         }
88                 }
89                 for (LabelIconView view : list) {
90                         if (view != v) {
91                                 view.getHitRect(rect);          
92                                 if (rect.contains(x, y)) {
93                                         
94                                         return view;
95                                 }
96                         }
97                 }
98                 return null;
99         }
100
101         @Override
102         public boolean onTouchEvent(MotionEvent event) {
103                 try {
104                         int x = (int) event.getRawX();
105                         int y = (int) event.getRawY();
106
107                         int dx = scrollView.getLeft();
108                         int dy = scrollView.getTop();
109
110                         x -= dx;
111                         y -= dy;
112                         
113                         int sx = x + scrollView.getScrollX();
114                         int sy = y + scrollView.getScrollY();
115                         LabelIconView v = getView(sx, sy, target, list, true);
116                         LabelIconView obj = (LabelIconView) v;
117
118                         int size = list.size();
119                         
120                         if (target == null) {
121                                 target = obj;
122                         }
123                         if (target == null){
124                                 return super.onTouchEvent(event);                               
125                         }
126
127                         this.getParent().requestDisallowInterceptTouchEvent(true);                      
128                         
129                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
130                                 if (!longClickFlg) {
131                                         return super.onTouchEvent(event);
132                                 }
133                                 obj = target;
134                                 
135                                 int sh = scrollView.getHeight();
136                                 int lh = this.getHeight();
137                                 int maxY = lh - sh;
138                                 int py = scrollView.getScrollY();
139                                 int sdy = 10;
140                                 int N = 100;
141                                 if (y < N){
142                                         if (py > 0){
143                                                 if (py < sdy){
144                                                         sdy = py;
145                                                 }
146                                                 scrollView.smoothScrollTo(0, py - sdy);
147                                                 currentY -= sdy;
148                                         }
149                                 }
150                                 else if (sh - y < N){
151                                         if (py < maxY){
152                                                 if (maxY - py < sdy){
153                                                         sdy = maxY - py;
154                                                 }
155                                                 scrollView.smoothScrollTo(0, py + sdy);
156                                                 currentY += sdy;
157                                         }
158                                 }
159
160                                 int diffX = offsetX - x;
161                                 int diffY = offsetY - y;
162
163                                 currentX -= diffX;
164                                 currentY -= diffY;
165                                 // currentX = x;;
166                                 // currentY = y;
167                                 obj.layout(currentX, currentY, currentX + obj.getWidth(),
168                                                 currentY + obj.getHeight());
169
170                                 offsetX = x;
171                                 offsetY = y;
172
173                                 v = getView(sx, sy, target, list, false);
174                                 if (dest == null) {
175                                         if (v != null){
176                                                 dest = v;
177                                                 dest.setAlpha(128);
178                                         }
179                                 } else {
180                                         if (v != dest){
181                                                 dest.setAlpha(255);
182                                                 dest = v;
183                                                 if (dest != null){
184                                                         dest.setAlpha(128);
185                                                 }
186                                         }
187                                 }
188                         } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
189                                 if (obj == target){
190                                         offsetX = x;
191                                         offsetY = y;
192                                         // TODO
193                                         currentX = obj.getLeft();
194                                         currentY = obj.getTop();
195         
196                                         target.setColorFilter(Color.RED, Mode.LIGHTEN);
197         
198                                         scrollView.setScrollable(false);
199                                 }
200                                 else {
201                                         target.clearColorFilter();
202                                         target = null;
203                                 }
204                                 // return false;
205                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
206                                 if (!longClickFlg) {
207                                         return super.onTouchEvent(event);
208                                 }
209                                 obj = target;
210
211                                 obj.setAnimation(anime);
212                                 obj.startAnimation(anime);
213                                 // obj.setVisibility(View.GONE);
214                                 if (dest == null) {
215                                         // \8fÁ\82·\8fê\8d\87
216                                         // layout.removeView(obj);
217
218                                         // \83L\83\83\83\93\83Z\83\8b
219                                         int srcX = target.getInitX();
220                                         int srcY = target.getInitY();
221                                         target.layout(srcX, srcY, srcX + target.getWidth(), srcY
222                                                         + target.getHeight());
223                                         target.setAlpha(255);
224                                 } else {
225                                         int srcX = target.getInitX();
226                                         int srcY = target.getInitY();
227                                         int dstX = dest.getInitX();
228                                         int dstY = dest.getInitY();
229
230                                         target.layout(dstX, dstY, dstX + target.getWidth(), dstY
231                                                         + target.getHeight());
232                                         target.init(dstX, dstY);
233                                         target.setAlpha(255);
234                                         dest.layout(srcX, srcY, srcX + dest.getWidth(),
235                                                         srcY + dest.getHeight());
236                                         dest.init(srcX, srcY);
237                                         dest.setAlpha(255);
238                                 }
239
240                                 //this.getParent().requestDisallowInterceptTouchEvent(false);
241
242                                 // bt2.getHitRect(rect);
243                                 // if (rect.contains(x, y)) {
244                                 // isBt2Click = true;
245                                 // }
246                                 longClickFlg = false;
247                         }
248
249                         return super.onTouchEvent(event);
250                 } catch (Exception e) {
251                         e.printStackTrace();
252                         return false;
253                 }
254         }
255
256         @Override
257         public boolean onLongClick(View view) {
258                 if (target == null) {
259                         return false;
260                 }
261                 LabelIconView v = (LabelIconView) target;
262                 v.setAlpha(128);
263                 v.clearColorFilter();
264
265                 moveTop(target);
266                 
267                 longClickFlg = true;
268                 return true;
269         }
270
271         public void setScrollView(final IconScrollView scrollView) {
272                 this.scrollView = scrollView;
273                 this.setOnLongClickListener(this);
274                 //this.setLongClickable(true);
275                 
276                 anime = AnimationUtils.loadAnimation(context, R.anim.sample);
277                 anime.setAnimationListener(new AnimationListener() {
278                         public void onAnimationStart(Animation animation) {
279                         }
280
281                         public void onAnimationRepeat(Animation animation) {
282                         }
283
284                         public void onAnimationEnd(Animation animation) {
285                                 // if (isBt2Click) {
286                                 // bt2.performClick();
287                                 // isBt2Click = false;
288                                 // }
289                                 target = null;
290                                 dest = null;
291                                 
292                                 scrollView.setScrollable(true);
293                                 scrollView.requestDisallowInterceptTouchEvent(false);
294                                 scrollView.invalidate();
295                         }
296                 });
297         }
298
299         // @Override
300         // public boolean onTouchEvent(MotionEvent event) {
301         // // return super.onTouchEvent(event);
302         // return false;
303         // }
304
305         // @Override
306         // public boolean onKeyDown(int keyCode, KeyEvent event) {
307         // return true;
308         // }
309         //
310         // @Override
311         // public boolean onKeyLongPress(int keyCode, KeyEvent event) {
312         // return true;
313         // }
314         //
315         // @Override
316         // public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent
317         // event) {
318         // return true;
319         // }
320         //
321         // @Override
322         // public boolean onKeyPreIme(int keyCode, KeyEvent event) {
323         // return true;
324         // }
325         //
326         // @Override
327         // public boolean onKeyShortcut(int keyCode, KeyEvent event) {
328         // return true;
329         // }
330         //
331         // @Override
332         // public boolean onKeyUp(int keyCode, KeyEvent event) {
333         // return true;
334         // }
335 }