Button b =newButton();
b.setOnTouchListener(newView.OnTouchListener() {
intlastX,lastY;
@Override
public booleanonTouch(View v,MotionEvent event) {
//TODO Auto-generated method stub
intea = event.getAction();
Log.i("TAG","Touch:"+ ea);
switch(ea) {
caseMotionEvent.ACTION_DOWN:
lastX= (int) event.getRawX();
lastY= (int) event.getRawY();
break;
/**
* layout(l,t,r,b)
* l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
* */
caseMotionEvent.ACTION_MOVE:
intdx = (int) event.getRawX() -lastX;
intdy = (int) event.getRawY() -lastY;
intleft = v.getLeft() + dx;
inttop = v.getTop() + dy;
intright = v.getRight() + dx;
intbottom = v.getBottom() + dy;
if(left <0) {
left =0;
right = left + v.getWidth();
}
if(right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if(top <0) {
top =0;
bottom = top + v.getHeight();
}
if(bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left,top,right,bottom);
Log.i("","position:"+ left +", "+ top +", "+ right +", "+ bottom);
lastX= (int) event.getRawX();
lastY= (int) event.getRawY();
break;
caseMotionEvent.ACTION_UP:
break;
}
return false;
}