在我的应用程序中,我想只在其父布局中拖动图像,但它在整个设备屏幕中拖动.我尽力实现这一目标,但没有找到任何解决方案.
我在这里添加了我的xml和代码.我搜索了这个,但无法找到解决方案.任何人都可以帮我解决这个问题?
我在图片中清楚地提到了我想要实现的目标.我想限制图像拖动那些y1和y2值.
我的xml:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_ android:layout_ android:id="@+id/mainlayout" tools:context=".MainActivity" >
我的活动:
public class MainActivity extends Activity { RelativeLayout rlImages,Cartoon_image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Cartoon_image=(RelativeLayout)findViewById(R.id.cartoon_image); rlImages=(RelativeLayout)findViewById(R.id.images); rlImages.setOnDragListener(new MyDragListener()); Cartoon_image.setOnTouchListener(new MyTouchListener()); } 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 { @Override public boolean onDrag(View v, DragEvent event) { int dragAction = event.getAction(); switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: Log.e("in entered","in enter"); break; case DragEvent.ACTION_DRAG_EXITED: Log.e("in exited","in exit"); break; case DragEvent.ACTION_DROP: Float s1=event.getX(); Float s2=event.getY(); int x=Integer.valueOf(s1.intValue()); int y=Integer.valueOf(s2.intValue()); View view = (View) event.getLocalState(); ViewGroup owner = (ViewGroup) view.getParent(); owner.removeView(view); RelativeLayout container = (RelativeLayout) v; container.addView(view); break; case DragEvent.ACTION_DRAG_ENDED: Log.e("xs"+(int)event.getX(),"xs"+(int)event.getX()); v.setVisibility(View.VISIBLE); if (dropEventNotHandled(event)) { v.setVisibility(View.VISIBLE); } view.setVisibility(View.VISIBLE); default: break; } return true; } private boolean dropEventNotHandled(DragEvent dragEvent) { return !dragEvent.getResult(); } } }![I have added the image for better understanding,Please find it][1]
1: