我正在嘗試onDrag
事件來移動ImageView
。我的ImageView
移動得非常好,但我的問題是ImageView
在DROP
事件的最後位置沒有顯示。 以下是我的代碼...我在哪裏犯錯?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);
}
}
}