0
我正在做很多東西,但我不能get.The問題,我面臨的是,當我拖動圖像它的工作好,當我下降它不能代替或看不到, 下面我已經把我的代碼,請幫助我錯了。android in drag'n'drop,img可以拖動,但不會掉落
公共類MainActivity擴展活動{
TextView comments;
ImageView img, buttonTarget;
String commentMsg;
MyDragEventListener myDragEventListener = new MyDragEventListener();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.ivImg);
buttonTarget = (ImageView) findViewById(R.id.buttonTarget);
// Create and set the tags for the Buttons
final String IMG_TAG = "img";
img.setTag(IMG_TAG);
img.setOnLongClickListener(sourceimgLongClickListener);
img.setOnDragListener(myDragEventListener);
buttonTarget.setOnDragListener(myDragEventListener);
}
ImageView.OnLongClickListener sourceimgLongClickListener = new ImageView.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] clipDescription = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData dragData = new ClipData((CharSequence) v.getTag(),
clipDescription, item);
DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
v.startDrag(dragData, myShadow, v, 0);
return true;
}
};
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
// private static Drawable shadow;
public MyDragShadowBuilder(View v) {
super(v);
// shadow = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onProvideShadowMetrics(Point size, Point touch) {
int width = getView().getWidth();
int height = getView().getHeight();
// Log.i("metrix",""+width+" "+height);
// shadow.setBounds(0, 0, width, height);
size.set(width, height);
touch.set(width/2, height/2);
// Log.i("metrix",""+width/5+" "+height/5);
}
/*
* @Override public void onDrawShadow(Canvas canvas) {
* shadow.draw(canvas); }
*/
}
protected class MyDragEventListener implements View.OnDragListener {
@SuppressWarnings("deprecation")
@Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i("action", "start");
// All involved view accept ACTION_DRAG_STARTED for
// MIMETYPE_TEXT_PLAIN
if (event.getClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true; // Accept
} else {
return false; // reject
}
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("action", "entered");
return true;
case DragEvent.ACTION_DRAG_LOCATION:
Log.i("action", "location");
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("action", "exited");
return true;
case DragEvent.ACTION_DROP:
Log.i("action", "drop");
// Gets the item containing the dragged data
// ClipData.Item item = event.getClipData().getItemAt(0);
// If apply only if drop on buttonTarget
if (v == buttonTarget) {
// Retrieve the source view using getLocalState()
View dragView = (View) event.getLocalState();
((ImageView) v)
.setBackgroundDrawable(((ImageView) dragView)
.getBackground());
}
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("action", "ended");
if (event.getResult()) {
} else {
}
return true;
default: // unknown case
return false;
}
}
}
}
所以上面是我的代碼,請檢查一下,並建議我的感謝。