UiAutomator有一个会显示XY的坐标,该工具类通过当前的XY坐标来确定其在这个屏幕的宽高比,之后的所有类型手机都根据宽高比例进行设置。
/** * 根据屏幕宽高进行点击 */ public class GetScreenWH { //传入横纵坐标点 private int eWidth = 0; private int eHeight = 0; public String TAG = "motejia"; public GetScreenWH(int width, int height){ this.eWidth = width; this.eHeight = height; } public int getWidth(){ //获取屏幕宽 WindowManager m = (WindowManager) FieldApplication.getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); m.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; Log.e(TAG, "getWidth: ============"+width); return width; } public int getHeight(){ //获取屏幕高 WindowManager m = (WindowManager) FieldApplication.getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); m.getDefaultDisplay().getMetrics(outMetrics); int height = outMetrics.heightPixels; Log.e(TAG, "getWidth: ============"+height); return height; } //根据宽算出当前比例 public int numWidth(){ int width = getWidth(); return width/eWidth; } //根据高算出当前比例 public int numHeight(){ int height = getHeight(); return height/eHeight; } }