2013-01-04 20 views
11

Benzer soruların yayınlandığının farkına vardım ve bir çözüm bulmak için onları ve diğer pek çok konuyu inceledim - açık bir şekilde eksik olanı görüyorum - hala öğreniyorum temelleri!Android beginner - Place Image at DragEvent

Hedef: Basit sürükle ve bırak. Kullanıcı, görüntüyü ekran boyunca hareket ettirir ve başka bir görüntünün üstüne veya ekranın herhangi bir yerine düşer.

API: Tamamlandı> 11

:

  • başka görüntünün üst kısmında görüntü ve yeri sürükleyip (onaylamak için Tost kullanarak) yanıtını alabilir miyim.
  • Görüntüyü ekranda herhangi bir yere sürükleyebilir ve yanıt alabilirsiniz (onaylamak için Tost kullanarak).

çalışma DEĞİL: parmak Ben farklı yöntemlerin çok ama her zaman derleme hataları çalıştılar

kaldırdı nerede

  • ekranın herhangi bir yerine ve mevduat resmin üzerine görüntüyü sürükleyebilirsiniz olamaz.

    public class MainActivity extends Activity { 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 
    
    boolean okToDrop; 
    
    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        findViewById(R.id.oval).setOnTouchListener(new theTouchListener()); 
        findViewById(R.id.square).setOnTouchListener(new theTouchListener()); 
        findViewById(R.id.robot).setOnTouchListener(new theTouchListener()); 
        findViewById(R.id.oval).setOnDragListener(new theDragListener()); 
        findViewById(R.id.square).setOnDragListener(new theDragListener()); 
        findViewById(R.id.robot).setOnDragListener(new theDragListener()); 
    } 
    
    private final class theTouchListener 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.VISIBLE); 
          return true; 
         } else { 
          return false; 
         } 
        } 
    } 
    
    class theDragListener implements OnDragListener { 
        @Override 
        public boolean onDrag(View view, DragEvent dragEvent) { 
         int dragAction = dragEvent.getAction(); 
         View dragView = (View) dragEvent.getLocalState(); 
         if (dragAction == DragEvent.ACTION_DRAG_EXITED) { 
          okToDrop = false; 
         } else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) { 
          okToDrop = true; 
         } else if (dragAction == DragEvent.ACTION_DRAG_ENDED) { 
          if (dropEventNotHandled(dragEvent) == true) { 
    
           // Code to generate image goes here *********************** 
    
           Context context = getApplicationContext(); 
           CharSequence text = "Action Dropped Not In Box!"; 
           int duration = Toast.LENGTH_SHORT; 
           Toast toast = Toast.makeText(context, text, duration); 
           toast.show(); 
    
           dragView.setVisibility(View.VISIBLE); 
          } 
         } else if (dragAction == DragEvent.ACTION_DROP && okToDrop) { 
          ImageView i = (ImageView) findViewById(R.id.square); 
          i.setImageResource(R.drawable.oval); 
    
          dragView.setVisibility(View.INVISIBLE); 
    
          Context context = getApplicationContext(); 
          CharSequence text = "Action Resulted In It Being Dropped In The Box"; 
          int duration = Toast.LENGTH_SHORT; 
          Toast toast = Toast.makeText(context, text, duration); 
          toast.show(); 
    
         } 
         return true; 
        } 
    
        private boolean dropEventNotHandled(DragEvent dragEvent) { 
         return !dragEvent.getResult(); 
        } 
    
    } 
    

    Ve xml: kodumu baktığımızda, herkes ACTION_DRAG_ENDED bir görüntü yerleştirmek için temiz ve basit bir yöntem tavsiye verebilecek İşte

    benim java kodu (akılda tutmak ben bir acemi)

    <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" 
        tools:context=".MainActivity" > 
    
        <ImageView 
         android:id="@+id/square" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_centerVertical="true" 
         android:src="@drawable/square_box" /> 
    
        <ImageView 
         android:id="@+id/oval" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentTop="true" 
         android:layout_marginLeft="58dp" 
         android:layout_marginTop="58dp" 
         android:src="@drawable/oval" /> 
    
        <ImageView 
         android:id="@+id/robot" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@+id/oval" 
         android:layout_below="@+id/square" 
         android:layout_marginTop="70dp" 
         android:src="@drawable/ic_launcher" /> 
    
    </RelativeLayout> 
    
+0

Ne elde hataları derleme edilir olmalıdır? – codeMagic

+0

Hi @codeMagic Çalıştığım kodun en yakın bit'i bir Linear düzen kurulumu kullanmaktı (sadece bir görüntü yerleştirmek için biraz ağır görünüyor) Bir hata aldım 'Kurucu ImageView (MainActivity.theDragListener) tanımlanmamış' Bu gerekir 'theDragListener' dışında ilan edilmek mi? - neden bu undefined değil emin olun .. – user1947262

+0

Bana öyle görünüyor ki ben de bir şey görmemeliyim. Hangi satırı hatayı alıyorsunuz? – codeMagic

cevap

1

Uygulamamın en iyi yolu olup olmadığından emin olmamama rağmen işe yarıyor.

sizin MainActivity bu üyeler ekle:

View root; // references root activity view 
float dropX, dropY; // records position of where finger was lifted on the root 

sizin onCreate() in:

Nihayet
root = findViewById(android.R.id.content); // get reference to root 
// add a Draglistener to your root view 
root.setOnDragListener(new OnDragListener() { 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     if(action == DragEvent.ACTION_DROP) { 
      // record position of finger lift 
      dropX = event.getX(); 
      dropY = event.getY(); 
     } 
     return false; 
    } 
}); 

, // Code to generate image goes here altında şu satırları yerleştirin: Bu yardımcı olur

dragView.setX(dropX - dragView.getWidth()/2); 
dragView.setY(dropY - dragView.getHeight()/2); 

Umut.

0

Tamam! Yani sorun aynı sürükleyici dinleyiciyi kullanmıyor.

findViewById(R.id.oval).setOnDragListener(new theDragListener()); 

theDragListener draglistener = new theDragListener(); 
findViewById(R.id.oval).setOnDragListener(draglistener);