android - 如何限制 android 中 View 的拖动区?

标签 android drag

在我的应用程序中,我只想在其父布局中拖动图像,但它是在整个设备屏幕中拖动的。我尽力实现这一点,但没有找到任何解决方案。

我已经在此处添加了我的 xml 和代码。我搜索了这个但找不到解决方案。谁能帮我解决这个问题?

我在图片中清楚地提到了我想要实现的目标。我想限制那些 y1 和 y2 值中的图像拖动。

我的 xml:

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:id="@+id/mainlayout"
    tools:context=".MainActivity" >


 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/images">

       <!-- cartoon image  -->

       <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:background="@drawable/img_1"

         android:layout_centerInParent="true"  

        android:id="@+id/cartoon_image"> 

      <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:background="@drawable/play_btn"
            android:orientation="horizontal"
            android:layout_centerInParent="true"
            android:id="@+id/play_btn"
           >

           </RelativeLayout>
      </RelativeLayout> 

       <!-- end of cartoon image  --> 

     </RelativeLayout>
      </RelativeLayout>

我的 Activity :

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 : enter image description here

最佳答案

this 中的类似问题问题,已通过自己实现的拖放功能解决。

关于android - 如何限制 android 中 View 的拖动区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491071/

相关文章:

android - 如何实现Android TvView

android - 使背景继承自 selectableItemBackground

android - 使用我的服务器提供安全的 Facebook 身份验证

javascript - 光标:鼠标抓取javascript

javascript - jQuery UI 可拖动 : Custom Snap-To Method

android - 在 android 中与 whatsapp 共享图像

android - 向R.java添加文本文件资源

ios - 对于 iOS,如何在按钮内创建可拖动按钮?

javascript - 如何制作一个可以从各个 Angular 调整大小的div?

javascript - 如何在 iPhone 上拖动 'div' 元素