您的位置:首页 > 移动开发 > Android开发

android如何让字体大小在不同分辨率中随之变化?

2013-05-16 11:47 495 查看
转自: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);


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐