简体   繁体   English

如何为Paint.setTextSize()设置单位

[英]How to set unit for Paint.setTextSize()

Is it possible to change the unit for Paint.setTextSize() ? 是否可以更改Paint.setTextSize()的单位? As far as I know, it's pixel but I like to set the text size in DIP for multiple screen support. 据我所知,它是像素,但我喜欢在DIP中设置文本大小以支持多屏幕。

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

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

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: 或者,如果资源中有spdp值:

<resources>
    <dimen name="fontSize">17sp</dimen>
</resources>

with

float scaledSizeInPixels = getResources().getDimensionPixelSize(R.dimen.fontSize);
mTextPaint.setTextSize(scaledSizeInPixels);

Other links 其他链接

And here is even shorter method to convert dp-s to px-els taking display metrics into account 这里有更短的方法将dp-s转换为考虑显示指标的px-els

https://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize(int) https://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize(int)

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() . 调用Canvas.drawText() ,文本大小首先由传入的Paint对象确定,该对象可以通过Paint.setTextSize()设置。 The text size is automatically scaled by Canvas based on the canvas density, which can be found using Canvas.getDensity() . 文本大小由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上绘制的绘制对象上设置文本大小时,使用单位值dpsp并让Canvas为您处理缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM