Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to take a screenshot of a view, without showing the view?

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?

like image 845
android developer Avatar asked Jun 19 '13 11:06

android developer


People also ask

How do you take a screenshot of a specific area and not the whole screen?

An alternative way to the above is pressing Alt + Print Screen on your keyboard - this does exactly the same as the above, but the image copied to the clipboard is only that of the currently active window.

How do I take a screenshot of a particular section of my screen?

Press “Windows + Shift + S”. Your screen will appear grayed out and your mouse cursor will change. Click and drag on your screen to select the part of your screen you want to capture. A screenshot of the screen region you selected will be copied to your clipboard.


1 Answers

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);
    }

}
like image 78
android developer Avatar answered Oct 10 '22 04:10

android developer