Android View 拖动&插入 研究
2010-07-01 14:10:00 来源:WEB开发网android:layout_height="100dip"
android:src="@drawable/beijing3_b"/>
< ImageButton
android:layout_width="100dip"
android:layout_height="100dip"
android:src="@drawable/beijing3_b"/>
< /LinearLayout>
2. 变量初始化
Java代码
lLayout = (LinearLayout)findViewById(R.id.linear);
lLayout = (LinearLayout)findViewById(R.id.linear);
3. 定义OnTouchListener 用于监听所有动作 并注册LinearLayour的所有View 实现为:
Java代码
touchListener = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch(action){
//鼠标按下 拖拉动作开始
case MotionEvent.ACTION_DOWN:
point1 = v.getTop();
startX = (int)event.getX();
startY = y - v.getTop();
break;
//鼠标移动 拖拉动作进行中
case MotionEvent.ACTION_MOVE:
v.layout(x - startX, y - startY, x + v.getWidth()
- startX, y - startY + v.getHeight());
v.bringToFront();
v.postInvalidate();
break;
//鼠标释放 拖拉动作结束
case MotionEvent.ACTION_UP:
point2 = v.getTop();
//计算插入位置 位于哪两个相邻View之间
int dest = getLocation(v);
//remove ori view, and then add view here
lLayout.removeView(v);
lLayout.addView(v, dest);
break;
}
return false;
}
};
//to listener all ImageView
for(int i=0;i
ImageView iv = (ImageView)lLayout.getChildAt(i);
更多精彩
赞助商链接