画布上的Android文字

mr.*_*REW 1 android android-widget android-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);
Run Code Online (Sandbox Code Playgroud)

android 2.2上的文字看起来比android 4.0.3更大.

Dhe*_*.S. 5

可能是因为屏幕密度不同.

我猜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);
Run Code Online (Sandbox Code Playgroud)