OSDN Git Service

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