安卓上多个ImageView触摸移动

15

下方是我的XML文件,它的名称是main.xml。


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:src="@drawable/icon"
        android:scaleType="matrix">
    </ImageView>
    <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:src="@drawable/bg_sound"
        android:scaleType="matrix"></ImageView>
</FrameLayout>

我的Java文件如下


import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Test1Activity extends Activity implements OnTouchListener {
    private static final String TAG = "Touch";
    // These matrices will be used to move and zoom image
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    // We can be in one of these 3 states

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView view = (ImageView) findViewById(R.id.imageView);
        view.setOnTouchListener(this);

        ImageView view1 = (ImageView) findViewById(R.id.imageView1);
        view1.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView view = (ImageView) v;
        // Dump touch event to log

        // Handle touch events here...
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_UP:
            savedMatrix.set(matrix);
            //matrix.postRotate(90);

            break;
        case MotionEvent.ACTION_MOVE:
                // ...
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - start.x, event.getY()
                        - start.y);
            break;
        }

        view.setImageMatrix(matrix);
        view.invalidate();
        return true; // indicate event was handled
    }

}
我能够实现触摸移动,但由于添加了2个ImageView,只有最新添加的ImageView可移动。
我猜问题在于layout_width="fill_parent"导致只有前面的ImageView被识别为可触摸的。如果我使用layout_width="wrap_content",那么ImageView只会在该图像大小的区域内移动并变得不可见。
如何解决这个问题?
谢谢。
4个回答

2

我几乎解决了你的问题。 由于时间不够,我没有进一步处理。 我对你的main.xml文件进行了如下更改:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

        <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
            android:layout_height="100dp" android:src="@drawable/ic_launcher"
            android:scaleType="matrix"
            android:layout_alignParentTop="true"
            >
        </ImageView>
        <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:src="@drawable/toyota_altis"
            android:scaleType="matrix"
            android:layout_below="@+id/imageView"></ImageView>

</RelativeLayout>

请记住要使用自己的图片并进行适当更改。 我使用了两张图片:
  1. ic_launcher
  2. toyota_altis

1
您可以添加一个切换按钮,点击该按钮后,使用以下方法将ImageView置于前端。
imageView1.bringToFront()/imageView2.bringToFront() 

1

只有一个视图处理onTouch操作。所以有几个选项:

1)返回“false”-这可能会对您有所帮助

2)使用“wrap_content”,不要使用ImageMatrix。将图像的左上角移动


0
在您的 Activity 文件中编写以下代码。
int windowwidth = getWindowManager().getDefaultDisplay().getWidth();
int windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
        }
        return true;
    }
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
        }
        return true;
    }
});

main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

并查看下面的SO答案链接以获取更多信息。

拖放两个图像


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接