/** * 得到屏幕宽度 * * @return */ private int getScreenWidth() { WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.widthPixels;
}
设置自定义控件的宽高,
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = 4 * getScreenWidth() / 5; int height = 4 * getScreenWidth() / 6 + getScreenWidth() / 10; setMeasuredDimension(width, height); }
设置自定义控件的位置:
private void initView() { diameter = 3 * getScreenWidth() / 5; //弧形的矩阵区域 bgRect = new RectF(); bgRect.top = longdegree + progressWidth / 2 + DEGREE_PROGRESS_DISTANCE; bgRect.left = longdegree + progressWidth / 2 + DEGREE_PROGRESS_DISTANCE; bgRect.right = diameter + (longdegree + progressWidth / 2 + DEGREE_PROGRESS_DISTANCE); bgRect.bottom = diameter + (longdegree + progressWidth / 2 + DEGREE_PROGRESS_DISTANCE);}