2012-07-03 5 views
1

내 활동에 이미지 뷰가 있으며 onTouchListener를 통해 사용자가 이미지 뷰를 터치하는 위치를 얻을 수 있습니다. 나는 사용자가 그 이미지를 터치하는 곳에 다른 이미지를 놓았다. 태그를 표시하려면 터치 위치 (x, y)를 저장하고 다른 활동에서 사용해야합니다. 첫 번째 활동에서 터치 위치를 저장했습니다. 첫 번째 활동에서 화면 상단의 내 이미지 뷰. 두 번째 활동은 화면 하단에 있습니다. 첫 번째 활동에서 저장 한 위치를 사용하면 이전에 첫 번째 활동을 클릭 한 이미지 뷰가 아니라 상단에 태그 이미지를 배치합니다. 어쨌든 이미지 뷰 내부에서 입장을 얻으 려는가?android의 이미지 뷰 내부에서 터치 위치 가져 오기

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; 

      } 
     }); 

답변

19

다음과 같이보기의 왼쪽 상단 모서리를 얻을 수 있습니다 :이에서

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

터치 당신이 이미지 뷰 내부의 지점을 계산할 수 있습니다 좌표 :

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 
+0

감사합니다 ...이 코드를 사용했습니다. 나는 이미지를 건드렸다. event.getX(), event.getY()에 87,176을 넣었다. imageX에서는 imageY 87,78입니다. 두 번째 활동에서 태그 이미지는 87,78에 배치됩니다. 하지만 내 이미지는 화면 하단에 있습니다. – Manikandan

+1

두 번째 액티비티에서'int [] viewCoords = new int [2];로 ImageView의 화면 좌표를 얻어야합니다. imageView.getLocationOnScreen (viewCoords);'또한 각각의 viewCoords 값에'imageX'와'imageY'를 추가하고 거기에 태그를 놓습니다. 그렇게해야합니다. –

+0

두 번째 활동에서 이렇게 사용했습니다. int [] viewCoords = 새로운 int [2]; im.getLocationOnScreen (viewCoords); int imageX = "x_ac1"- viewCoords [0]; \t \t \t \t int imageY = "y_ac1"- viewCoords [1]; imageX, imageY 위치를 설정하여 태그를 배치합니다. 그러나 나는 부정적인 imageY 값을 얻었습니다. – Manikandan