android 在自定义view中获取屏幕宽度,并设置自定义控件位置

/**
 * 得到屏幕宽度
 *
 * @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);
}

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/80526481
今日推荐