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

3
我正在开发一个应用程序,其中我想要拖动一个按钮。我已经编写了两个程序。问题在于它们中的每一个只能满足我想要的特定部分。我想要一个可以自由移动到屏幕上的按钮(没有消失等问题),并且在移动时,程序还需要给我当前本地按钮位置的坐标(永久性)。
第一个程序的问题在于,当拖动时按钮会消失。这种情况发生在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个回答

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

对于按钮拖放,我在这里分享我的代码,请查看。

这是按钮触摸监听器。

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()

查看此链接以获取坐标。

检索Android按钮的X和Y坐标?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接