Skip to content

Commit d00c465

Browse files
shai-almogclaude
andauthored
Fix screenshot API in Android implementation (#4107)
* Fix Android screenshot API to include PeerComponents The screenshot() method in AndroidImplementation was missing PeerComponents (native Android views like BrowserComponent, MapView, etc.) in the captured image on devices running Android < API 26 (Oreo). The issue was in the AndroidScreenshotTask.tryFallbackDraw() method, which only drew the CodenameOneSurface view itself. PeerComponents are added as siblings to the CodenameOneSurface within the parent RelativeLayout container, so they were not being captured. The fix: - Draw the parent container (RelativeLayout) instead of just the view - Calculate and apply proper canvas translation to align the content - Maintain backward compatibility by falling back to view-only drawing if no parent is found Note: The PixelCopy method (API 26+) already worked correctly as it captures the entire window, so this only affects older Android versions. * Fixed Android screenshot test --------- Co-authored-by: Claude <[email protected]>
1 parent 72e3105 commit d00c465

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Ports/Android/src/com/codename1/impl/android/AndroidScreenshotTask.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,35 @@ private void tryFallbackDraw(int w, int h) {
8383
try {
8484
final Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
8585
final Canvas canvas = new Canvas(bmp);
86-
// Draw the view hierarchy (includes background + children)
87-
((View)view).draw(canvas);
86+
87+
// Get the parent container that holds both the CodenameOneSurface and PeerComponents
88+
View viewToDraw = (View)view;
89+
android.view.ViewParent parent = viewToDraw.getParent();
90+
91+
// If the view has a parent (relativeLayout), draw the parent to include PeerComponents
92+
// Otherwise fall back to drawing just the view
93+
if (parent instanceof View) {
94+
View parentView = (View) parent;
95+
// Save the parent's current position
96+
final int[] parentLoc = new int[2];
97+
parentView.getLocationInWindow(parentLoc);
98+
final int[] viewLoc = new int[2];
99+
viewToDraw.getLocationInWindow(viewLoc);
100+
101+
// Calculate offset between view and parent
102+
final int offsetX = viewLoc[0] - parentLoc[0];
103+
final int offsetY = viewLoc[1] - parentLoc[1];
104+
105+
// Translate canvas to align view content correctly
106+
canvas.translate(-offsetX, -offsetY);
107+
108+
// Draw the parent view hierarchy (includes PeerComponents as siblings)
109+
parentView.draw(canvas);
110+
} else {
111+
// Fallback: draw only the view if no parent found
112+
viewToDraw.draw(canvas);
113+
}
114+
88115
postSuccess(bmp);
89116
} catch (Throwable t) {
90117
Log.e(t);
9.16 KB
Loading

0 commit comments

Comments
 (0)