【问题标题】:Android multiple ImageView moving on touchAndroid 多个 ImageView 在触摸上移动
【发布时间】:2011-10-19 14:47:44
【问题描述】:

下面是我的 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 个图像视图,因此只有最新添加的图像视图是可移动的。

我猜问题是layout_width="fill_parent",它只会导致触摸时识别出正面图像视图。如果我使用的是layout_width="wrap_content",那么 imageview 只会在该图像大小的区域中移动并且不可见。

如何解决这个问题?

谢谢,

【问题讨论】:

    标签: android imageview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      只有一个查看进程onTouch 操作。所以有几个选择:

      1) 返回 "false" - 它可以帮助你

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

      【讨论】:

        【解决方案3】:

        你还可以添加一个切换按钮,点击切换按钮,你可以使用这个将图像视图放在前面

        imageView1.bringToFront()/imageView2.bringToFront() 
        

        【讨论】:

          【解决方案4】:

          将以下代码写入您的活动文件。

          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 答案链接。

          Drag & Drop Two Images

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-12
            • 2016-08-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多