在很多情况下,做的图片在不同设备上显示的大小不同,影响显示效果,或者模拟器运行正常,但手机上显示出异常,今天因为在图片上绘制文字时,文字无法设置到满意的大小,这个找了很多的相关资料,用这个比率后解决问题了,希望对大家有帮助。
//设置适配不同屏幕大小的比率
public float getRATIO(){
//获取当前设备屏幕大小
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
//计算与480*800的纵横比
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
float ratioWidth = (float)screenWidth / 480;
float ratioHeight = (float)screenHeight / 800;
float RATIO = Math.min(ratioWidth, ratioHeight);
if (ratioWidth != ratioHeight) {
int OFFSET_LEFT;
int OFFSET_TOP;
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;
}
}
//int TEXT_SIZE = Math.round(35 * RATIO);
//System.out.println(TEXT_SIZE);
return RATIO;
用你希望看到的文字大小*
RATIO之后,得到想到的效果。