3

我正在创建一个拖放应用程序,它在主布局上拖动一个对象。我的问题是我想拥有图像视图的无限/无限副本,这样我就可以尽可能多地拖动图像。

例如心形,当我已经拖动图像时,我不能有另一个心形,因为布局中只有一个图像(心形)。

在此处输入图像描述

这是我在 imageViews(星、心、闪电)上的触摸代码:

private final class MyTouchListener implements OnTouchListener {

    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
          ClipData data = ClipData.newPlainText("", "");
          DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
          view.startDrag(data, shadowBuilder, view, 0);
          view.setVisibility(View.INVISIBLE);
          return true;
        } else {
        return false;
        }
      }

    }

这是我的拖放区/主图像的拖动监听器:

class MyDragListener implements OnDragListener {
    @Override
      public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        final int X = (int) event.getX();
        final int Y = (int) event.getY();

        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
        // do nothing
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
          break;
        case DragEvent.ACTION_DRAG_EXITED:        
          break;
        case DragEvent.ACTION_DROP:
          // Dropped, reassign View to ViewGroup
          View view = (View) event.getLocalState();
          ViewGroup owner = (ViewGroup) view.getParent();
          owner.removeView(view);
          RelativeLayout container = (RelativeLayout) v;

          RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(30, 30);
           params1.leftMargin = (int) event.getX() - 15;
           params1.topMargin = (int) event.getY() -15;

          container.addView(view,params1);
          view.setVisibility(View.VISIBLE);
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          default:
          break;
        }
        return true;
      }
  }
4

1 回答 1

2

在您的情况下,使用画布进行拖放要容易得多。

创建一个扩展 View 的类,制作一个 rect 来在里面绘制图像,在该视图上设置 Ontouch,让 rect 跟随您的触摸,并使用 invalidate() 强制重绘视图。

编辑 使用画布时,您不会因边距和布局参数而头疼,它很简单。

DragDropView.java

public class DragDropView extends View implements OnTouchListener  {
    Rect originalRect , draggableRect;
    int left, right, top, bottom, newX, newY, size;
    Drawable originalDrawable,draggableDrawable;

    public DragDropView(Context context)
    {
        super(context, null);
        size = 20;
        originalRect= new Rect();
        originalRect.set(200, 200, 240, 240); //left , top , right , bottom
        draggableRect= new Rect();
        draggableRect.set(200, 200, 240, 240);
        originalDrawable = getResources().getDrawable(R.drawable.ic_launcher);
        draggableDrawable = getResources().getDrawable(R.drawable.ic_launcher);
        originalDrawable.setBounds(originalRect); // since it wont move no need to do that in onDraw()
        newX = 220;
        newY=220;
        this.setOnTouchListener(this);
    }
    public DragDropView(Context context, AttributeSet attrs)
    {    super(context, attrs);    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        originalDrawable.draw(canvas);
        draggableRect.set(newX-size, newY-size, newX+size, newY+size);
        draggableDrawable.setBounds(draggableRect);
        draggableDrawable.draw(canvas);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        newX = (int)event.getX();
        newY = (int)event.getY();
        invalidate(); // force redraw

        //if you need special touch events
//      //on touch down
//      if( event.getAction() == MotionEvent.ACTION_DOWN )
//      {   }
//      //on touch move
//      if( event.getAction() == MotionEvent.ACTION_MOVE )
//      {   }
//      //on touch up
//      if( event.getAction() == MotionEvent.ACTION_UP )
//      {   }

        return true; // always return true to let touch listener listen to next touch
    }
}

你的活动.java

DragDropView dragDropView = new DragDropView();
于 2013-10-29T14:36:11.577 回答