1. 程式人生 > >Android手機各種截圖方法

Android手機各種截圖方法

/**
 * 螢幕相關的輔助類
 * 
 * @since 2015-05-27
 * @author lixi
 */
public class ScreenUtils {

    private ScreenUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 獲得螢幕高度
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE
); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 獲得螢幕寬度 */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE
); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 獲得狀態列的高度 */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName
("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } /** * 獲取當前螢幕截圖,包含狀態列 */ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /** * 獲取當前螢幕截圖,不包含狀態列 */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; } /** * 應用activity介面截圖 * @param activity * @param shareBtn 去除activity介面底部要不顯示的元件 * @param sportDate 運動日期元件 * @return */ public static Bitmap takeScreenShot(RelativeLayout topView,Activity activity, FrameLayout shareBtn, LinearLayout sportDate) { // 螢幕截圖,通過設定隱藏,截圖,再顯示的方式,去掉頭部,尾部的ui顯示 topView.setVisibility(View.INVISIBLE); shareBtn.setVisibility(View.INVISIBLE); sportDate.setVisibility(View.VISIBLE); View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); shareBtn.setVisibility(View.VISIBLE); sportDate.setVisibility(View.INVISIBLE); topView.setVisibility(View.VISIBLE); // 獲取狀態列高度 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(b1, 0, statusBarHeight,width,height-statusBarHeight ); view.destroyDrawingCache(); return b; } }