Android View 拖动&插入 研究
2010-07-01 14:10:00 来源:WEB开发网if(iv !=null){
iv.setOnTouchListener(touchListener);
}
else {
//error, view is null!
}
}
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);
if(iv !=null){
iv.setOnTouchListener(touchListener);
}
else {
//error, view is null!
}
}
4. getLocation(View) 用于: 根据目标View 判断待插入的位置 即:哪2个相邻ImageView 之间 实现为:
Java代码
public int getLocation(View v){
for(int i=0;i
更多精彩
赞助商链接