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

转载于:https://www.cnblogs.com/woaixingxing/p/5780014.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值