package com.manny.utils;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
/**
* Created by manny on 2017/12/28.
*/
public class ScreenUtils
{
private ScreenUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException( “cannot be instantiated” );
}
/**
* 获得屏幕密度density
*/
public static float getDensity( Context context )
{
return context.getResources().getDisplayMetrics().density;
}
/**
* 获得屏幕宽度
*/
public static int getScreenWidth( Context context )
{
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获得屏幕高度
*/
public static int getScreenHeight( Context context )
{
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获得状态栏的高度
*/
public static int getStatusHeight( Context context )
{
if( context != null )
{
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier( "status_bar_height", "dimen", "android" );
if( resourceId > 0 )
{
statusBarHeight = res.getDimensionPixelSize( resourceId );
}
return statusBarHeight;
}
return -1;
}
/**
* 获取当前屏幕截图,包含状态栏
*/
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;
}
}