如何在android中以侧面(水平)方式拖动View?

时间:2013-12-05 05:20:41

标签: android

我希望只能以水平方式拖动视图。我使用了draglistener来拖动视图。

如何才能以横向方式拖动视图?

我只是想只以水平方式拖动视图。有没有办法做到这一点。

我的触控听众课程:

private final class MyTouchListener implements OnTouchListener {
    Drawable normalShape = getResources().getDrawable(R.drawable.circle);
//  Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.pinkcircle);
    @SuppressWarnings("deprecation")
    public boolean onTouch(View view, MotionEvent motionEvent) {
//      view.setVisibility(View.INVISIBLE);
//      view.setBackgroundDrawable(normalShape); 
        view.setBackgroundResource(R.drawable.circle);
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//          full_left.setVisibility(View.VISIBLE);
            centre_leftanimate.clearAnimation();
            centre_leftanimate.setVisibility(View.GONE);
//          RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(b.getWidth(),b.getHeight());
//          RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//          layoutParams.gravity = Gravity.CENTER;
//          view.setLayoutParams(layoutParams);
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
//          view.setVisibility(View.INVISIBLE);
            half_left.setVisibility(View.GONE);
            return true;
        } else {
            return false;
        }
    }
}

我的DragListener类:

class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.halfcircle_left);

    //  Drawable normalShape = getResources().getDrawable(R.drawable.shape);
    public boolean onDrag(View v, DragEvent event) {
        int dragAction = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // dragView.setBackgroundDrawable(normalShape);
                Log.e("in started", "in start");
                // dragView.setBackgroundResource(R.drawable.pinkcircle);
                // Do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                Log.e("in entered", "in enter");
                // dragView.setBackgroundDrawable(normalShape);
                // dragView.setBackgroundResource(R.drawable.pinkcircle);
                // v.setBackgroundDrawable(enterShape);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                Log.e("in exited", "in exit");
                // v.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DROP:
                int x = Integer.valueOf(s1.intValue());
                int y = Integer.valueOf(s2.intValue());
                Log.e("xs" + x, "xs" + y);
                Log.e("x1" + Cartoon_image.getLeft(), "y1" + Cartoon_image.getTop());
                Log.e("x4" + Cartoon_image.getRight(), "y4" + Cartoon_image.getBottom());
                if ((Cartoon_image.getLeft() < x) && (x < Cartoon_image.getRight()) && ((Cartoon_image.getTop() < y) && (y < Cartoon_image.getBottom()))) {
                    break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        Log.e("xs" + (int) event.getX(), "xs" + (int) event.getX());
                        // full_left.setVisibility(View.VISIBLE);
                        if (dropEventNotHandled(event)) {
                            half_left.setVisibility(View.VISIBLE);
                            // dragView.setVisibility(View.VISIBLE);
                        }
                    default:
                        break;
                }
                return true;
        }

    private boolean dropEventNotHandled(DragEvent dragEvent) {
        return !dragEvent.getResult();
    }
}

1 个答案:

答案 0 :(得分:-1)

您应该将draglistener添加到视图的容器中。拖动开始时,将当前y保存到类字段。然后,您只需在用户拖动视图时设置该值。

   ViewGroup cotainer = (ViewGroup) view.getParent();
   container.setOnDragListener(new MyDragListener());

那里,你执行几个步骤

class MyDragListener implements OnDragListener {

  @Override
  public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
       View view = (View) event.getLocalState();
      CURRENT_Y=view.getY();
      break;

    case DragEvent.ACTION_DROP:
      // Dropped, reassign View to ViewGroup
      View view = (View) event.getLocalState();
      ViewGroup owner = (ViewGroup) view.getParent();
      owner.removeView(view);
      LinearLayout container = (LinearLayout) v;
      container.addView(view);
       v.setY(CURRENT_Y);
      view.setVisibility(View.VISIBLE);
      break;

    }
    return true;
  }
} 

当然,您可以改进此代码,现在只有在删除视图时才会重置y。但是从这里你可以开始,现在看看其余的事件动作,这样你就可以修复每个位置的变化