Saturday, 31 August 2013

Smooth text scaling in Android

Smooth text scaling in Android

I am trying to scale text smoothly in Android, but I've got the following
problem:
I have a separate thread in my application that is drawing text on a
bitmap. I am drawing that bitmap on the View object during onDraw event.
However, it seems that Android is ignoring small changes in text size. For
example:
The text that I draw using drawText command with text size 10.49f is drawn
of the same size as text that I draw using text size 10.00f (I assumed
text drawn with text size 10.00f should be slightly smaller than text
drawn with text size 10.49f). It seems as if Android is ignoring the
fractional parts of the float text size.
Here it is a simplified sample code where one can demonstrate the problem:
@Override
public void onDraw(Canvas c) {
try {
if (mBitmap == null)
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
mBitmap.eraseColor(Color.argb(0, 0, 0, 0));
Canvas cnv = new Canvas(mBitmap);
// !!! mPaint.setTextSize(10.00f);
mPaint.setTextSize(10.49f);
mPaint.setSubpixelText(true);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
cnv.drawText("This is a very long text, hopefully the scaling will
work normally.", 10.0f, 100.0f, mPaint);
c.drawBitmap(mBitmap, 0, 0, null);
} catch (Throwable t) {
t.printStackTrace();
}
}
How can I scale texts using drawText command smoothly, so that fractional
parts of the text size will also be taken in account?

No comments:

Post a Comment