获取在Android上的ImageView内的触摸位置 [英] Get the touch position inside the imageview in android

查看:127
本文介绍了获取在Android上的ImageView内的触摸位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动一ImageView的,我能够获得在用户触摸ImageView的,通过onTouchListener的位置。我把另一个图像,用户触摸了该图像。我需要存储的触摸位置(X,Y),并用它在另一个活动,来显示标记。余存储在第一活动的触摸位置。在第一个活动,我的imageview的在屏幕的顶部。在屏幕底部的第二个活动的。如果我用从第一acitvity存储的位置,其放置在标签图像的顶部,而不是在ImageView的,我在那里previously点击的第一个活动。反正是有得到的ImageView内的位置。

FirstActivity:

  cp.setOnTouchListener(新OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                // TODO自动生成方法存根
            Log.v(摸帽IMG&GT x的VAL;>中,event.getX()+);
                Log.v(摸帽IMG&GT的ŸVAL;>中,event.getY()+);
                X =(int)的event.getX();
                Y =(INT)event.getY();
                tag.setVisibility(View.VISIBLE);

                    INT [] viewCoords =新INT [2];
                    cp.getLocationOnScreen(viewCoords);

                    INT IMAGEX = X  -  viewCoords [0]; // viewCoods [0]是在X坐标
                    INT宜美= Y  -  viewCoords [1]; // viewCoods [1]是y坐标
                    Log.v(真正的X>>>中,IMAGEX +);
                    Log.v(真正的Y'GT;>>中,宜美+);

                    RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.lay_lin);
                    ImageView的IV =新ImageView的(Capture_Image.this);
                    位图BM = BitmapFactory.de codeResource(getResources()
                            R.drawable.tag_icon_32);
                    iv.setImageBitmap(BM);
                    RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                    params.leftMargin = X;
                    params.topMargin = Y;
                    rl.addView(IV,则params);
                     意向意图=新
                     意图(Capture_Image.this,Tag_Image.class);
                     叠B =新包();
                     b.putInt(XVAL,IMAGEX);
                     b.putInt(利用yval,宜美);
                     intent.putExtras(B);
                     startActivity(意向);

                返回false;
            }
        });




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



IM =(ImageView的)findViewById(R.id.img_cam22);
        B = getIntent()getExtras()。
        XX = b.getInt(XVAL);
        YY = b.getInt(利用yval);

        im.setOnTouchListener(新OnTouchListener(){

            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                 INT [] viewCoords =新INT [2];
                    im.getLocationOnScreen(viewCoords);

                    INT IMAGEX = XX + viewCoords [0]; // viewCoods [0]是在X坐标
                    INT宜美= YY + viewCoords [1]; // viewCoods [1]是y坐标
                    Log.v(真正的X>>>中,IMAGEX +);
                    Log.v(真正的Y'GT;>>中,宜美+);
                RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.lay_lin);
                ImageView的IV =新ImageView的(Tag_Image.this);
                位图BM = BitmapFactory.de codeResource(getResources()
                        R.drawable.tag_icon_32);
                iv.setImageBitmap(BM);
                RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(
                        30,40);
                params.leftMargin = ImageX的;
                params.topMargin =宜美;
                rl.addView(IV,则params);
                返回true;

            }
        });
 

解决方案

您能得到您的视图的左上角,如下所示:

  INT [] viewCoords =新INT [2];
imageView.getLocationOnScreen(viewCoords);
 

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

  INT touchX =(INT)event.getX();
INT敏感=(INT)event.getY();

INT IMAGEX = touchX  -  viewCoords [0]; // viewCoords [0]是在X坐标
INT宜美=棘手 -  viewCoords [1]; // viewCoords [1]是y坐标
 

I have a imageview in my activity and I am able to get the position where the user touch the imageview, through onTouchListener. 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. 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. Is there anyway to get the position inside the imageview.

FirstActivity:

cp.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
            Log.v("touched x val of cap img >>", event.getX() + "");
                Log.v("touched y val of cap img >>", event.getY() + "");
                x = (int) event.getX();
                y = (int) event.getY();
                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:



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:

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

这篇关于获取在Android上的ImageView内的触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆