安卓在拖放,IMG可以拖放,但不能拖放

  • 本文关键字:拖放 但不能 IMG android
  • 更新时间 :
  • 英文 :


我正在做很多事情,但我无法得到。我面临的问题是,当我拖动图像时,它的工作正常,当我放下它时,它无法替换或看不到,下面我把我的代码请帮助我错的地方。

公共类 主活动扩展活动 {

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

}

所以以上是我的代码,请检查它并建议我谢谢。

首先,

你的语法有点混乱,请原谅我这么说。并且使用图像而不是 img 更具可读性。用直接的方式描述你的问题,不要讲故事。"它不能替换(什么)或看不到(什么)"。实际上,您的初始视图状态是什么,或者在第一次运行应用程序后显示哪些视图?在将"它"放到另一个"视图"后,您期望什么行为或您希望看到什么?

相关内容

最新更新