@@ -3,6 +3,7 @@ package com.harnessui
33import android.content.Context
44import android.graphics.Bitmap
55import android.graphics.Canvas
6+ import android.graphics.Rect
67import android.os.Handler
78import android.os.Looper
89import android.os.SystemClock
@@ -263,12 +264,25 @@ class UIHelperImpl(
263264 val height = targetView.height
264265
265266 if (width > 0 && height > 0 ) {
266- Log .i(TAG , " Capturing view $nativeId : w=$width h=$height " )
267+ val location = IntArray (2 )
268+ targetView.getLocationOnScreen(location)
269+ val viewRect = Rect (location[0 ], location[1 ], location[0 ] + width, location[1 ] + height)
270+ val rootRect = Rect (0 , 0 , root.width, root.height)
271+
272+ val isFullyOnScreen = rootRect.contains(viewRect)
267273 val bitmap = Bitmap .createBitmap(width, height, Bitmap .Config .ARGB_8888 )
268274 val canvas = Canvas (bitmap)
269275
270- // Draw the specific view
271- targetView.draw(canvas)
276+ if (isFullyOnScreen) {
277+ Log .i(TAG , " View $nativeId is fully on screen. Using screen-based screenshotting." )
278+ // Translate canvas to capture the view from the root view hierarchy
279+ canvas.translate(- location[0 ].toFloat(), - location[1 ].toFloat())
280+ root.draw(canvas)
281+ } else {
282+ Log .i(TAG , " View $nativeId is partially/fully off screen. Using direct view render." )
283+ // Draw the specific view directly
284+ targetView.draw(canvas)
285+ }
272286
273287 val outputStream = ByteArrayOutputStream ()
274288 bitmap.compress(Bitmap .CompressFormat .PNG , 100 , outputStream)
0 commit comments