2015-08-16 73 views
1

首先我很抱歉我的英語。從ImageView中單擊獲取圖像上x和y點的相應位置?

我正在做一個android項目,獲取ImageView中包含的圖像的一個點擊,並在圖像中根據點擊接收到的cordenadas x,y繪製一個圓。

我的問題是如何推廣到所有屏幕尺寸? 圖像轉到ImageView的將與CameraView拍攝的照片, 一個Mat()取決於屏幕的大小。 我可以在gentmotion模擬器(720x1290三星Galaxy S3,320dpi)中做到這一點,圖像320x240。但是,當在Galaxy Y(屏幕尺寸320x240)上測試無效時。 下面的代碼:

... onCreate 
im = (ImageView) findViewById(R.id.imageView1); 
final DisplayMetrics dm = getResources().getDisplayMetrics(); 
params = (LayoutParams) im.getLayoutParams(); 
params.width = 640; 
params.height = 480; 
im.setLayoutParams(params); 
Mat m = new Mat(); 
im.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      real_x = (float) (event.getX() * (160f/dm.densityDpi)); 
      real_y = (float) (event.getY() * (160f/dm.densityDpi)); 
      updateMat(real_x,real_y); 
} 


public void loadMat(){ 
    //ma = new Mat(); 
    InputStream inpT = getResources().openRawResource(R.drawable.image); 
    try {     
     m = MainActivity.readInputStreamIntoMat(inpT); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(), Bitmap.Config.ARGB_8888); 

    Utils.matToBitmap(m, bm); 
    Log.i("BTM", String.valueOf(bm.getHeight() + " " + bm.getWidth())); 

    im.setImageBitmap(bm); 



} 

public void updateMat(float real_x2, float real_y2){ 
    Point b = new Point(real_x2,real_y2); 
    //if is firstClick -> Load the ImageView image on a Mat 
    if (first_click) { 
     loadMat(); 
     first_click= false; 


    } 

    Core.circle(m, b, 10, new Scalar(0, 255, 0), 2); 

    Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888); 

    Utils.matToBitmap(m, bm); 
    Log.i("BTM", String.valueOf(bm.getHeight() + " " + bm.getWidth())); 

    im.setImageBitmap(bm); 
} 
+2

可能的[Android的重複:如何獲取圖像/ ImageView中的xy座標?](http://stackoverflow.com/questions/8909835/android-how-do-i-get-the-xy-座標 - 圖像 - 圖像視圖) – 2015-08-16 00:23:32

回答

1

我來處理這個疑難問題做了這樣的解決方案,當用戶點擊一個位置,我覺得相對於ImageView的位置的速度。可以說ImageView的寬度和高度是200 * 200,當用戶點擊100 * 200時,其比率是0.5,1。之後,在圖像上使用相同的速度。可以說圖像是600 * 600,用戶實際上點擊600x0.5 * 600 * 1 = 300 * 600。 編輯:嘗試下面的代碼。

public boolean onTouch(View v, MotionEvent event) { 
     if(event.getAction()==MotionEvent.ACTION_UP){ 
      int[] viewCoords = new int[2]; 
      imageView.getLocationOnScreen(viewCoords); 
      int touchX = (int) event.getX(); 
      int touchY = (int) event.getY(); 
      int imageViewX = touchX - viewCoords[0]; // X touch coordinate on  imageView 
      int imageViewY = touchY - viewCoords[1]; // Y touch coordinate on imageView 
      float rateOfXPosition = imageViewX/imageView.getWidth(); 
      float rateOfYPosition = imageViewY/imageView.getHeight(); 
      int xOnImage = (int)rateOfXPosition*image.getWidth(); 
      int yOnImage = (int)rateOfYPosition*image.getHeight(); 
     } 

    } 
+0

感謝收聽。所以,考慮到我的圖像完全填充ImageView(默認情況下已經是這樣),ImageView的大小是:'imageView.width =(int)(screen_width * 0.8);''和'imageView.height =(int)(screen_heigh * 0.8 );'我如何獲得點擊率並將其轉換爲圖像中相應的像素(Point(x,y))?對不起,我無法在代碼中看到它。 –

+0

http://stackoverflow.com/questions/11312128/get-the-touch-position-inside-the-imageview-in-android –

0

getX()getY()並不總是返回「絕對」值(或者我應該說相對於點擊查看)。有時他們會從之前的觸摸事件中返回一個三角洲。我建議使用getRawX()getRawY()

見本question/answer

的區別在於getX/Y返回座標相對於視圖(當不是增量),而一對getRawX/Y返回屏幕座標。如果從視圖座標需要增量,則可以使用View.getLocationOnScreen並使用這些值來抵消getRawX/Y

相關問題