2017-11-27 4 views
0

Ich habe zwei ImageViews und ein Bild auf einem ImageView und andere ImageView ist leer. Ich möchte Bild ziehen und auf leere Bildansicht fallen lassen. Ich möchte auch, dass das Bild wieder an seinen ersten Platz zurückkehrt. Einfach gesagt, ich möchte das Bild zwischen zwei ImageViews bewegen. Ich denke, wenn ich das Bild ziehe, ziehe ich auch das ImageView. Ich möchte nur ein Bild ziehen, nicht das ImageVew, weil ich das Bild an seinen ersten Platz zurückbewegen muss. Ich versuchte dies und konfrontiert Problem, jede einfache Lösung benötigt?Drag & Drop des Bildes

private final class ChoiceTouchListner implements View.OnTouchListener{ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if((event.getAction()==MotionEvent.ACTION_DOWN) && ((ImageView)v).getDrawable()!=null){ 
      ClipData data= ClipData.newPlainText("",""); 
      View.DragShadowBuilder shadowBuilder= new View.DragShadowBuilder(v); 
      v.startDrag(data,shadowBuilder,v,0); 
      return true; 
     }else{ return false;} 

    } 
} 

/////////////////////////////////////////// /////////////////////////////////////////

private class ChoiceDragListner implements View.OnDragListener{ 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     switch (event.getAction()){ 
      case DragEvent.ACTION_DRAG_STARTED: 
       break; //no action necesary 
      case DragEvent.ACTION_DRAG_ENTERED: 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       break; 
      case DragEvent.ACTION_DROP: 
       ImageView imageView= (ImageView) event.getLocalState(); //the source image 
       ((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.blue)); 
       // ((ImageView)v).setImageDrawable(null); //replace source by empty 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       break; 
     } 

     return true; 
    } 
} 

Antwort

0

Android Ziehen und Ablegen - Beispiel

Sie können dieses Beispiel versuchen.

activity_main.xml

<LinearLayout 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:orientation="vertical" 
    android:gravity="center" 
    tools:context=".MainActivity" > 
<LinearLayout 
    android:id="@+id/bottomlinear" 
    android:layout_width="match_parent" 
    android:layout_height="400px" 
    android:gravity="center" 
    android:background="#00ff00" 
    android:orientation="vertical" > 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/one" 
     android:textSize="30sp" 
     android:text="Drag Me" /> 

</LinearLayout> 
<LinearLayout 
    android:id="@+id/bottomlinear" 
    android:layout_width="match_parent" 
    android:layout_height="400px" 
    android:gravity="center" 
    android:background="#00ffff" 
    android:orientation="vertical" > 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/drop" 
    android:textSize="30sp" 
    android:text="Drop Here" /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/Total" 
    android:textSize="20sp" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/Sucess" 
     android:textSize="20sp" /> 

</LinearLayout> 

</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 
Button drag; 
LinearLayout drop; 
TextView text,sucess; 
int total , failure = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    drag = (Button)findViewById(R.id.one); 
    drop = (LinearLayout)findViewById(R.id.bottomlinear); 
    text = (TextView)findViewById(R.id.Total); 
    sucess = (TextView)findViewById(R.id.Sucess); 

    drop.setOnDragListener(new View.OnDragListener() { 

     @Override 
     public boolean onDrag(View v, DragEvent event) { 
      // TODO Auto-generated method stub 
     final int action = event.getAction(); 
     switch(action) { 

     case DragEvent.ACTION_DRAG_STARTED: 
     break; 

     case DragEvent.ACTION_DRAG_EXITED: 
      break; 

     case DragEvent.ACTION_DRAG_ENTERED: 
      break; 

     case DragEvent.ACTION_DROP:{ 
      failure = failure+1; 
      return(true); 
     } 

     case DragEvent.ACTION_DRAG_ENDED:{ 
      total = total +1; 
      int suc = total - failure; 
      sucess.setText("Sucessful Drops :"+suc); 
      text.setText("Total Drops: "+total); 
      return(true); 

     } 

     default: 
     break; 
     } 
     return true; 
     }}); 
     drag.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent arg1) { 
      // TODO Auto-generated method stub 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag); 
      v.startDrag(data, shadow, null, 0); 
      return false; 
     } 
    }); 

} 

} 
+0

ok. Danke, aber das ist nicht was ich will. Grundsätzlich möchte ich, dass ich zuerst zwei imageViews (imageView1 und imageView2) habe und beide leer sind. Angenommen, ich berühre ImageView1 und dann ein Bild auf imageView1. Und dann möchte ich dieses Bild ziehen und auf imageView2 fallen lassen. und umgekehrt. Bedeutet, wir haben zwei ImageViews und ein Bild. –