2013-07-04 19 views
1

,所以我創造我的看法編程像這樣:獲取Android的一個編程方式創建的ImageView的座標

for (int i = 0; i < num_devices; i++) { 

     ImageView imageView = new ImageView(this); 
     LinearLayout.LayoutParams vp = new LinearLayout. 
       LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     imageView.setLayoutParams(vp); 

     try { 
      Class res = R.drawable.class; 
      Field field = res.getField(device_types.get(i)); 
      int resId = field.getInt(null); 
      imageView.setImageResource(resId); 
     } 
     catch (Exception e) { 
      Log.e("MyTag", "Failure to get drawable id.", e); 
     } 

     LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices); 
     link_devices.addView(imageView); 
} 

,我試圖讓ImageView的座標:

我曾嘗試以下方法,但我沒有得到任何地方

 imageView.getDrawable().getBounds().centerX() /centreY 
     imageView.getX()/getY 
     imageView.getLocationInWindow();/onScreen(); 

可能有人請向我解釋如何得到這是繪製圖像的cooridinates這樣我就可以畫一個框周圍或繪製兩個圖像之間的線(即計劃的功能)

+0

你最終做了什麼?目前有同樣的問題。 – DanCat

+0

@DanCat檢索getTop()和getLeft()適用於您的x,y座標。 – portfoliobuilder

回答

0
int top = imageView.getTop(); 
int left = imageView.getLeft(); 

從那裏您可以得到左上角的座標。

+0

,它不工作,它只是給我零 – cxzp

+0

這意味着該視圖是在其父母的座標(0,0)。如果將父代嵌套在另一個視圖中,請使用相同的技巧。 – SceLus

+0

它不是嵌套的,它不在左上角 – cxzp

相關問題