0
我有一堆純色背景的意見。這些觀點可以拖動;我想根據視圖是否與另一個視圖相交來更改背景。下面是我使用的代碼MotionEvent.ACTION_UP
改變視圖背景顏色的奇怪問題
// Get all children of the parent view
for(int i = 0; i < ((ViewGroup) getParent()).getChildCount(); i++) {
View childOne = ((ViewGroup) getParent()).getChildAt(i);
// For each child, check for intersection with siblings
for(int z = 0; z < ((ViewGroup) getParent()).getChildCount(); z++) {
View childTwo = ((ViewGroup) getParent()).getChildAt(z);
if(childOne == childTwo)
continue;
Rect recOne = new Rect();
childOne.getHitRect(recOne);
Rect recTwo = new Rect();
childTwo.getHitRect(recTwo);
if (Rect.intersects(recOne, recTwo)) {
((EditView) childTwo).setPaintColor(Color.RED);
} else {
((EditView) childTwo).setPaintColor(Color.GREEN);
}
}
}
這應該遍歷每個視圖,並檢查視圖與另一個視圖相交。如果是這樣,請更改交叉視圖的背景顏色。所有視圖都擴展了EditView
類,該類處理自己的觸摸事件。我還應該提到,MotionEvent.ACTION_DOWN
帶來了「前」的感動,所以用戶可以很容易地操縱它,如果它隱藏在另一個視圖之後。下面介紹它目前是如何工作的:
這些視圖佈局使用RelativeLayout
。我開始認爲這些觀點的排序可能是這個原因,但我不確定。
感謝您的提示!我相信我也瞭解了這個問題。在循環視圖時,如果視圖不與視圖相交,則無論視圖是否已與其他視圖相交,它都將變爲綠色。我需要找到解決辦法。 – Raggeth 2015-02-10 21:56:50
@Raggeth我其實並沒有考慮你剛剛提到的情況。 – iRuth 2015-02-10 21:59:21