我想在 imageview 内移动一个 textview,这样它就不会超出 imageview 的范围。如果我触摸并移动 textview,它应该跟随我的手指。但是如果我把它移到图像视图之外,我的文本视图不应该移到图像视图之外。
public boolean onTouch(View view, MotionEvent event)
{
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)view.getLayoutParams();
imgrect=new Rect(parent.getLeft(), parent.getTop(), parent.getRight(), parent.getBottom());
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
x = X - lParams.leftMargin;
y = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
int w=parent.getWidth() - imgrect.left-view.getWidth();
int h=parent.getHeight() - imgrect.top;//-view.getHeight();
if(lParams.leftMargin<1)
{
lParams.leftMargin = 1;
view.setLayoutParams(lParams);
}
else if(lParams.topMargin<1)
{
lParams.topMargin = 1;
view.setLayoutParams(lParams);
}
else if(lParams.leftMargin>w)
{
lParams.leftMargin = w-1;
view.setLayoutParams(lParams);
}
else if(lParams.topMargin>h)
{
lParams.topMargin = h-1;
view.setLayoutParams(lParams);
}
else
{
lParams.leftMargin = X - x;
lParams.topMargin = Y - y;
view.setLayoutParams(lParams);
}
break;
}
parent.invalidate();
return false;
}