John Jared
John Jared

Reputation: 800

Android writing on Bitmap with canvas

How do I make my editText what is written in, get written on the Bitmap I found this code, but it doesn't work

EditText et = (EditText) findViewById(R.id.etWrite);
Bitmap b = Bitmap.createBitmap(500,500,Bitmap.Config.Alpha_8);
Canvas c = new Canvas(b);
et.draw(c);

I think I should use currentBitmap I think, I tried but it doesn't work

Upvotes: 0

Views: 1000

Answers (4)

johann316
johann316

Reputation: 63

Ok, so this might be a bit late, but basically, you need to use these calls to write onto your canvas. First, use an OnFocusChanged listener to set your edittext text to a string, then write it onto your canvas in the Ondraw(c) call for the canvas. Before you do, though you need a string to write to (etstring) from your edittext, and you need to declare a paint object.

 Paint paint= new Paint();
 paint.setTypeface(Typeface.SERIF); //sets typface
 int textx = screenwidth/2;  //use screenwidth/2 to center the text
 c.drawText(etstring, textx, 85, paint); //85 is the height

Upvotes: 0

blessanm86
blessanm86

Reputation: 31779

This code should be enough to put the generated bitmap to a view.

EditText et = (EditText) findViewById(R.id.etWrite);
et.buildDrawingCache(); 
Bitmap bitmap = et.getDrawingCache();

Now use the following line for a normal view

view.setBackgroundDrawable(new BitmapDrawable(bitmap));

for imageview use

imageview.setImageDrawable(new BitmapDrawable(bitmap));

AFAIK the draw method will overwrite the pixel values of the bitmap you pass to it using the canvas.

Upvotes: 1

HandlerExploit
HandlerExploit

Reputation: 8251

Bitmap.Config.ALPHA_8 only draws the alpha channel. Use Bitmap.Config.ARGB_8888.

Upvotes: 1

yugidroid
yugidroid

Reputation: 6690

Try something like I show you blow:

EditText et = (EditText) findViewById(R.id.etWrite);
et.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache());
Canvas c = new Canvas(bmp);
et.draw(c);

Hope it works!

Upvotes: 1

Related Questions