get ScreenShot from Activity and return a Bitmap - Android User Interface

Android examples for User Interface:ScreenShot

Description

get ScreenShot from Activity and return a Bitmap

Demo Code


//package com.java2s;
import android.app.Activity;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.util.DisplayMetrics;
import android.view.View;

public class Main {

    public static Bitmap getScreenShot(Activity activity, Config config) {
        View view = activity.getWindow().getDecorView().getRootView();
        DisplayMetrics displayMetrics = activity.getResources()
                .getDisplayMetrics();/*www. ja va 2 s .c o  m*/
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        Bitmap b = Bitmap.createBitmap(width, height, config);
        view.draw(new Canvas(b));
        return b;
    }
}

Related Tutorials