转自:http://www.dewen.org/q/7564
1.获取当前设备的屏幕大小
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
2.计算与你开发时设定的屏幕大小的纵横比(这里假设你开发时定的屏幕大小是480*800)
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
float ratioWidth = (float)screenWidth / 480;
float ratioHeight = (float)screenHeight / 800;
RATIO = Math.min(ratioWidth, ratioHeight);
if (ratioWidth != ratioHeight) {
if (RATIO == ratioWidth) {
OFFSET_LEFT = 0;
OFFSET_TOP = Math.round((screenHeight - 800 * RATIO) / 2);
}else {
OFFSET_LEFT = Math.round((screenWidth - 480 * RATIO) / 2);
OFFSET_TOP = 0;
}
}
3.根据上一步计算出来的最小纵横比来确定字体的大小(假定在480*800屏幕下字体大小设定为35)
public static int TEXT_SIZE = Math.round(35 * RATIO);
4.根据上一步计算的字体大小来设定应用程序中字体的大小
Paint paint = new Paint();
paint.setTextSize(TEXT_SIZE);
canvas.drawText("test", 0, 0, paint);