2016-03-04 250 views
4

您好我一直在看開發商代碼放大視圖,我似乎不能是找出這個代碼該做的:getGlobalVisibleRect()究竟是什麼?

final ImageView expandedImageView = (ImageView) findViewById(
     R.id.expanded_image); 
expandedImageView.setImageResource(imageResId); 

// Calculate the starting and ending bounds for the zoomed-in image. 
// This step involves lots of math. Yay, math. 
final Rect startBounds = new Rect(); 
final Rect finalBounds = new Rect(); 
final Point globalOffset = new Point(); 

// The start bounds are the global visible rectangle of the thumbnail, 
// and the final bounds are the global visible rectangle of the container 
// view. Also set the container view's offset as the origin for the 
// bounds, since that's the origin for the positioning animation 
// properties (X, Y). 
thumbView.getGlobalVisibleRect(startBounds); 
findViewById(R.id.container) 
     .getGlobalVisibleRect(finalBounds, globalOffset); 
startBounds.offset(-globalOffset.x, -globalOffset.y); 
finalBounds.offset(-globalOffset.x, -globalOffset.y); 

1)具體我不是很確定什麼getGlobalVisibleRect(finalBounds,globalOffset)猜想做?

2)另外,startBounds.offset()假設要做什麼,-globalOffset.x,-globalOffset.y甚至意味着什麼?

+0

噢,謝謝。我發現之前和之前有聯繫,它仍然沒有幫助,所以我問這個問題。此外,我發現兩個stackoverflow帖子試圖回答這個問題,但答案非常模糊。 – Wowzer

+0

@HaniyehKhaksar鏈接已損壞 –

回答

5
  1. getGlobalVisibleRect(finalBounds,globalOffset)返回容器的視圖全球位置,並且globalOffset是整個屏幕的偏移量。所以在這段代碼中,globalOffset.x是0,globalOffset.y是75.(在我的手機中,75是狀態欄高度)如果我調用finalBounds.off(-globalOffset.x,-globalOffset.y),finalBounds有(0,0,origin-0,origin-75),意思是finalBounds是局部座標不是全局的。 容器視圖很重要,因爲它提供了兩個圖像的基準座標。

  2. 在調用startBounds.offset之前,startBounds具有thumbView的全局位置。 startBounds.offset()確實將startBounds作爲容器視圖的本地座標。 finalBounds.offset()做同樣的事情。現在startBounds和finalBounds具有相同的相對座標,以便使轉換動畫變得容易。

  3. 如果使用globalrect,寬度/高度將是錯誤的。