This code was supposed to convert text to image
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.MONOSPACE);
Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ALPHA_8);
float x = bm.getWidth();
float y = bm.getHeight();
Canvas c = new Canvas(bm);
c.drawText("Test", x, y, paint);
}
Is this code ok? If yes, how can I make this new bitmap visible on screen? I tried this code which produced an error
setContentView(c); //<- ERROR!
I am confused with the element Canvas as there is not such element in XML which I can use in the code.
setContentView(View)takes aViewand Canvas is not aView.I am not sure that you want to create a
Canvason your own. There are ways to get aCanvaspassed to you from the Android Framework though. One way you can do this is by creating a customView. To do this, you will need to create a new class that extendsView.When overriding a
Viewclass, you will have the ability to override theonDraw(Canvas)method. This is probably where you want to do what you are attempting to do in youronCreate()method in the code you posted.This link gives a good overview of what is required to create your own custom view.