13

Short question:

Suppose I have some kind of a layout file and I inflate it (or use the normal CTORs in code).

Instead of showing the inflated view, I wish to take a "screenshot" (a bitmap) of how it would look like under some limitations (of given width&height, even larger than the screen).

I do not wish to add the view to anywhere on the screen, but only hold it for this purpose and maybe add it later.

Such a thing could be useful for easy manipulations of how to place things. For example, I could use a layout that an image would be put inside it so that it would have a frame around it.

Is such a thing possible? If so, How?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Is this for Test App? If yes then you can check com.android.systemui.screenshot.ScreenshotTest – Pankaj Kumar Jun 19 '13 at 11:27
  • what do you mean "Test App" ? i want to know about it for any kind of app. – android developer Jun 19 '13 at 11:35
  • I can't test it currently, but the views `draw`-method, should be able to draw to a canvas, even if it is not added to any layout. You do however need to take it through all the layout-stages of its lifecycle. http://developer.android.com/reference/android/view/View.html#draw%28android.graphics.Canvas%29 – Jave Jun 19 '13 at 11:58
  • how would i make them all to draw to a bitmap instead of to the screen, and without really add them to the real views hierarchy ? – android developer Jun 19 '13 at 13:05

1 Answers1

17

OK, I've found a possible way to do it, based on this link :

public static Bitmap drawToBitmap(Context context,final int layoutResId,
                                  final int width,final int height)
{
    final Bitmap bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    final LayoutInflater inflater =
         (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(layoutResId,null);
    layout.setDrawingCacheEnabled(true);
    layout.measure(
         MeasureSpec.makeMeasureSpec(canvas.getWidth(),MeasureSpec.EXACTLY),
         MeasureSpec.makeMeasureSpec(canvas.getHeight(),MeasureSpec.EXACTLY));
    layout.layout(0,0,layout.getMeasuredWidth(),layout.getMeasuredHeight());
    canvas.drawBitmap(layout.getDrawingCache(),0,0,new Paint());
    return bmp;
}

Example of usage:

public class MainActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView iv = (ImageView)findViewById(R.id.image);
        final DisplayMetrics metrics = getResources().getDisplayMetrics();
        final Bitmap b = drawToBitmap(this,R.layout.test, metrics.widthPixels,
                                    metrics.heightPixels);
        iv.setImageBitmap(b);
    }

}
Sufian
  • 6,405
  • 16
  • 66
  • 120
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • @Nerves82 It was changed since last time. I would never put such a problematic thing here. Also, VirusTotal says it's ok, based on 67 Antiviruses: https://www.virustotal.com/en/url/c3279c186440c89052ae0dbd9ac2c1ed5e0663904eed15eae5c49cae348b2f37/analysis/1460416779/ https://www.virustotal.com/en/url/51b003e759a05a34c58ebcb3078de02e73ee779dbffb54ab19ef0711fdd81af5/analysis/1460416787/ – android developer Apr 11 '16 at 23:20
  • 1
    @Nerves82 Anyway, updated link to show previous content, using Wayback – android developer Apr 11 '16 at 23:21
  • Awesome. I figured it had changed in the 2 years since the answer was posted, but better safe then sorry. Thanks for editing it so quickly. – Sev Apr 11 '16 at 23:27
  • If drawing from existing view, use buildDrawingCache()/destroyDrawingCache() to create drawing cache forcefully. – Roger Huang Jan 09 '18 at 09:18