- 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);
- from:http://blog.csdn.net/cq361106306/article/details/38400647