在給定的像素位置有一個視圖。我有像素的座標。如何在給定的座標中查找視圖的ID?座標系中的視圖ID
4
A
回答
1
我覺得這樣的事情應該工作:
走你的視圖結構,找到孩子的意見(即那些沒有子視圖)是可見的。
使用View.getLocationOnScreen()在你的視圖來檢索他們的位置(上/左窗口座標)
使用getMeasuredWidth()/ getMeasuredHeight()來獲取視圖的寬度和高度
看看你的這個像素矩形內的座標落在
3
如果你是ViewGroup
內:
int count = viewgroup.getChildCount();
for (int i = 0; i < count; i++)
{
View view = viewgroup.getChildAt(i);
if (view.getX() == theX && view.getY() == theY)
return view.getId()
}
EDIT(kcoppock):中的for循環,我會做這樣的事情:
View view = viewgroup.getChildAt(i);
if(!view.getVisibility() == View.VISIBLE) continue;
int[] location = {0, 0};
view.getLocationOnScreen(location);
int right = location[0] + view.getWidth();
int bottom = location[1] + view.getHeight();
Rect bounds = new Rect(location[0], location[1], right, bottom);
if(bounds.contains(coordinatesX, coordinatesY)) return view.getId();
相關問題
- 1. ios視圖座標系
- 2. 座標系,秒圖
- 3. 轉換地圖座標到座標系
- 4. 計算新參考座標系中圖像像素的座標
- 5. 核心圖形座標系
- 6. 瞭解Qt的圖形視圖座標系統
- 7. TimeZoneInfo Id座標
- 8. Android中的屏幕和視圖座標
- 9. Android MotionEvent和視圖座標
- 10. Babylon.js中的座標系
- 11. SpriteKit中的座標系
- 12. C++中的座標系
- 13. Cocos2D中的座標系
- 14. Android中事件座標和視圖座標不匹配?
- 15. 查找圖像視圖內的座標?
- 16. CIFaceFeature的座標系
- 17. iOS目標C將座標從絕對圖像位置轉換爲我的視圖座標系
- 18. 將視圖端口座標轉換爲實際圖像座標
- 19. D3D11座標系
- 20. Matlab座標系?
- 21. Cocos2d座標系
- 22. OpenLayer座標系
- 23. libGDX座標系
- 24. PDFBox - 座標系
- 25. WPF座標系
- 26. MKOverlayView座標系
- 27. CATIA宏,2D座標中的3d點座標(從空間到繪圖視圖)
- 28. 查找ID座標
- 29. JAVA GUI圖形上的座標系
- 30. 瞭解從視圖座標到場景座標的映射
這已經很接近,但只打算當他點擊左上角的確切像素的工作的觀點。我會在一個建議中編輯。 – kcoppock 2012-07-12 15:03:20
確切地感謝您編輯 – Blackbelt 2012-07-12 15:04:59
。您還需要確保getChildAt()返回可見的視圖(getVisibility()== View.VISIBLE) – CSmith 2012-07-12 15:14:38