我做了很多东西,但我无法得到。我面临的问题是当我拖动图像时它的工作正常,当我放下它时它无法取代或看不到, 下面我已经把我的代码请帮助我错了。
公共类MainActivity扩展了Activity {
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;
}
}
}
}
所以以上是我的代码请检查并建议我谢谢。
答案 0 :(得分:0)
首先你的语法有点令人困惑,请原谅我说。并且使用图像而不是img更具可读性。直接描述你的问题而不讲故事。 “它不能取代(什么)或不能看到(什么)”。 实际上,在第一次运行应用程序后,您的初始状态是什么或者显示了哪些视图? 将“it”放到另一个“视图”后,您期望的行为或您想要查看的内容是什么?