2016-02-04 128 views
2

我想獲得X和Y的觀點(ImageButton)。獲取0值爲X和Y座標的視圖(圖片按鈕)在android

當我嘗試找到X和Y點擊事件與下面的代碼我得到適當的X和Y座標視圖。

ImageButton imagebutton = findviewbyId(R.id.imagebutton);      
    imagebutton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int[] posXY = new int[2]; 
      v.getLocationOnScreen(posXY); 
      x = posXY[0]; 
      y = posXY[1]; 

      Log.d("X and Y Point", x + " " + y);   
     } 
    }); 

但我希望X和Y座標之前點擊視圖,所以我嘗試得到X和Y與下面的代碼。

ImageButton imagebutton = findviewbyId(R.id.imagebutton);      
    int[] posXY = new int[2]; 
    imageButton.getLocationOnScreen(posXY); 
    x = posXY[0]; 
    y = posXY[1]; 

    Log.d("X and Y Point", x + " " + y); 

但每當我試圖讓X與Y ClickListener我得到的X和Y爲0和0.So,我不無Clicklistener得到適當的X和Y。 任何人都可以幫助解決什麼是實際問題,我如何得到適當的X和Y沒有ClickListener。

+1

*服用時*你叫'imageButton.getLocationOnScreen(posXY)'?如果您在實際佈局之前調用它,它將始終爲'(0,0)'。您需要等待佈局來佈置視圖。 –

+0

那麼,我如何才能讓X和Y無法點擊呢?有沒有可能的方法? –

+0

[Here](http://stackoverflow.com/questions/22972022/why-does-calling-getwidth-on-a-view-in-onresume-return-0)你可以找到一些解決方案和一些解釋和一個有用的鏈接。 – Onik

回答

3

返回的值爲零的原因是因爲如果你調用這個方法尚未創建的ImageButton「的onCreate()」。 可以使用ViewTreeObserver獲得位置:

ViewTreeObserver vto = imagebutton.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     this.imagebutton.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

     int[] posXY = new int[2]; 

     imagebutton.getLocationOnScreen(posXY); 
     x = posXY[0]; 
     y = posXY[1]; 

     Log.d("X and Y Point", x + " " + y); 

    } 
}); 

快樂編碼

0

兩個選項:

  • onResume實現你的邏輯。
  • 使用ViewTreeObserver

    final ViewTreeObserver vto = findViewById(R.id.YOUR_VIEW_ID).getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
         vto.removeOnGlobalLayoutListener(this); 
    
         // Get X, Y of the ImageView here 
        } 
    }); 
    
+0

在刪除前檢查vto是否存在。 – Keshav

+0

@Keshav在這種情況下,不需要檢查。 –

+0

讓我檢查一下,順便說一句@Keshav什麼是vto? –

0

獲取有關onWindowFocusChanged()值。

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 

    ImageButton imagebutton = (ImageButton) findViewById(R.id.imagebutton); 
    int[] posXY = new int[2]; 
    imagebutton.getLocationInWindow(posXY); 
    int x = posXY[0]; 
    int y = posXY[1]; 

    Log.d("X and Y Point", x + " " + y); 
} 
2

post()在setContentView()之後被調用。

方法setContentView()結束於調用頂部視圖的ViewGroup.addView(),andaddView()調用始終觸發requestLayout()。接下來,requestLayout()將任務發佈到稍後要執行的主線程中。該任務將在視圖層次結構上執行度量和佈局。現在,如果您發佈另一個任務,它將被放入隊列後的佈局任務中,並因此總是在測量和佈局發生後執行。因此,您將始終擁有有效的尺寸。

https://stackoverflow.com/a/21938380

button.post(new Runnable() { 
    @Override 
    public void run() { 
     // get coordinates 
    } 
}); 
+0

@ HiI'mFrogatto,請幫我解釋一下。我想明白。 – Keshav

+0

當視圖附加到窗口afaik時,將調用此方法。 – Keshav

+0

但是你會不會針對UI線程運行,並且不保證在繪製佈局後啓動? –