我创建了一个 Canvas 。我在 Canvas 上画文字。但是当我在不同版本的 android 上测试时,文本看起来不同。版本 4.х 和 2.2 之间的区别。
Bitmap btmText = Bitmap.createBitmap(140, 90, Bitmap.Config.ARGB_4444);
Canvas cnvText = new Canvas(btmText);
Typeface tf = tf = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(tf);
paint.setTextSize(50);
cnvText.drawText(text, 0, 5, 0, 55, paint);
文本在 android 2.2 上看起来比 android 4.0.3 大。
最佳答案
这可能是因为屏幕密度不同。
我想 Paint.setTextSize()
以像素为单位而不是 dp。要在所有设备上以英寸为单位显示相同的大小,您必须决定要以 dp 为单位显示的文本大小,并将该值转换为像素。
// The TEXT SIZE expressed in dp
private static final float MYTEXTSIZE = 50.0f;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
textSizePx = (int) (MYTEXTSIZE * scale + 0.5f);
paint.setTextSize(textSizePx);
关于 Canvas 上的 Android 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743098/