int startX,startY;//startX、startY是全局变量
params= (RelativeLayout.LayoutParams) bt4.getLayoutParams();
bt4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
//初始化开始的点
startX=(int)event.getRawX();
startY=(int)event.getRawY();
// Log.e(“startX:ACTION_DOWN”+startX,”startY:ACTION_DOWN”+startY);
break;
case MotionEvent.ACTION_MOVE:
//移动过程中的点位置
int newX = (int)event.getRawX();
int newY = (int)event.getRawY();
//差
int dX = (newX – startX);
int dY = (newY – startY);
//重新设置开始点
startX = newX;
startY = newY;
//设置四边的Margin
params.leftMargin += dX;
params.topMargin += dY;
//设置右下的margin可以将控件移出屏幕边界
params.rightMargin += -dX;
params.bottomMargin += -dX;
//将改变应用到控件
bt4.setLayoutParams(params);
//bt4.requestLayout();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
0 Comments