Nella mia applicazione, voglio trascinare l'immagine solo nel suo layout genitore, ma sta trascinando l'intero schermo del dispositivo. Ho fatto del mio meglio per raggiungere questo obiettivo, ma non ho trovato alcuna soluzione.Come posso limitare il dragzone per una vista in Android?
Ho aggiunto il mio codice XML e il codice qui. Ho cercato questo, ma non sono riuscito a trovare la soluzione. Qualcuno può aiutarmi a risolvere questo problema?
Ho menzionato chiaramente nell'immagine cosa voglio ottenere. Voglio limitare il trascinamento dell'immagine in quei valori y1 e y2.
mio 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>
La mia attività:
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:
Ho visto questo progetto, è molto simile al mio requisito. Posso sapere che esiste un altro modo semplice per raggiungere questo obiettivo. – user1891910