0
imageview
을 화면 크기 내에서만 드래그 할 수 있습니다. 그러나 아래 코드의 문제는 왼쪽 및 위쪽 여백 만 imageview
을 중지하고, 오른쪽 및 아래쪽 여백에 if 문을 구현하여 뷰가 포커스를 벗어나지 않도록하는 방법입니다. 아래의 코드, 내가 대답을 자신을 발견하고 나는 또한 AbsoluteLayout
에 LinearLayout
을 바꾸 긴 조사 후 사전android : 레이아웃 크기 (sceen 크기) 내에서 드래그보기
public boolean onTouch(View view, MotionEvent event) {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float screenHeight = displaymetrics.heightPixels;
float screenWidth = displaymetrics.widthPixels;
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
if(view==image1){
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams)
view.getLayoutParams();
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
x1Delta = lParams.rightMargin;
y2Delta = lParams.bottomMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
.getLayoutParams();
//below leftMargin , topMargin works fine
if((x - xDelta) > 0 && (x - xDelta) < screenWidth){layoutParams.leftMargin = x - xDelta;}
if((y - yDelta) > 0 && (y - yDelta) < screenHeight){layoutParams.topMargin = y - yDelta;}
layoutParams.rightMargin = 0;//how to make this stops the imageview on rightMargin
layoutParams.bottomMargin = 0;//how to make this stops the imageview on bottomMargin
view.setLayoutParams(layoutParams);
break;
}
mainLayout.invalidate();
}
return true;
}
};