Skip to content

Commit b01a58a

Browse files
Fix bug in ViewCapture computing Rect for PixelCopy
When SDK > 34 the PixelCopy method was not treating the Rect as documented, instead of applying it as a Rect local to the coordinates of the View it was being applied as a Rect relative to the window. PiperOrigin-RevId: 634008870
1 parent a6efc73 commit b01a58a

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
**Bug Fixes**
88

99
* Remove unused androidx.test.annotation dependency
10+
* Fix `Rect` handling in `ViewCapture` for SDK >= 34 for non root views.
1011

1112
**New Features**
1213

core/java/androidx/test/core/view/ViewCapture.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,7 @@ private suspend fun View.generateBitmapFromPixelCopy(
217217
return suspendCancellableCoroutine<Bitmap> { cont ->
218218
var bounds = getBoundsInSurface()
219219
if (rect != null) {
220-
bounds =
221-
Rect(
222-
bounds.left + rect.left,
223-
bounds.top + rect.top,
224-
bounds.left + rect.right,
225-
bounds.top + rect.bottom,
226-
)
220+
bounds = Rect(rect).apply { offset(bounds.left, bounds.top) }
227221
}
228222
val onCopyFinished =
229223
PixelCopy.OnPixelCopyFinishedListener { result ->
@@ -302,10 +296,17 @@ private suspend fun View.generateBitmapFromPixelCopy(
302296
destBitmap: Bitmap,
303297
rect: Rect? = null,
304298
): Bitmap {
299+
val boundsInWindow = getBoundsInWindow()
300+
val sourceRect =
301+
if (rect != null) {
302+
Rect(rect).apply { offset(boundsInWindow.left, boundsInWindow.top) }
303+
} else {
304+
boundsInWindow
305+
}
305306
return suspendCancellableCoroutine<Bitmap> { cont ->
306307
val request =
307308
PixelCopy.Request.Builder.ofWindow(this)
308-
.setSourceRect(rect ?: getBoundsInWindow())
309+
.setSourceRect(sourceRect)
309310
.setDestinationBitmap(destBitmap)
310311
.build()
311312
val onCopyFinished =

0 commit comments

Comments
 (0)