2013-03-13 1 views
3

enter image description here경계에서 이미지 드래그

해당 이미지를 상위 뷰에서만 끌기를 원합니다. 사용자가 뷰 밖으로 드래그하려고하면 원래 위치로 되돌아갑니다. 기본적으로 안드로이드 구현 유형의 기능을 구현 해요. 제발 도와주세요. 그것의 주요 클래스.

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

문제는 ....? –

+0

나는 그것을 수평으로 만 끌고 싶다. 사용자가 드래그하여 드래그 앤 터치하면 이벤트가 취소됩니다. 이미지는 원래 위치로 되돌아갑니다. – riskPlayGround

+0

이 작업을 수행 했습니까? 그렇다면 답을 –

답변

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