2013-03-13 111 views
3

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; 
    } 
    } 
} 

主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> 
+0

问题是....? – 2013-03-13 10:34:37

+0

我想只水平拖动它。当用户将其向上或向下拖动时,拖动并触摸事件应取消。图像应返回其原始位置。 – riskPlayGround 2013-03-13 10:38:24

+0

你做到了吗?如果是,请分享答案 – 2013-10-23 09:31:08

回答

0

可以通过在OnTouch使用此()方法acheive移动控制。

//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); 
      } 
     } 
     }