在Android中获取ImageView内的触摸位置

5
我在我的活动中有一个ImageView控件,当用户触摸它时,我能够通过onTouchListener获取到他们的位置。我在触摸位置上放置了另一张图片。我需要存储触摸位置(x,y),并在另一个活动中使用它来显示标签。我将触摸位置存储在第一个活动中。在第一个活动中,我的ImageView位于屏幕顶部。在第二个活动中,它位于屏幕底部。如果我使用从第一个活动中存储的位置,则会将标签图像放置在顶部,而不是在之前在第一个活动中单击的ImageView上。有没有办法获取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;

    }
});
2个回答

22
您可以按照以下方式获取您视图的左上角:
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

2
你应该在第二个活动中使用 int[] viewCoords = new int[2]; imageView.getLocationOnScreen(viewCoords); 获取 ImageView 的屏幕坐标,然后将 imageXimageY 添加到相应的 viewCoords 值中,并将标签放置在那里。这样应该可以正常工作。 - Adam Monos
我在第二个活动中使用了以下代码:int[] viewCoords = new int[2];im.getLocationOnScreen(viewCoords); int imageX = "x_ac1"- viewCoords[0]; int imageY = "y_ac1"- viewCoords[1]; 并将imageX、imageY的位置设置为标签的位置。但是,我得到了负的imageY值。 - Manikandan
2
是的,因为在第一个活动中,您必须减去触摸坐标和屏幕坐标,但在第二个活动中,您必须将它们相加。在第二个活动中用 imageX = x_ac1 + viewCoords[0]; int imageY = y_ac1 + viewCoords[1]; 替换 imageX = x_ac1 - viewCoords[0]; int imageY = y_ac1 - viewCoords[1];,这样您就可以得到正确的坐标。 - Adam Monos
但是它没有被放置在我在第一个活动中点击的确切位置。 - Manikandan
2
哦,是我不好。我再次检查了代码,但实际上我并没有看到任何逻辑错误。尝试调试你使用的x,y坐标,从那里你应该能够找出错误所在。 - Adam Monos
显示剩余5条评论

0

将以下代码添加到ImageView中

android:scaleType="fitXY" 

android:adjustViewBounds="true"

如果您的图像视图显示不正确,那是因为图像在IV中没有保留其原始尺寸。


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