Convert Text To Bitmap(Pixel) on Android

2019-03-20 01:53发布

问题:

I have an android application in which I need to download text from a website, convert it into bitmap format and display it on an LED-based display board.

I am struggling with the bitmap conversion.

Tried to use the following:

Bitmap mybitmap = Bitmap.createBitmap(100, 16, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(mybitmap);
c.drawText("0", 0, 0, paint);

But it doesn't seem to be working. Any suggestions?

Update:

Paint object is initialized like this:

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.MONOSPACE);

回答1:

I think you draw outside the image. Try setting y to 16.

c.drawText("0", 0, 16, paint);

Note that when drawing text the coordinate origin is the lower left coordinate corner.