( 轉 ) 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM