I have been playing around with Spoon for some time and I discovered a problem with the created screenshots. They do not incorporate the elevation that is present on CardView for example. This is because of the way how Spoon creates it's screenshots.
At first I didn't know if it was related to Spoon or my views, so I copied the Spoon implementation of taking screenshots and started experimenting. This is the implementation I took:
private static void takeScreenshot(File file, final Activity activity) throws IOException {
View view = activity.getWindow().getDecorView();
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), ARGB_8888);
if (Looper.myLooper() == Looper.getMainLooper()) {
// On main thread already, Just Do It™.
drawDecorViewToBitmap(activity, bitmap);
} else {
// On a background thread, post to main.
final CountDownLatch latch = new CountDownLatch(1);
activity.runOnUiThread(new Runnable() {
@Override public void run() {
try {
drawDecorViewToBitmap(activity, bitmap);
} finally {
latch.countDown();
}
}
});
try {
latch.await();
} catch (InterruptedException e) {
String msg = "Unable to get screenshot " + file.getAbsolutePath();
Log.e(TAG, msg, e);
throw new RuntimeException(msg, e);
}
}
OutputStream fos = null;
try {
fos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(PNG, 100 /* quality */, fos);
chmodPlusR(file);
} finally {
bitmap.recycle();
if (fos != null) {
fos.close();
}
}
}
Spoon client version:
1.6.4
. Spoon runner version:spoon-runner-1.7.1-jar-with-dependencies.jar
.I have been playing around with Spoon for some time and I discovered a problem with the created screenshots. They do not incorporate the elevation that is present on
CardView
for example. This is because of the way how Spoon creates it's screenshots.At first I didn't know if it was related to Spoon or my views, so I copied the Spoon implementation of taking screenshots and started experimenting. This is the implementation I took:
Because it was me giving odd results, I went searching for a solution and stumbled upon this post: https://stackoverflow.com/questions/34795786/how-to-capture-screenshot-with-shadow-using-getdrawingcache
I quickly tried to make a screenshot with the following implementation, which worked as expected (it is in kotlin).