OSDN Git Service

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