图像如何不会超过另一个图像被拖动或移动?

时间:2015-05-15 03:20:29

标签: android image

我有一个代码,可以使用鼠标移动多个图像来拖动图像,但它会经过另一个图像。我想移动它而不会过去。有谁知道怎么办?图像不能相互花费。

     package br.com.example.teste8;

     import android.app.Activity;
     import android.os.Bundle;
     import android.view.MotionEvent;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.ImageView;
     import android.widget.RelativeLayout;

     public class MainActivity extends Activity implements View.OnTouchListener {

private ImageView mImageView;

private ViewGroup mRrootLayout;

private int _xDelta;
private int _yDelta;

RelativeLayout.LayoutParams layoutParams;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRrootLayout = (ViewGroup) findViewById(R.id.marco);

    mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50,50);
    layoutParams.leftMargin = 0;
    layoutParams.topMargin = 10;

    mImageView.bringToFront();
    mImageView.setLayoutParams(layoutParams);
    mImageView.setOnTouchListener(this);


    mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView2);

    RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(50,50);
    layoutParams1.leftMargin = 50;
    layoutParams1.topMargin = 10;

    mImageView.setLayoutParams(layoutParams1);
    mImageView.setOnTouchListener(this);


    mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView3);

    RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(50,50);
    layoutParams2.leftMargin = 100;
    layoutParams2.topMargin = 10;

    mImageView.setLayoutParams(layoutParams2);
    mImageView.setOnTouchListener(this);

}


public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_UP:
        break;

    case MotionEvent.ACTION_MOVE:
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

        layoutParams.leftMargin = X - _xDelta;
        layoutParams.topMargin = Y - _yDelta;
        layoutParams.rightMargin = -50;
        layoutParams.bottomMargin = -50;
        view.setLayoutParams(layoutParams);
        break;
    }
    mRrootLayout.invalidate();
    return true;
}
}

XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/marco"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
android:orientation="horizontal"
android:clickable="true">


 <TextView
 android:id="@+id/txt"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello_world"
 android:gravity="top" />

 <ImageView 

android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />

<ImageView 
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果图像重叠,您没有说明该怎么做。假设图像会相互阻挡,因此在移动时会导致重叠,因此您无需执行任何操作。 我修改了以下代码:

package br.com.example.teste8;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements View.OnTouchListener {

    private ImageView mImageView;
    private ImageView mImages[] = new ImageView[3];

    private ViewGroup mRrootLayout;

    private int _xDelta;
    private int _yDelta;

    RelativeLayout.LayoutParams layoutParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRrootLayout = (ViewGroup) findViewById(R.id.marco);

        mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                50, 50);
        layoutParams.leftMargin = 0;
        layoutParams.topMargin = 10;

        mImageView.bringToFront();
        mImageView.setLayoutParams(layoutParams);
        mImageView.setOnTouchListener(this);
        mImages[0] = mImageView;

        mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView2);

        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
                50, 50);
        layoutParams1.leftMargin = 50;
        layoutParams1.topMargin = 10;

        mImageView.setLayoutParams(layoutParams1);
        mImageView.setOnTouchListener(this);
        mImages[1] = mImageView;

        mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView3);

        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(
                50, 50);
        layoutParams2.leftMargin = 100;
        layoutParams2.topMargin = 10;

        mImageView.setLayoutParams(layoutParams2);
        mImageView.setOnTouchListener(this);
        mImages[2] = mImageView;

    }

    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_UP:
            break;

        case MotionEvent.ACTION_MOVE:
            int left = X - _xDelta;
            int top = Y - _yDelta;

            boolean overlapped = false;
            for(int i=0; i<mImages.length; i++ )
            {
                if( view == mImages[i] ) {
                    continue;
                }
                RelativeLayout.LayoutParams lP = (RelativeLayout.LayoutParams) mImages[i]
                        .getLayoutParams();
                if( left >= lP.leftMargin - lP.width &&
                    left <= lP.leftMargin + lP.width &&
                    top >= lP.topMargin - lP.height &&
                    top <= lP.topMargin + lP.height
                )
                {
                    overlapped = true;
                    break;
                }
            }

            if( overlapped == false ) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -50;
                layoutParams.bottomMargin = -50;
                view.setLayoutParams(layoutParams);
            }
            break;
        }
        mRrootLayout.invalidate();
        return true;
    }
}