究竟是什么getGlobalVisibleRect()?

Wow*_*zer 11 java android

嗨,我一直在查看缩放视图的开发人员代码,我似乎无法弄清楚这段代码假设要做什么:

 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);
Run Code Online (Sandbox Code Playgroud)

1)具体来说,我不太确定getGlobalVisibleRect(finalBounds,globalOffset)应该做什么?

2)此外,startBounds.offset()假设要做什么,-globalOffset.x,-globalOffset.y甚至是什么意思?

Ian*_*ong 16

  1. getGlobalVisibleRect(rect, offset) 返回一个布尔值,说明视图在全局坐标中是否可见。
  2. getGlobalVisibleRect(rect, offset),第一个rect是一个输出参数,将被设置为全局坐标中视图的可见矩形。
  3. getGlobalVisibleRect(rect, offset),第二个点也是一个输出参数,设置为View左上点的坐标。请注意,如下图所示,此偏移量可以具有负值,这意味着视图的左上角位于屏幕之外。

参考:https : //www.cnblogs.com/ai-developers/p/4413585.html

在此处输入图片说明


inh*_*ogo 8

  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,宽度/高度将是错误的.