5
내 응용 프로그램에서는 부모 레이아웃으로 만 이미지를 드래그하려고하지만 전체 장치 화면에서 드래그하고 있습니다. 이를 달성하기 위해 최선을 다했지만 아무 해결책도 찾지 못했습니다.안드로이드에서 뷰의 드래그 존을 어떻게 제한 할 수 있습니까?
여기에 내 xml 및 코드를 추가했습니다. 나는 이것을 찾았지만 해결책을 찾을 수 없었습니다. 누구든지이 문제를 해결하도록 도와 줄 수 있습니까?
나는 내가 달성하고 싶은 것에 관해 명확하게 그림에서 언급했다. y1과 y2 값에서 이미지 끌기를 제한하고 싶다.
내 XML :
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlayout"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/images">
<!-- cartoon image -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_1"
android:layout_centerInParent="true"
android:id="@+id/cartoon_image">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/play_btn"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:id="@+id/play_btn"
>
</RelativeLayout>
</RelativeLayout>
<!-- end of cartoon image -->
</RelativeLayout>
</RelativeLayout>
내 활동 :
public class MainActivity extends Activity {
RelativeLayout rlImages,Cartoon_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cartoon_image=(RelativeLayout)findViewById(R.id.cartoon_image);
rlImages=(RelativeLayout)findViewById(R.id.images);
rlImages.setOnDragListener(new MyDragListener());
Cartoon_image.setOnTouchListener(new MyTouchListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int dragAction = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.e("in entered","in enter");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.e("in exited","in exit");
break;
case DragEvent.ACTION_DROP:
Float s1=event.getX();
Float s2=event.getY();
int x=Integer.valueOf(s1.intValue());
int y=Integer.valueOf(s2.intValue());
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.e("xs"+(int)event.getX(),"xs"+(int)event.getX());
v.setVisibility(View.VISIBLE);
if (dropEventNotHandled(event)) {
v.setVisibility(View.VISIBLE);
}
view.setVisibility(View.VISIBLE);
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
}
}![I have added the image for better understanding,Please find it][1]
1 :
이 프로젝트를 보았을 때, 제 요구 사항과 매우 흡사합니다.이 작업을 수행하는 다른 간단한 방법이 있습니다. – user1891910