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

}

更多相关文章

  1. 跨进程存取数据
  2. android 下的内存状态获取
  3. 【Android】问题记录
  4. Activity瓦解坠落退出效果
  5. 小红点
  6. 获取系统联系人及电话
  7. Android6.0+权限申请工具类PermissionUtil
  8. Android获取SD卡大小
  9. android 登录界面 记住多组账号与密码

随机推荐

  1. ImageView属性详解
  2. libxxx.so- text relocations问题的终极
  3. Android系统定制之源码完美下载
  4. Flutter基本UI组件StatefulWidget与State
  5. Android(安卓)8.1 MT6739 预置apk 可卸载
  6. Android(安卓)3.0新亮点,新机会
  7. Android环形进度条
  8. Android教程之Android(安卓)SDK1.5模拟器
  9. cocos2dx中利用xcode 调用java中的函数
  10. Android(安卓)GreenDao 3.0使用实例讲解