2011-04-24 77 views
1

我想獲取圖像在屏幕上的座標。我目前在一個活動中有一個ImageView。我明白getLocationOnScreen()方法只能在佈局創建後調用,所以在oncreate函數中調用這個方法會返回[0,0]。但我不知道如何讓這個方法返回正確的值。我嘗試了各種超類方法,例如onstart方法或onTouchEvent方法,它仍然向我返回[0,0]。我目前擁有的代碼如下:ImageView getLocationtOnScreen android

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    // Some code here after which .. 
    image = (ImageView) findViewById(R.id.imageVfi);   
    image.setImageBitmap(imageData.entrySet().iterator().next().getValue()); 
} 

然後,我有我已經重寫

@Override 
public void onStart() 
{ 
    super.onStart();   
    image.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      int[] dim = new int[2]; 
      image.getLocationOnScreen(dim); 
      new AlertDialog.Builder(DisplayPicture.this) 
      .setIcon(R.drawable.icon) 
      .setTitle("Touch coordinates : " + 
        String.valueOf(dim[0]) + "x" + String.valueOf(dim[1])) 
      .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) 
       { 
        // TODO Auto-generated method stub 
       } 
      }).show(); 
      // image calculations go here 
      return true; 

     } 
    }); 
} 

這將返回0x0到我OnStart方法。任何幫助深表感謝。

+0

我想補充一點,我認爲這是把整個屏幕圖像,因爲當我打電話image.getHeight()函數返回800個像素,image.getWidth函數返回480個像素這是與屏幕分辨率相同。但是,實際可見的圖像並不佔用整個屏幕。任何想法如何我可以解決這個問題? – kushaldsouza 2011-04-24 03:44:36

+0

請注意,其餘空白區域不在原始圖像內。 – kushaldsouza 2011-04-24 04:09:40

+0

我剛剛意識到我在ImageView上被稱爲getLocationOnScreen方法,而我的圖像並不佔據整個視圖。所以這意味着我必須以某種方式將屏幕座標映射到實際的圖像座標。我不知道如何去做這個 – kushaldsouza 2011-04-24 05:00:03

回答

3

ImageView不提供在屏幕上獲取其圖像位置的方法。您只能查詢ImageView本身的位置。你可以嘗試使用ImageView.getImageMatrix(),但我不知道我們填充它當圖像被簡單地居中(而不是縮放等)

+0

非常感謝。按照建議,我能夠使用getImageMatrix獲得圖像座標。我在下面添加了我的解決方案 – kushaldsouza 2011-04-25 04:05:01

0

如果它只是您在活動中的圖像視圖,那麼它將默認放置在座標爲0x0的左上角。

+0

不,我已經使用android:scaleType =「center」定位它,所以返回的座標不正確。我有一個臨時的解決方法,即將其縮放以適應屏幕,但圖像會丟失其寬高比。 – kushaldsouza 2011-04-24 04:53:37

+1

@ k9ty嘗試用「適合中心」縮放圖像。然後你可以得到圖像矩陣並計算屏幕上圖像的大小。從中你可以得到座標=圖像視圖的位置+(圖像視圖的寬度或高度/ 2 - 圖像的寬度或高度/ 2)。您是否使用寬度或高度進行計算取決於屏幕和圖像的高寬比。 – siamii 2011-04-24 05:51:45

3
按照上述

解決方案給出建議後

解決方案:

遵循下面的Romain Guy提供的建議,我能夠獲得座標,但將以下代碼添加到onStart overidden方法中。

display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
      int orientation = display.getOrientation(); 
      float rowStart=0, rowEnd=0, columnStart=0, columnEnd=0; 
      if(orientation ==0) 
      { 
       final Matrix matrix = image.getImageMatrix(); 
       float[] values = new float[9]; 
       matrix.getValues(values); 
       rowStart = values[0]; 
       columnStart = values[5]; 
       rowEnd = image.getWidth()-rowStart; 
       columnEnd = image.getHeight()-columnStart; 
      }else if(orientation == 1) 
      { 
       final Matrix matrix = image.getImageMatrix(); 
       float[] values = new float[9]; 
       matrix.getValues(values); 
       rowStart = values[2]; 
       columnStart = values[3]; 
       rowEnd = image.getWidth()-rowStart; 
       columnEnd = image.getHeight()-columnStart; 
      }