如何在Android中拖动时获得精确的坐标?

时间:2013-03-14 11:05:28

标签: android android-layout android-widget

晚上好

我正在尝试使用onTouchListener在拖动视图上的拖动视图和onDragListener上实现拖放,如下所示:

这是MyDragListener ..

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

  @Override
  public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    switch (event.getAction()) {

    /*Some irrelevant cases*/

    case DragEvent.ACTION_DROP:
      // Dropped, reassign View to ViewGroup
        View view = (View) event.getLocalState();
        ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        FrameLayout container;
        container = (FrameLayout) v;
    container.addView(view);
    view.setX(event.getX());
    view.setY(event.getY()); 
        view.setVisibility(View.VISIBLE);
      break;
      default:
      break;
    }
    return true;
  }
}

我正在将此侦听器分配给拖拽区,在我的示例中是一个frameLayout 像这样:

 FrameLayout f= (FrameLayout) findViewById(id.FrameLayout);

    f.setOnDragListener(new MyDragListener());

这是布局:

<FrameLayout 
android:id="@+id/FrameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8">

<ImageView
    android:contentDescription="This is the main panel of the program"
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blank" 
    android:isScrollContainer="true"
    />
<Button 
    android:id="@+id/DraggableButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Drag me -_-"
    />

现在的问题是我只能得到原始的X,Y坐标,并且没有产生精确的结果....

我尝试使用onTouchListener处理这个问题(并在DragListener上给出ip),它给了我容器上的坐标,并且在拖动时产生了精确的结果但是当我放下视图时它就消失了。 这是MyTouchListener代码:

private final class MyTouchListener implements OnTouchListener {
    @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
          ClipData data = ClipData.newPlainText("", "");
          DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
          view.startDrag(data, shadowBuilder, view, 0);
          view.setVisibility(View.INVISIBLE);
          return true;
        } 
        else 
            if(motionEvent.getAction()==MotionEvent.ACTION_UP)
            {
                    // View view = (View) motionEvent.getLocalState();

                    view.setX(motionEvent.getX());
                    view.setY(motionEvent.getY());


                  view.setVisibility(View.VISIBLE);
               return true;
                }
        return false;


      }
    }

有人能告诉我我想做什么吗?

1 个答案:

答案 0 :(得分:0)

你必须做这样的事情:

case DragEvent.ACTION_DROP:
  // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    owner.removeView(view);
    FrameLayout container;
    container = (FrameLayout) v;
    float y = event.getY() - view.getHeight()/2;

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(view.getWidth(), view.getHeight());
    layoutParams.setMargins(0, (int)y, 0, 0);
    view.setLayoutParams(layoutParams);

    container.addView(view);
    view.setVisibility(View.VISIBLE);
  break;