2
iam이 image2의 배경 아래 숨어서 드래그 후 image2 위에 image1을 설정해야 할 때. image1.setOnTouchListener (새 View.OnTouchListener() {이미지를 다른 이미지 위로 드래그하는 방법
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (LayoutParams) image1.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
image1.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
이있다 .XML 파일
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#888"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:src="@drawable/image" />
</FrameLayout>
xml의 interchange imageView1 및 imageView2 –
xml의 상호 교환 후 동일하게 작동합니다. – Bunny