Android日常--自定义View

获取字体的长度

Rect rect = new Rect();

paint.getTextBounds(getLabel(), 0, getLabel().length(), rect);

int strwid = rect.width()/2;

int strhei = rect.height()/2;

canvas.drawText(getLabel(), cx-strwid, cy+strhei, paint);


获取屏幕size

@SuppressLint("NewApi")

public static Point getDisplaySize(Activity act) {

WindowManager m = act.getWindowManager();

Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

Point outSize = new Point();

d.getSize(outSize);

return outSize;

} else {

Point p = new Point(d.getWidth(), d.getHeight());

return p;

}

}

}

你可能感兴趣的:(Android日常--自定义View)