简体   繁体   English

获取android中imageview内的触摸位置

[英]Get the touch position inside the imageview in android

I have a imageview in my activity and I am able to get the position where the user touch the imageview, through onTouchListener. 我的活动中有一个imageview,我可以通过onTouchListener获得用户触摸imageview的位置。 I placed another image where the user touch over that image. 我在用户触摸该图像的位置放置了另一个图像。 I need to store the touch position(x,y), and use it in another activity, to show the tags. 我需要存储触摸位置(x,y),并在另一个活动中使用它来显示标签。 I stored the touch position in the first activity. 我将触摸位置存储在第一个活动中。 In the first activity, my imageview at the top of the screen. 在第一个活动中,我的图像视图位于屏幕顶部。 In the second activity its at the bottom of the screen. 在第二个活动中它位于屏幕的底部。 If I use the position stored from the first acitvity, it place the tag image at the top, not on the imageview, where I previously clicked in the first activity. 如果我使用从第一个活动中存储的位置,它会将标记图像放在顶部,而不是我之前在第一个活动中单击的imageview。 Is there anyway to get the position inside the imageview. 无论如何都要在imageview中获得位置。

FirstActivity: FirstActivity:

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;
    }
});

In TagImage.java I used the following: 在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;

    }
});

You can get the top left corner of your view as follows: 您可以按如下方式获取视图的左上角:

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

From this and the touch coordinates you can calculate the point inside the ImageView: 从这个和触摸坐标,您可以计算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

Add these lines to ImageView 将这些行添加到ImageView

android:scaleType="fitXY" 

and

android:adjustViewBounds="true"

to your image view it is because image are not having their original dimensions in IV 在图像视图中,这是因为图像在IV中没有原始尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM