Android 多个 ImageView 触摸移动

标签 android imageview

下面是我的 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 只会在该图像大小的区域内移动并且不可见。

如何解决这个问题?

谢谢,

最佳答案

我几乎解决了你的问题。 因为时间我没有前进 我在您的 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. 丰田_altis

关于Android 多个 ImageView 触摸移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7823165/

相关文章:

Android - 多选 ListView 的自定义适配器

android - 使用 Gradle 在 Eclipse 中添加依赖项

android - 底部边距或填充在 android 上的 xml 中的相对布局中不起作用

android - ImageView 上的 drawCircle 坐标错误

android - 为什么 setLayoutParams 不改变我的 ImageView 的大小?

java - Android - 将多个图像合并为一个 ImageView

android - 相机拍摄的图像不适合 imageview android

javascript - 为什么 WebView.loadUrl ("javascript: someFunction()")不适用于本地 html 文件?

Java从Microsoft SQL Server准确读取日期而不指定时区

android - 自定义 RelativeLayout 边框不显示