我正在尝试在 android 上的图像内拖动和调整大小的 textview。我能够在触摸 TextView 时拖动,但它会拖动到屏幕之外。你能帮我在图像中拖动 textview。代码在这里
public void drag(MotionEvent event, View v){
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
Log.v("touch","move");
params.topMargin = (int)event.getRawY() - (textView.getHeight());
params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);
int y= (int)event.getRawY();
int x=(int)event.getRawX();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
if(params.topMargin>=0 && params.leftMargin>=0 )
textView.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_UP:
{
Log.v("touch","up");
params.topMargin = (int)event.getRawY() - (textView.getHeight());
params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);
Log.v("touch", "params.topMargin"+params.topMargin);
Log.v("touch", " params.leftMargin"+ params.leftMargin);
if( ( params.topMargin>=0) && (params.leftMargin>=0))
textView.setLayoutParams(params);
// textView.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_DOWN:
{
textView.setLayoutParams(params);
break;
}
}
}