在边界拖动图像

时间:2013-03-13 09:09:04

标签: android touch drag

enter image description here

我希望只在父视图中拖动该图像。当用户试图将其拖到视图外时,它将返回其原始位置。基本上我正在实现android解锁类型功能。请帮帮我。它是我的主要班级。

public class MainActivity extends Activity {

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
  }

  private final class MyTouchListener implements OnTouchListener {
    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 {
        return false;
      }
    }
  }

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

    @Override
    public boolean onDrag(View v, DragEvent event) {
      //int action = event.getAction();
      switch (event.getAction()) {
      case DragEvent.ACTION_DRAG_STARTED:
        // Do nothing
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
        v.setBackgroundResource(R.drawable.shape_droptarget);
        break;
      case DragEvent.ACTION_DRAG_EXITED:
        v.setBackgroundResource(R.drawable.shape);
        setFinishOnTouchOutside(true);
        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);
        view.setVisibility(View.VISIBLE);
        break;
      case DragEvent.ACTION_DRAG_ENDED:         
        v.setBackgroundResource(R.drawable.shape);
      default:
        break;
      }
      return true;
    }
  }
} 

main xml

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

<RelativeLayout
    android:id="@+id/topleft"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_centerInParent="true"       
    android:background="@drawable/shape" >

    <ImageView
        android:id="@+id/myimage1"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以在OnTouch()方法中使用它来实现移动控制。

//Some global variables
    float oldX=0,oldY=0;
    public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        oldY=motionEvent.getY();
        oldX=motionEvent.getX();
        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_MOVE) {
        if((motionEvent.getY()>oldY+10 || motionEvent.getY()<oldY-10))//The value 10 is controlling sensitivtiy of y axis greater than 10 or less than 10 the below code will be fired.
          {
             view.setEnabled(false);
                  RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)            view.getLayoutParams();
            layoutParams.leftMargin = oldX;
            layoutParams.topMargin = oldY;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            view.setLayoutParams(layoutParams);
            view.setEnabled(true);
          }
      }
      }