【问题标题】:Get the touch position inside the imageview in android获取android中imageview内的触摸位置
【发布时间】:2012-07-03 13:35:46
【问题描述】:

我的活动中有一个 imageview,我可以通过 onTouchListener 获取用户触摸 imageview 的位置。我在用户触摸该图像的位置放置了另一个图像。我需要存储触摸位置(x,y),并在另一个活动中使用它来显示标签。我将触摸位置存储在第一个活动中。在第一个活动中,我的图像视图位于屏幕顶部。在第二个活动中,它位于屏幕底部。如果我使用从第一个活动存储的位置,它将标签图像放在顶部,而不是在我之前在第一个活动中单击的图像视图上。无论如何要在imageview中获得位置。

第一个活动:

cp.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            x = (int) event.getX();
            y = (int) event.getY();

            Log.v("touched x val of cap img >>", x + "");
            Log.v("touched y val of cap img >>", y + "");

            tag.setVisibility(View.VISIBLE);

            int[] viewCoords = new int[2];
            cp.getLocationOnScreen(viewCoords);

            int imageX = x - viewCoords[0]; // viewCoods[0] is the X coordinate
            int imageY = y - viewCoords[1]; // viewCoods[1] is the y coordinate
            Log.v("Real x >>>",imageX+"");
            Log.v("Real y >>>",imageY+"");

            RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
            ImageView iv = new ImageView(Capture_Image.this);
            Bitmap bm = BitmapFactory.decodeResource(getResources(),
                    R.drawable.tag_icon_32);
            iv.setImageBitmap(bm);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = x;
            params.topMargin = y;
            rl.addView(iv, params);

            Intent intent= new Intent(Capture_Image.this,Tag_Image.class);
            Bundle b=new Bundle();
            b.putInt("xval", imageX);
            b.putInt("yval", imageY);
            intent.putExtras(b);
            startActivity(intent);

            return false;
    }
});

在 TagImage.java 中我使用了以下内容:

im = (ImageView) findViewById(R.id.img_cam22);
b=getIntent().getExtras();
xx=b.getInt("xval");
yy=b.getInt("yval");

im.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
         int[] viewCoords = new int[2];
            im.getLocationOnScreen(viewCoords);

            int imageX = xx + viewCoords[0]; // viewCoods[0] is the X coordinate
            int imageY = yy+ viewCoords[1]; // viewCoods[1] is the y coordinate
            Log.v("Real x >>>",imageX+"");
            Log.v("Real y >>>",imageY+"");
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
        ImageView iv = new ImageView(Tag_Image.this);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.tag_icon_32);
        iv.setImageBitmap(bm);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                30, 40);
        params.leftMargin =imageX ;
        params.topMargin = imageY;
        rl.addView(iv, params);
        return true;

    }
});

【问题讨论】:

    标签: android touch imageview


    【解决方案1】:

    您可以按如下方式获取视图的左上角:

    int[] viewCoords = new int[2];
    imageView.getLocationOnScreen(viewCoords);
    

    从这个和触摸坐标你可以计算出ImageView内部的点:

    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    
    int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
    int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate
    

    【讨论】:

    • 谢谢...我使用了这段代码。我触摸了图像,我得到了 event.getX(),event.getY() 为 87,176。在 imageX,imageY 87,78。在第二个活动中,标签图像放置在 87,78 处。但我的图像在屏幕底部。
    • 您应该在第二个活动中使用int[] viewCoords = new int[2]; imageView.getLocationOnScreen(viewCoords); 获取ImageView 的屏幕坐标,然后将imageXimageY 添加到各自的viewCoords 值,并放置标签那里。它应该这样工作。
    • 我在第二个活动中就是这样使用的。 int[] viewCoords = new int[2];im.getLocationOnScreen(viewCoords); int imageX = "x_ac1"- viewCoords[0]; int imageY = "y_ac1"- viewCoords[1];并设置 imageX,imageY 位置来放置标签。但我得到了负 imageY 值。
    • 是的,因为在第一个活动中您必须减去触摸坐标和屏幕坐标,但在第二个活动中您必须添加它们。在您的第二个活动中将imageX = x_ac1 - viewCoords[0]; int imageY = y_ac1 - viewCoords[1]; 替换为imageX = x_ac1 + viewCoords[0]; int imageY = y_ac1 + viewCoords[1];,您将获得正确的坐标。
    • 哦,是的,我的错。我再次检查了代码,但我并没有真正看到逻辑中有任何错误。尝试调试您使用的 x,y 坐标,从中您应该能够找出错误所在。
    【解决方案2】:

    将这些行添加到 ImageView

    android:scaleType="fitXY" 
    

    android:adjustViewBounds="true"
    

    对于您的图像视图,这是因为图像在 IV 中没有其原始尺寸

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      相关资源
      最近更新 更多