如何将视图拖动到其父级之外后拖动视图?

时间:2016-08-28 17:01:42

标签: android android-layout

我希望能够将黑盒子拖到橙色容器外面并再次拖动它。当黑色图像视图位于橙色框外时,它不会响应触摸。如果它被丢弃在橙色框中的某处,我只能重新拖动它。

Screenshot of app

我的代码如下:

activity_main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/holo_orange_dark"
        android:gravity="center"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:padding="20dp">

        <com.example.noname.myapplication.DraggableImageView
            android:id="@+id/activity_animation_icon_imageview"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="35dp"
            android:background="#000000"/>

    </LinearLayout>

</LinearLayout>

DraggableImageView.java

public class DraggableImageView extends ImageView {

    float dX, dY;

    public DraggableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                dX = getX() - event.getRawX();
                dY = getY() - event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:

                animate()
                        .x(event.getRawX() + dX)
                        .y(event.getRawY() + dY)
                        .setDuration(0)
                        .start();
                break;
            default:
                return false;
        }
        return true;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

提前致谢:)

2 个答案:

答案 0 :(得分:0)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:gravity="center">




        <launcher.simpleapp.vr.com.draggableimageview.DraggableImageView
            android:id="@+id/activity_animation_icon_imageview"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="35dp"
            android:background="#000000"/>



</LinearLayout>


Draggable ImageView 



import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by cvija on 8/28/2016.
 */
public class DraggableImageView extends ImageView{


        int dX, dY;
    private int _xDelta;
    private int _yDelta;

        public DraggableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }


        @Override
        public boolean onTouchEvent(MotionEvent event) {

            LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) getLayoutParams();
            _xDelta = dX - lParams.leftMargin;
            _yDelta = dY - lParams.topMargin;
            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    dX = (int)(getX() - event.getRawX());
                    dY =  (int)( getY() - event.getRawY());
                    break;

                case MotionEvent.ACTION_MOVE:

                    animate()
                            .x(event.getRawX() + dX)
                            .y(event.getRawY() + dY)
                            .setDuration(0)
                            .start();
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)
                            getLayoutParams();
                    layoutParams.leftMargin = dX - _xDelta;
                    layoutParams.topMargin = dY - _yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    setLayoutParams(layoutParams);
                    invalidate();
                    break;
                default:
                    return true;
            }
            return true;
        }
    }

The same is working fine for me 

答案 1 :(得分:0)

从某种意义上说,你的触摸将首先影响视图组,然后你将在视图中获得控件

因此,您将图像视图保留在线性布局中,以便您只能在线性布局中进行控制