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

来源:互联网 发布:网络调研平台 编辑:程序博客网 时间:2024/06/06 13:08
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);

0 0
原创粉丝点击