1

我正在创建一个 android 应用程序,它将在主布局上放置图像。我的问题是如何将图像拖放到另一个布局上。

我的一些代码来自:http ://www.vogella.com/articles/AndroidDragAndDrop/article.html

只要您将其拖动到相同的布局上,它就不会在其他布局上移动,我的代码就可以正常工作。

这是我的代码:

public class MainActivity extends Activity {    
int _xDelta;
int _yDelta;
  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

}

 private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {


       final int X = (int) motionEvent.getRawX();
        final int Y = (int) motionEvent.getRawY();
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
        }
        return true;
}
 }

  class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
  int action = event.getAction();
  switch (event.getAction()) {
  case DragEvent.ACTION_DRAG_STARTED:
     break;
  case DragEvent.ACTION_DRAG_ENTERED:
      break;
  case DragEvent.ACTION_DRAG_EXITED:
    break;
  case DragEvent.ACTION_DROP:
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    owner.removeView(view);
    LinearLayout container = (LinearLayout) v;
    container.addView(view);
    view.setVisibility(View.VISIBLE);
    break;
  case DragEvent.ACTION_DRAG_ENDED:
  default:
    break;
  }
  return true;
}

} }

希望您能够帮助我。

4

1 回答 1

0

我使触摸视图不可见,以编程方式在旧视图的位置创建了外观相同的视图,并将这个新视图设置为移动(根是新创建视图的根布局)。

     ImageView tmpView;
     private float dX;
     private float dY;
     public boolean onTouch(View view, MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:

                    view.setVisibility(View.GONE);
                    Log.d(TAG, "event.getRawX() " + event.getRawX() + ", event.getRawY() " + event.getRawY());
                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();

                    tmpView = new ImageView(SongActivity.this);
                    tmpView.setImageDrawable(((ImageView) view).getDrawable());

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
                    params.leftMargin = (int) event.getRawX();
                    params.topMargin = (int) event.getRawY();
                    root.addView(tmpView, params);

                    RelativeLayout.LayoutParams ivLayoutParams = (RelativeLayout.LayoutParams) tmpView.getLayoutParams();
                    ivLayoutParams.leftMargin = (int) (event.getRawX());
                    ivLayoutParams.topMargin = (int) (event.getRawY() - 250);
                    ivLayoutParams.rightMargin = -250;
                    ivLayoutParams.bottomMargin = -250;
                    tmpView.setLayoutParams(ivLayoutParams);

                    break;
                case MotionEvent.ACTION_MOVE:
                    tmpView.setY(event.getRawY() - 250);
                    tmpView.setX(event.getRawX() + dX);
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    return false;
            }
            return true;
        }
于 2016-04-13T15:10:08.930 回答