DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
float density = dm.density;
int width = dm.widthPixels;
WindowManager windowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
float density = dm.density;
int width = dm.widthPixels;
int height = dm.heightPixels;
通过真机(里程牌 480*854)代码测试打印数据:320*569 ,
而通过模拟器(480*854)测试打印数据为:480*854.
后来发现跟屏幕的密度density有很大的关联:
int density = dm.density;
里程牌(480*854)的密度为:1.5
G3(320*480)的密度为:1.0
因此:
if (width <= 320) {
width = (int) Math.ceil(width * density);
height = (int) Math.ceil(height * density);
因此:
if (width <= 320) {
width = (int) Math.ceil(width * density);
height = (int) Math.ceil(height * density);
}