Java 如何为 Paint.setTextSize() 设置单位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3061930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set unit for Paint.setTextSize()
提问by Yverman
Is it possible to change the unit for Paint.setTextSize()
? As far as I know, it's pixel but I like to set the text size in DIP for multiple screen support.
可以换单位Paint.setTextSize()
吗?据我所知,它是像素,但我喜欢在 DIP 中设置文本大小以支持多屏幕。
采纳答案by Alex Volovoy
Convert it like this
像这样转换
// The gesture threshold expressed in dip
private static final float GESTURE_THRESHOLD_DIP = 16.0f;
// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels
from here http://developer.android.com/guide/practices/screens_support.html#dips-pels
从这里http://developer.android.com/guide/practices/screens_support.html#dips-pels
回答by Felipe Caldas
I know this topic is old and already answered but I would like to also suggest this piece of code:
我知道这个话题很旧并且已经回答了,但我也想推荐这段代码:
int MY_DIP_VALUE = 5; //5dp
int pixel= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
MY_DIP_VALUE, getResources().getDisplayMetrics());
回答by pelotasplus
And here is even shorter method to convert dp-s to px-els taking display metrics into account
这是将 dp-s 转换为 px-els 的更短的方法,将显示指标考虑在内
回答by Tanner Perrien
If your Paint object is being used to draw text on a Canvas, you can let the Canvas handle scaling for you.
如果您的 Paint 对象用于在 Canvas 上绘制文本,您可以让 Canvas 为您处理缩放。
When calling Canvas.drawText()
the text size is first determined by the passed in Paint
object, which can be set via Paint.setTextSize()
. The text size is automatically scaled by Canvas
based on the canvas density, which can be found using Canvas.getDensity()
.
调用时Canvas.drawText()
文本大小首先由传入的Paint
对象决定,可以通过Paint.setTextSize()
. 文本大小Canvas
根据画布密度自动缩放,可以使用Canvas.getDensity()
.
When setting the text size on a paint object that will be drawn on Canvas, work with a unit value of dp
or sp
and let Canvas handle the scaling for you.
在将在 Canvas 上绘制的绘画对象上设置文本大小时,请使用单位值dp
orsp
并让 Canvas 为您处理缩放。
回答by Suragch
The accepted answer is for gestures, not setting text size. The highest voted answer (at the time of this writing) is close, but the documentation recommends using sp
rather than dp
because in addition to being scaled for screen densities (as dp
values are), sp
is also scaled according to user preferred font sizes.
接受的答案是针对手势,而不是设置文本大小。最高投票的答案(在撰写本文时)接近,但文档建议使用sp
而不是dp
因为除了根据屏幕密度(如dp
值)进行sp
缩放外,还根据用户首选字体大小进行缩放。
From an int
in code
从int
代码
int spSize = 17;
float scaledSizeInPixels = spSize * getResources().getDisplayMetrics().scaledDensity;
mTextPaint.setTextSize(scaledSizeInPixels);
Or alternatively
或者替代地
int spSize = 17;
float scaledSizeInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spSize, getResources().getDisplayMetrics());
mTextPaint.setTextSize(scaledSizeInPixels);
From resources
来自资源
Or if you have the sp
or dp
value in resources:
或者,如果您在资源中具有sp
或dp
价值:
<resources>
<dimen name="fontSize">17sp</dimen>
</resources>
with
和
float scaledSizeInPixels = getResources().getDimensionPixelSize(R.dimen.fontSize);
mTextPaint.setTextSize(scaledSizeInPixels);
Other links
其他链接