android-将dp转换为float [英] android - convert dp to float

查看:463
本文介绍了android-将dp转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字体大小是12dp.

My font size is 12dp.

我正在使用TextPaint设置字体,因为我正在使用跨度.问题是TextPaint接受的参数在float中.我想知道如何将12 dp转换为float?

I'm setting the font using TextPaint, since I'm using a span. The problem is the parameter that TextPaint accepts is in float. I'm wondering how can I convert 12 dp to float?

推荐答案

来自android.content.res.Resources.getDimension(int id):

float twelveDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 12, 
                mContext.getResources().getDisplayMetrics() );

这篇关于android-将dp转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆