ScreenUtil工具类:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
 
/**
 * 屏幕工具类,涉及到屏幕宽度、高度、密度比、(像素、dp、sp)之间的转换等。
 */
public class ScreenUtil {
 
	private ScreenUtil(){}
	
	/**
	 * 获取屏幕宽度,单位为px
	 * @param context	应用程序上下文
	 * @return 屏幕宽度,单位px
	 */
	public static int getScreenWidth(Context context){
		return getDisplayMetrics(context).widthPixels;
	}
	
	/**
	 * 获取屏幕高度,单位为px
	 * @param context	应用程序上下文
	 * @return 屏幕高度,单位px
	 */
	public static int getScreenHeight(Context context){
		return getDisplayMetrics(context).heightPixels;
	}
	
	/**
	 * 获取系统dp尺寸密度值
	 * @param context	应用程序上下文
	 * @return
	 */
	public static float getDensity(Context context){
		return getDisplayMetrics(context).density;
	}
	
	/**
	 * 获取系统字体sp密度值
	 * @param context	应用程序上下文
	 * @return
	 */
	public static float getScaledDensity(Context context){
		return getDisplayMetrics(context).scaledDensity;
	}
	
	/**
	 * dip转换为px大小
	 * @param context	应用程序上下文
	 * @param dpValue	dp值
	 * @return	转换后的px值
	 */
	public static int dp2px(Context context, int dpValue){
		return (int) (dpValue * getDensity(context) + 0.5f);
	}
	
	/**
	 * px转换为dp值
	 * @param context	应用程序上下文
	 * @param pxValue	px值
	 * @return	转换后的dp值
	 */
	public static int px2dp(Context context, int pxValue){
		return (int) (pxValue / getDensity(context) + 0.5f);
	}
	
	/**
	 * sp转换为px
	 * @param context	应用程序上下文
	 * @param spValue	sp值
	 * @return		转换后的px值
	 */
	public static int sp2px(Context context, int spValue){
		return (int) (spValue * getScaledDensity(context) + 0.5f);
	}
	
	/**
	 * px转换为sp
	 * @param context	应用程序上下文
	 * @param pxValue	px值
	 * @return	转换后的sp值
	 */
	public static int px2sp(Context context, int pxValue){
		return (int) (pxValue / getScaledDensity(context) + 0.5f);
	}
	
	/** 
     * 获得状态栏的高度 
     *  
     * @param context 
     * @return 
     */  
	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;
	}
	
	/** 
     * 获取当前屏幕截图,包含状态栏 
     * @param activity 
     * @return 
     */ 
	 public static Bitmap snapShotWithStatusBar(Activity activity){  
		 View decorView = activity.getWindow().getDecorView();
		 decorView.setDrawingCacheEnabled(true);
		 decorView.buildDrawingCache();
		 Bitmap bmp = decorView.getDrawingCache();
		 int width = getScreenWidth(activity);
		 int height = getScreenHeight(activity);
		 Bitmap bitmap = null;
		 bitmap = Bitmap.createBitmap(bmp, 0, 0, width, height);
		 decorView.destroyDrawingCache();
		 return bitmap;
	 }
	
	 /** 
	  * 获取当前屏幕截图,不包含状态栏 
	  * @param activity 
	  * @return 
	  */  
	 public static Bitmap snapShotWithoutStatusBar(Activity activity){  
		 View decorView = activity.getWindow().getDecorView();
		 decorView.setDrawingCacheEnabled(true);
		 decorView.buildDrawingCache();
		 Bitmap bmp = decorView.getDrawingCache();
		 Rect frame = new Rect();
		 activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		 int statusHeight = frame.top;
		 
		 int width = getScreenWidth(activity);
		 int height = getScreenHeight(activity);
		 Bitmap bitmap = null;
		 bitmap = Bitmap.createBitmap(bmp, 0, statusHeight, width, height - statusHeight);
		 decorView.destroyDrawingCache();
		 return bitmap;	
	 }
	 
	/**
	 * 获取DisplayMetrics对象
	 * @param context	应用程序上下文
	 * @return
	 */
	public static DisplayMetrics getDisplayMetrics(Context context){
		WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(metrics);
		return metrics;
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值