Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 防止触摸和旋转时图像视图重叠_Android_Imageview - Fatal编程技术网

Android 防止触摸和旋转时图像视图重叠

Android 防止触摸和旋转时图像视图重叠,android,imageview,Android,Imageview,在我正在开发的应用程序中,在相对布局中有两个独立的图像视图。我希望两个图像都可以移动和旋转,以响应给定的动作事件。我的活动的XML是: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" a

在我正在开发的应用程序中,在相对布局中有两个独立的图像视图。我希望两个图像都可以移动和旋转,以响应给定的动作事件。我的活动的XML是:

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.vishalbisht.dragfeature.MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:id="@+id/Recycle"
    android:layout_alignParentLeft="true" />
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Ring"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true" />

</RelativeLayout>

当我触摸或拖动左边的图像时,它会很好,但当我单击“环”时,两个图像会重叠并一起移动/旋转。为什么会发生这种情况以及如何解决

更改布局xml,如下所示:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:id="@+id/Recycle"
    android:layout_alignParentLeft="true" />
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Ring"
    android:layout_toRightOf="@+id/Recycle"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true" />
我用这个来解决我的问题,我希望这能帮助别人


另外,将两个图像视图保留在父相对视图内的单独框架布局中。框架布局可以具有高度和宽度的属性wrap_内容

我刚刚尝试了您的解决方案,虽然这对于移动imageview id:Recycle肯定更好,但当我触摸imageview id:ring或拖动它时,仍然会遇到相同的问题。
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:id="@+id/Recycle"
    android:layout_alignParentLeft="true" />
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Ring"
    android:layout_toRightOf="@+id/Recycle"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true" />
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int pointerCount = event.getPointerCount();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
//                downCounter++;
//                Log.v("","Action Down"+downCounter);

                startClickTime = Calendar.getInstance().getTimeInMillis();

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

                final float rawX = event.getRawX();
                final float rawY = event.getRawY();

                // Remember where we started
                mLastTouchX = rawX;
                mLastTouchY = rawY;
                break;

            case MotionEvent.ACTION_MOVE:
//                moveCounter++;
//                Log.v("", "Action Move "+moveCounter);
                if (pointerCount == 1) {

                    final float x1 = event.getRawX();
                    final float y1 = event.getRawY();

                    // Calculate the distance moved
                    final float dx = x1 - mLastTouchX;
                    final float dy = y1 - mLastTouchY;

                    // Make sure we will still be the in parent's container
                    Rect parent = new Rect(0, 0, root.getWidth(), root.getHeight());

                    //Find what the bounds of view will be after moving
                    int newLeft = (int) (view.getX() + dx),
                            newTop = (int) (view.getY() + dy),
                            newRight = newLeft + view.getWidth(),
                            newBottom = newTop + view.getHeight();

                    if (!parent.contains(newLeft, newTop, newRight, newBottom)) {
                        Log.v("", "Out of Bounds");
                    } else {
                        // Move the object
                        view.animate().
                                x(x1 + dX).
                                y(y1 + dY).
                                setDuration(0).
                                start();
                        // Remember this touch position for the next move event
                        mLastTouchX = x1;
                        mLastTouchY = y1;
                        // Invalidate to request a redraw
                        root.invalidate();
                        break;
                    }
                }
//                if(mode==2)
                if (pointerCount == 2)
                {
                    long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                    if(clickDuration > MAX_CLICK_DURATION) {
                        //Log.v(TAG, "for rotation");

                        float degree = rotation(event);

                        //Log.v(TAG, "setting at angle" + degree);

                        view.setRotation(view.getRotation() + degree);
                        break;

                    }
                }
                break;

            case MotionEvent.ACTION_UP:
//                upCounter++;
//                Log.v(TAG,"Action Up"+upCounter);


           /* case MotionEvent.ACTION_POINTER_DOWN:
                Log.v(TAG, "ACTION POINTER DOWN");
                if (pointerCount == 2) {
                    flagFirstTouch = 1;
                }
                break;*/

        }
        return true;
    }