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

  1. 1.获取当前设备的屏幕大小  
  2.   
  3. DisplayMetrics displayMetrics = new DisplayMetrics();  
  4. this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);  
  5.   
  6. 2.计算与你开发时设定的屏幕大小的纵横比(这里假设你开发时定的屏幕大小是480*800)  
  7.   
  8. int screenWidth = displayMetrics.widthPixels;  
  9. int screenHeight = displayMetrics.heightPixels;  
  10. float ratioWidth = (float)screenWidth / 480;  
  11. float ratioHeight = (float)screenHeight / 800;  
  12.           
  13. RATIO = Math.min(ratioWidth, ratioHeight);  
  14. if (ratioWidth != ratioHeight) {  
  15.     if (RATIO == ratioWidth) {  
  16.     OFFSET_LEFT = 0;  
  17.     OFFSET_TOP = Math.round((screenHeight - 800 * RATIO) / 2);  
  18.     }else {  
  19.     OFFSET_LEFT = Math.round((screenWidth - 480 * RATIO) / 2);  
  20.     OFFSET_TOP = 0;  
  21.     }  
  22. }  
  23.   
  24. 3.根据上一步计算出来的最小纵横比来确定字体的大小(假定在480*800屏幕下字体大小设定为35)  
  25.   
  26. public static int TEXT_SIZE = Math.round(35 * RATIO);  
  27.   
  28. 4.根据上一步计算的字体大小来设定应用程序中字体的大小  
  29.   
  30. Paint paint = new Paint();  
  31. paint.setTextSize(TEXT_SIZE);  

 

  1. canvas.drawText("test", 0, 0, paint);  
  2. from:http://blog.csdn.net/cq361106306/article/details/38400647
posted @ 2016-08-17 14:53  一只呆萌的萌呆  阅读(4764)  评论(0编辑  收藏  举报