1

I need to capture the principal layout on the screen, or just the whole screen if that is faster. I've tried two methods and both cause a noticable delay.

Method 1: drawing cache - takes about 80 ms

setDrawingCacheEnabled (true);
Bitmap oldLayout = Bitmap.createBitmap (getDrawingCache ());
setDrawingCacheEnabled (false);

Method 2: draw to canvas - takes about 50 ms

Bitmap oldLayout = Bitmap.createBitmap (getWidth (), getHeight (), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas (oldLayout);
int wSpec = MeasureSpec.makeMeasureSpec (getWidth (), MeasureSpec.EXACTLY);
int hSpec = MeasureSpec.makeMeasureSpec (getHeight (), MeasureSpec.EXACTLY);
measure (wSpec, hSpec);
layout (0, 0, getWidth (), getHeight ());
draw (canvas);

It seems that if I could capture directly from the screen that would be faster. I haven't seen a way to do that, however. Any other techniques worth trying?

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • `if I could capture directly from the screen that would be faster` do you mean without writing code? – Sagar May 02 '18 at 01:37
  • I think AI has a way to go yet, so that we can program computers without writing code :) – Peri Hartman May 02 '18 at 01:40
  • What I meant was, do you intent to use android studio to capture screen shot or you want to achieve it programmatically. The phrase `directly from the screen` was confusing. Have you tried [spoon](https://stackoverflow.com/questions/29929082/how-to-get-spoon-to-take-screenshots-for-espresso-tests) its only available for testing tough – Sagar May 02 '18 at 02:12
  • Capture in code. By "directly from screen", I'm assuming there is hardware memory for what's displayed on the screen. At some level of the OS, there is obviously a way to read and write that memory. Doing so, including time for Java to allocate memory for a bitmap, would take an extremely negligible amount of time. – Peri Hartman May 02 '18 at 04:13
  • I just looked at spoon - found the screen capture in the source code. They use method 2, above. Except they don't do the measure and layout steps. I realized I don't need that either and took them out. It did not make much difference in time, unfortunately. – Peri Hartman May 02 '18 at 04:36
  • Were you able to quantify the improvement? It would be great if you could update your question with this information. – Sagar May 02 '18 at 05:57
  • Insignificant. The measurements vary from trial to trial - not sure why - and the difference was within that variance. So, not significant. – Peri Hartman May 02 '18 at 13:46

0 Answers0