Android:拖放按钮(按钮消失;按钮位置)

时间:2014-02-11 15:16:17

标签: java android drag-and-drop ontouchevent

我正在开发一个我要拖动按钮的应用。我写了两个程序。问题是他们每个人只满足我想要的特定部分。我想要一个可以在屏幕上自由移动的按钮(没有消失和东西),我还需要程序在移动时永久地给我本地按钮位置的坐标。

在第一个程序中,拖动时按钮消失的问题。这种情况发生在x和y的负值以及正x值(不是正y值;这里是一切都很完美)。正y值是按钮上方的所有值,正x值表示按钮右侧的所有内容,依此类推。对于负值,按钮消失,就像有覆盖按钮的墙。对于正x值,按钮会随着x值的上升而消失(有点像水中的嘶嘶声)。

以下是代码:

seat_leftright = (ImageButton) findViewById(R.id.seat_leftright);
    seat_leftright.setOnTouchListener(new OnTouchListener() {

   int k = 0;
   int prevX,prevY;
   int x=0;
   int y=0;

    @Override
    public boolean onTouch(final View v,final MotionEvent event)
      { 
      final LinearLayout.LayoutParams par=(LinearLayout.LayoutParams)v.getLayoutParams();        

      switch(event.getAction())
        {
        case MotionEvent.ACTION_MOVE:
          {

              if(k == 0){ //otherwise there would be an offset when touching the button the first time
                            prevY=(int)event.getRawY(); 
                prevX=(int)event.getRawX();
              }
              y+=prevY -(int)event.getRawY();
              prevY=(int)event.getRawY();                 
              par.bottomMargin = y;


              x+=(int)event.getRawX()-prevX;
              prevX=(int)event.getRawX();
                  par.leftMargin = x;


              Log.i("LOG","x: "+ x +", y: " + y );

              k++;

              v.setLayoutParams(par);

              return true; 
          }
        case MotionEvent.ACTION_UP:
          {
          par.bottomMargin=0;
          par.leftMargin=0;
          k=0;                  
          y=0;
          x=0;
          v.setLayoutParams(par);
          return true;
          }
        }
      return false;
      }
  });

}

在第二个程序中,我使用了DragShadowBuilder函数。该按钮在此程序中完美运行,因此它不会消失等。在这里我遇到接收值的问题。移动时我经常需要按钮的x和y位置。我用Action_drag_location尝试了它,但它只在我将按钮拖到另一个按钮上方时返回坐标(这里是按钮“arrow_down”)。用我的背景替换“arrow_down”按钮以不断接收坐标根本不起作用。我还尝试将我的第一个程序与第二个程序结合起来,结果是我根本没有收到任何值。

我希望你能帮助我。我很感激各种帮助!

在第二个程序的代码下面。

OnTouchListener myOnTouchListener = new OnTouchListener() {


        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view);
                view.startDrag(null, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            }



            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                view.setVisibility(View.VISIBLE);}
                return true;

        }
    };

    OnDragListener myDragListener = new OnDragListener() {

        @Override
        public boolean onDrag(View layoutview, DragEvent dragevent) {

            int action = dragevent.getAction();
            View view = (View) dragevent.getLocalState();

            switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.i("LOG","DragStarted");
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_LOCATION:
                float x = (int)layoutview.getX();
                float y = (int)layoutview.getY();
                Log.i("COORDS","X: " + x +" Y: " + y);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:                 
                break;

            case DragEvent.ACTION_DRAG_ENDED:
                Log.d("LOG", "Drag ended");
                if (dropEventNotHandled(dragevent)) {
                    view.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
            }
            return true;
        }

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

    findViewById(R.id.arrow_up).setOnTouchListener(myOnTouchListener);    
    findViewById(R.id.arrow_down).setOnDragListener(myDragListener);  

2 个答案:

答案 0 :(得分:2)

@Jay:谢谢你的帮助,但这并没有解决问题。消失的按钮是由于我的布局,我有这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/seat" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="160dp"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/seat_updown"
        android:layout_width="120dp"
        android:layout_height="140dp"
        android:scaleType="fitCenter"
        android:src="@drawable/doublearrow"
        android:rotation="90" /> />


</LinearLayout>

因为ImageButton嵌入了LinearLayout,它就消失了!

答案 1 :(得分:1)

按钮拖动&amp;我在这里分享我的代码检查它。

此处按钮触摸侦听器。

MultiTouchListener touchListener = new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);

触摸侦听器类。

public class MultiTouchListener implements OnTouchListener {

    private float mPrevX;
    private float mPrevY;

    public MainActivity mainActivity;

    public MultiTouchListener(MainActivity mainActivity1) {
        mainActivity = mainActivity1;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        float currX, currY;
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN: {

            mPrevX = event.getX();
            mPrevY = event.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE: {

            currX = event.getRawX();
            currY = event.getRawY();

            MarginLayoutParams marginParams = new MarginLayoutParams(
                    view.getLayoutParams());
            marginParams.setMargins((int) (currX - mPrevX),
                    (int) (currY - mPrevY), 0, 0);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    marginParams);
            view.setLayoutParams(layoutParams);

            break;
        }

        case MotionEvent.ACTION_CANCEL:
            break;

        case MotionEvent.ACTION_UP:

            break;
        }

        return true;
    }

}

当然,你可以得到这些,确保在尝试获取位置之前至少抽取一次视图。您可以尝试获取onResume()中的位置并尝试这些功能

view.getLocationInWindow()view.getLocationOnScreen()

或者如果您需要与父母相关的内容,请使用

view.getLeft()view.getTop()

请点击此链接获取坐标。

Retrieve the X & Y coordinates of a button in android?