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

时间:2023-03-09 05:18:15
( 转 ) Android自绘字体大小paint.settextsize随分辨率大小变化
  1. 1.获取当前设备的屏幕大小
  2. DisplayMetrics displayMetrics = new DisplayMetrics();
  3. this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  4. 2.计算与你开发时设定的屏幕大小的纵横比(这里假设你开发时定的屏幕大小是480*800)
  5. int screenWidth = displayMetrics.widthPixels;
  6. int screenHeight = displayMetrics.heightPixels;
  7. float ratioWidth = (float)screenWidth / 480;
  8. float ratioHeight = (float)screenHeight / 800;
  9. RATIO = Math.min(ratioWidth, ratioHeight);
  10. if (ratioWidth != ratioHeight) {
  11. if (RATIO == ratioWidth) {
  12. OFFSET_LEFT = 0;
  13. OFFSET_TOP = Math.round((screenHeight - 800 * RATIO) / 2);
  14. }else {
  15. OFFSET_LEFT = Math.round((screenWidth - 480 * RATIO) / 2);
  16. OFFSET_TOP = 0;
  17. }
  18. }
  19. 3.根据上一步计算出来的最小纵横比来确定字体的大小(假定在480*800屏幕下字体大小设定为35)
  20. public static int TEXT_SIZE = Math.round(35 * RATIO);
  21. 4.根据上一步计算的字体大小来设定应用程序中字体的大小
  22. Paint paint = new Paint();
  23. paint.setTextSize(TEXT_SIZE);
  1. canvas.drawText("test", 0, 0, paint);
  2. from:http://blog.csdn.net/cq361106306/article/details/38400647