2013-03-13 117 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); 
      } 
     } 
     }