2013-01-04 8 views
11

पर प्लेस छवि मुझे एहसास है कि इसी तरह के प्रश्न पोस्ट किए गए हैं, और मैंने उन्हें समाधान खोजने के लिए उन्हें और कई अन्य विषयों आदि को देखा है - मैं स्पष्ट रूप से स्पष्ट रूप से याद कर रहा हूं - क्योंकि मैं अभी भी सीख रहा हूं मूल बातें!एंड्रॉइड शुरुआती - ड्रैगएवेंट

लक्ष्य: सरल खींचें और छोड़ें। उपयोगकर्ता स्क्रीन पर छवि को स्थानांतरित करता है और या तो स्क्रीन पर किसी अन्य छवि या कहीं भी ऊपर जाता है।

एपीआई:> 11

पूरा हुआ:

  • एक और छवि के शीर्ष पर छवि और जगह खींचें और (पुष्टि करने के लिए टोस्ट का प्रयोग करके) प्रतिक्रिया प्राप्त कर सकते हैं।
  • छवि पर कहीं भी छवि खींचें और प्रतिक्रिया प्राप्त करें (पुष्टि करने के लिए टोस्ट का उपयोग करके)।

नहीं काम कर रहा है: जहां उंगली

मैं विभिन्न तरीकों की बहुत सारी हैं, लेकिन हमेशा संकलन त्रुटियों की कोशिश की है उठाया

  • स्क्रीन और जमा चित्र पर कहीं भी छवि को खींचें नहीं कर सकते। मेरे कोड को देखते हुए, किसी को भी एक साफ और सरल विधि की सिफारिश कर सकता है ACTION_DRAG_ENDED पर एक छवि जगह (ध्यान में रखना मैं कर रहा हूँ अभी शुरुआत)

    यहाँ मेरी जावा कोड है:

    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(); 
        } 
    
    } 
    

    और मेरे 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" 
        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

क्या त्रुटियों कि आप संकलन कर रहे हैं? – codeMagic

+0

हाय @codeMagic मैं काम कर रहा कोड का सबसे नज़दीकी बिट एक रैखिक लेआउट सेटअप का उपयोग कर रहा था (हालांकि एक छवि रखने के लिए थोड़ा भारी लगता है) मुझे एक त्रुटि मिली 'निर्माता छवि दृश्य (मुख्य गतिविधि .heDragListener) अपरिभाषित है' क्या इसकी आवश्यकता होगी 'द ड्रैगलिस्टर' के बाहर घोषित किया जाए? - सुनिश्चित नहीं है कि यह अपरिभाषित क्यों है .. – user1947262

+0

यह मेरे लिए ठीक लग रहा है इसलिए मुझे कुछ भी नहीं देखना चाहिए। आपको किस लाइन के लिए त्रुटि मिल रही है? – codeMagic

उत्तर

1

हालांकि मुझे यकीन नहीं है कि मेरा कार्यान्वयन जाने का सबसे अच्छा तरीका है, यह काम करता है। अपने OnCreate() में

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

:

अपने MainActivity में इन सदस्यों को शामिल करें

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 के तहत इन पंक्तियों जगह:

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

आशा इस मदद करता है।

0

ठीक है! तो आपकी समस्या एक ही ड्रैग श्रोता का उपयोग नहीं कर रही है।

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

होना चाहिए

theDragListener draglistener = new theDragListener(); 
findViewById(R.id.oval).setOnDragListener(draglistener); 
संबंधित मुद्दे