Android自绘字体大小paint.settextsize随分辨率大小变化

转自:http://www.xuebuyuan.com/2225748.html

Android自绘字体大小paint.settextsize随分辨率大小变化

2015年02月21日  ⁄ 综合 ⁄ 共 774字 ⁄ 字号  小 中 大  ⁄ 评论关闭

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.根据上一步计算的字体大小来设定应用<span class="wp_keywordlink" style="margin: 0px; padding: 0px; border: 0px; background: transparent;"><a target=_blank href="http://www.xuebuyuan.com/" title="程序" target="_blank" style="text-decoration: none; color: rgb(1, 150, 227);">程序</a></span>中字体的大小

Paint paint = new Paint();
paint.setTextSize(TEXT_SIZE);
canvas.drawText("test", 0, 0, paint);

<p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">// 通过WindowManager获取</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">DisplayMetrics dm = new DisplayMetrics();</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">getWindowManager().getDefaultDisplay().getMetrics(dm);</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("heigth : " + dm.heightPixels);</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("width : " + dm.widthPixels);</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">// 通过Resources获取</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">DisplayMetrics dm2 = getResources().getDisplayMetrics();</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("heigth2 : " + dm2.heightPixels);</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("width2 : " + dm2.widthPixels);</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">// 获取屏幕的默认分辨率</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">Display display = getWindowManager().getDefaultDisplay();</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("width-display :" + display.getWidth());</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">System.out.println("heigth-display :" + display.getHeight());</p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 15px;">可以看到,第一、第三种方法都用到了getWindowManager()这个方法,而第二种方法没有用getWindowManager()。<span style="color: rgb(255, 102, 102);">有些网友说书写getWindowManager()后eclipse会报错</span>,说没有getWindowManager()这个方法,这是什么原因呢?因为getWindowManager()这个方法是在类Activity中的,如果你自己编写的类不是继承于类Activity,那么必然在这个类中书写代码就不能用到getWindowManager()这个方法。所以当你自己编写的类不是继承于类Activity,那么就只能应用第二种方法获取手机屏幕的大小。</p>

你可能感兴趣的:(Android自绘字体大小paint.settextsize随分辨率大小变化)