2014-02-25 54 views
0

我正在嘗試onDrag事件來移動ImageView。我的ImageView移動得非常好,但我的問題是ImageViewDROP事件的最後位置沒有顯示。 以下是我的代碼...我在哪裏犯錯?Imageview在丟棄事件中沒有丟失

package com.example.dragndropdemo; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.ClipData; 
import android.content.ClipDescription; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.util.Log; 
import android.view.DragEvent; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.DragShadowBuilder; 
import android.view.View.OnDragListener; 
import android.widget.*; 

public class MainActivity extends Activity{ 
    ImageView ima; 
    private static final String IMAGEVIEW_TAG = "Android Logo"; 
    String msg; 

    private android.widget.RelativeLayout.LayoutParams layoutParams; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ima = (ImageView)findViewById(R.id.iv_logo); 
     // Sets the tag 
     ima.setTag(IMAGEVIEW_TAG); 

     ima.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         ClipData.Item item = new ClipData.Item((CharSequence)v.getTag()); 

        String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; 
        ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item); 
        // Instantiates the drag shadow builder. 
        DragShadow dragShadow=new DragShadow(v); 

        // Starts the drag 
        v.startDrag(dragData // the data to be dragged 
          , dragShadow // the drag shadow builder 
          , v    // no need to use local data 
          , 0    // flags (not currently used, set to 0) 
          ); 
        v.setVisibility(View.INVISIBLE); 
        return true; 
      } 
      else{ 
       return false; 
      } 
     } 
     }); 

     // Create and set the drag event listener for the View 
     ima.setOnDragListener(new OnDragListener(){ 
     @Override 
     public boolean onDrag(View v, DragEvent event){ 
      int dragEvent=event.getAction(); 
     switch(event.getAction())     
     { 
      case DragEvent.ACTION_DRAG_STARTED: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED"); 
       // Do nothing 
       break; 

      case DragEvent.ACTION_DRAG_ENTERED: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED"); 
       break; 

      case DragEvent.ACTION_DRAG_EXITED : 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED"); 
       break; 

      case DragEvent.ACTION_DRAG_LOCATION : 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION"); 
       break; 

      case DragEvent.ACTION_DRAG_ENDED : 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED"); 
       // Do nothing 
       break; 

      case DragEvent.ACTION_DROP: 
       Log.d(msg, "ACTION_DROP event"); 
       ImageView imgvw=(ImageView)event.getLocalState(); 
       imgvw.setVisibility(View.VISIBLE); 
       break; 

      default: break; 
      } 
      return true; 
     } 
     });   
    } 

    private class DragShadow extends View.DragShadowBuilder 
    { 

    public DragShadow(View view) { 
     super(view); 
    } 

    @Override 
    public void onDrawShadow(Canvas canvas) { 
     super.onDrawShadow(canvas); 
    } 

    @Override 
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { 
     View v= getView(); 

     int height=v.getHeight(); 
     int width=v.getWidth(); 

     shadowSize.set(width, height); 
     shadowTouchPoint.set((int)width/2,(int)height/2); 
    } 

    } 
} 

回答

1

我一直在爲同樣的問題奮鬥了幾個小時。但是我在ApiDemos裏找到了一個解決方案。你可以看看內部的包com.example.android.apis.view,你會看到DragAndDropDemo.java

在裏面,你會發現可拖動的對象是DraggableDot的實例,它們也是放置點。然後看內DraggableDot,它只是View的子類,實現onDragEvent

最後,我創建了一個View的子類,並插入了onDragEvent方法。我使用這個子類作爲我的下降點,並且,令人驚訝的是,我的DragListener開始註冊其他DragEvent操作,尤其是ACTION_DROP

-1

變化:

ImageView imgvw=(ImageView)event.getLocalState(); 

imgvw.setVisibility(View.VISIBLE); 

爲:

v.setVisibility(View.VISIBLE);