Android writing on Bitmap with canvas - android

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

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!

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

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.

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

Related

TextView to Bitmap, alignment issues

I am facing a situation in which I need to draw bitmap for a given text. I tried to render a TextView with required properties and then draw its bitmap using canvas. My try looks like this:
TextView nameTv = new TextView(context);
Bitmap bmp = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
nameTv.layout(0, 0, 200, 200);
nameTv.setGravity(Gravity.CENTER);
nameTv.setText("Test");
nameTv.setBackgroundColor(Color.RED);
nameTv.draw(canvas);
The problem with this code is that the text on TextView not center aligned, which I require.
Any suggestions are welcomed :)
Thanks,
Ammar
I think the problem is in measuring. You not calling measure for that and it don't have properly size. Call nameTv.measure() before layout().
Call measure like this:
int measureSpecWidth = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
int measureSpecHeight = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
nameTv.measure(measureSpecWidth, measureSpecHeight);

How to add number scale on android SeekBar

I'm using the out of the box android SeekBar component. Below I would like to add the numbers form 1 to 5 showing the progress of the SeekBar. I have problem distributing the numbers correctly on the seek bar.
Just like the image below
For drawing text over the seekbar thumb use this function
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
This function will call as
seekbar.setThumb(writeOnDrawable(R.drawable.thumbimage, mytext));
put thumbimage.png file in res/drawable/ and 'mytext' is the string which you want to write on top of that drawable
For complete conversation see the below link
how to set the seek bar thumb with a layout or with a TextView?
Hope this will be helpful to you.

How to add images to bitmap

Hi every am new to this android development.
Currently am developing drawing application with adding stamps/labels to drawn image.so i have done drawing part so now i have to implement adding stamps/labels to that drawn image.
So please help me out this..
Bitmap Rbitmap = Bitmap.createBitmap(bitmap).copy(Config.ARGB_4444, true);
Canvas canvas = new Canvas(Rbitmap);
canvas.drawBitmap(label, -9, Rbitmap.getHeight()-label.getHeight()-10, null);
canvas.save();
return Rbitmap;
Making your question little more specific will help you more.If I understood is correct this piece of code will help you out to draw a bitmap to a drawn canvas.
private Paint green = new Paint();
private int greenx , greeny;
green.setColor(Color.GREEN);
green.setAntiAlias(false);
canvas.drawCircle(greenx,greeny,20,green);
how to add image in this code replace drawcircle with image how ?
You could be a little more specific, i.e posting some code to show what you have to get more specific answers. Anyway, you can draw a bitmap on top of another bitmap by using something like this:
//You will have a Bitmap bottomBmp, a Bitmap topBmp and a Canvas canvas.
//If you are inside an onDraw() method the canvas will be provided to you, otherwise you will have to create it yourself, use a mutable bitmap of the same size as the bottomBmp.
canvas.drawBitmap(bottomBmp, 0, 0, null); //Draw the bottom bitmap in the upper left corner of the canvas without any special paint effects.
canvas.drawBitmap(topBmp, 0, 0, null); //Draw the top bitmap over the bottom bitmap, change the zeroes to offset this bitmap.
Try with this code:
private Bitmap background;
public birdClass(Context context) {
super(context);
background = BitmapFactory.decodeResource(getResources(),R.drawable.splash );
}

Text in OpenGL on an Object

I'm developing a sphere which should show text at certain positions. I tried it with CBFG and it's API, but I just counld saw a text at a fixed position on the screen. But I need to have a dynamic text which I can move around with a onTouchListener. With my Sphere it works fine, also with a Bitmap on it, but how can i make it with the text?
I solved my problem:
TextView textV = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(128, 128);
textV.setLayoutParams(layoutParams);
textV.setTextColor(Color.WHITE);
textV.setBackgroundColor(Color.TRANSPARENT);
textV.setGravity(Gravity.CENTER);
textV.setText(text);
textV.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap( textV.getLayoutParams().width, textV.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
textV.layout(0, 0, textV.getLayoutParams().width, textV.getLayoutParams().height);
textV.draw(c);
textV.setDrawingCacheEnabled(false);
With that code I'll get a Bitmap which I can use as a texture ;-)

Drawing a bitmap in a canvas after calling setBitmap doesn't work

I'm drawing a bitmap in a canvas and I want to have the result in a new bitmap, but I still have a black screen as result.
This is my code, part of the onDraw(Canvas canvas) method:
if (bitmapTemplate == null) {
canvasBis = new Canvas();
bitmapTemplate = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
drawZones(canvasBis,bitmapTemplate);
}
bitmapRes = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmapRes);
canvas.drawBitmap(bitmapTemplate, matrix, null);
My goal is to have a new bitmap (bitmapRes) by applying a matrix on an existing bitmap (bitmapTemplate). With this code I always have a black screen, but when I remove the line canvas.setBitmap(bitmapRes), I have a result but not in a new bitmap. Any ideas please? Maybe transparency? Thanks in advance.
drawZones draws some zones in bitmapTemplate.
You should try using the Canvas(Bitmap) constructor instead of the empty constructor. This sets the bitmap to the canvas for you.

Categories

Resources