Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.Rect;

import android.view.View;

public class Main {
    /**
     * get view screen shot bitmap.
     * @param v the view
     * @return bitmap
     */
    public static Bitmap capture(View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        Bitmap bitmap = v.getDrawingCache();
        v.setDrawingCacheEnabled(false);
        return bitmap;
    }

    /**
     * get activity screen shot bitmap. system status bar is not included.
     * @param activity the activity
     * @return bitmap
     */
    public static Bitmap capture(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
        Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
}