I am using the following code snippet to create a bitmap with text.
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Style.FILL);
paint.setColor(fontColor);
paint.setTextSize(fontSize);
canvas.drawText("My Text", x, y, paint);
Here's the catch. How do I determine the size of the Bitmap to use in the canvas beforehand? For instance if I want a bitmap with "Hello World!" on it, I want to find the width and height of it even before I draw the text on the canvas.
You can tyr this:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Rect bounds = new Rect();
paint.setStyle(Style.FILL);
paint.setColor(fontColor);
paint.setTextSize(fontSize);
paint.getTextBounds("My Text", 0, "My Text".length(), bounds);
int width = bounds.width();
int height = bounds.height();
canvas.drawText("My Text", x, y, paint);
Try this, it loads the bitmap, then gets the heigth and width, then you just have to draw it. Replace bitmap with your image name
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
bitmapHeigth = bitmap.getHeigth();
bitmapWidth = bitmap.getWidth();
Related
I want to create a Bitmap using the String. The problem is when I assign the Paint and String to the Canvas.
All I see is a dot/black pixel that is created is something wrong with the Configs that I am using?
Here is my code below:
private void createBitmap(){
int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getApplicationContext().getResources().getDisplayMetrics());
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(textSize);
paint.setColor(Color.BLACK);
int w = 500, h = 200;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap myBitmap = Bitmap.createBitmap(w, h, conf);
Canvas myCanvas = new Canvas(myBitmap);
myCanvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
myCanvas.drawText("Just a string", 0, 0, paint);
imageView = new ImageView(this);
imageView.setImageBitmap(myBitmap);
}
The y parameter is actually for the baseline of the text, so you won't really see anything with y == 0. The dot you're seeing is probably the descender of the "g" in "string".
Try changing to
myCanvas.drawText("Just a string", 0, 100, paint);
so at least you can see something.
Note: You are setting the text size based on density, but you are making the bitmap an absolute pixel size, so you are going to have to do some calculation to get the look you want.
Once you have your Paint configured, you can determine the height of the text in pixels by calling getFontMetrics() on the Paint, then looking at the FontMetrics values. ascent will be negative since it's measuring upward, so you can get a rough idea of the height by fm.descent - fm.ascent.
Here's a way to draw your text just below the top edge of the bitmap:
Paint.FontMetrics fm = paint.getFontMetrics();
int baseline = (int) - fm.ascent; // also fm.top instead of fm.ascent
myCanvas.drawText("Just a string", 0, baseline, paint);
please help me to draw a text with rounded rectangle as background. I have to draw many texts on a canvas and the text has rounded background. SO what I am trying is to write a function "createTextBitmap" which return a bitmap image so that we can draw image(which return by the function) on a main canvas.
the 'createTextBitmap'function can return a created bitmap, the bitmap image is the one,which contains the text with rounded edge background...
i have tried one, which gives below.
private Bitmap ProcessingBitmap(String text,Paint paint, boolean lastPoint){
Bitmap bm1 = null;
Bitmap newBitmap = null;
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float width = bounds.width();
float height =bounds.height();
float radius;
if (width > height){
radius = height/4;
}else{
radius = width/4;
}
Paint paint1 = new Paint();
paint1.setColor(Color.GREEN);
paint1.setStrokeWidth(5);
paint1.setStyle(Paint.Style.FILL);
float center_x, center_y;
center_x = width/4;
center_y = height/4;
final RectF rect = new RectF();
rect.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
Canvas canvas2 = new Canvas();
canvas2.drawRoundRect(rect, 0, 0, paint);
canvas2.drawText(text, 0, 0, paint);
return newBitmap;
}
and my question is How can we convert this canvas2 to a bitmap image? and image has the size of text bounds,
which look like
to convert your canvas into bitmap please do the following :
public Bitmap convertCanvasToBitmap(int width , int height) {
Bitmap drawnBitmap = null;
try {
drawnBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(drawnBitmap);
// now draw anything you want to the canvas
} catch (Exception e) {
e.printStackTrace();
}
return drawnBitmap;
}
so the idea is just pass the bitmap to the canvas , draw with the canvas it will be drawn into your bitmap .
and please refer to this answer here to see how to deal with the text size in the bitmap .
and please give me some feedback
Hope that helps .
you can create a bitmap, then call draw on that bitmap, something like this:
newBitmap = Bitmap.createBitmap(rect.width, rect.height, Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(newBitmap);
How can I draw text on a bitmap? I made this:
float width = Converter.convertDpToPixel(250, context);
float height = Converter.convertDpToPixel(40, context);
Bitmap myBitmap = Bitmap.createBitmap((int)width + 1, (int)height+1, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface tanger = Typeface.createFromAsset(context.getAssets(),"fonts/Tangerine_Bold.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(tanger);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(55);
paint.setTextAlign(Align.CENTER);
myCanvas.drawText(quote, 16, 60, paint);
return myBitmap;
but only display a part of the text and I donĀ“t know why.
Thank you
Your Bitmap is 40px in height, while your text is drawn starting from 60px below the top of your bitmap. Since your textSize is 55 part of it is still visible. You'll probably want to increase the height of your Bitmap.
In my application I used canvas to draw the text. now I want to set the width of the draw text. I can set only x , y position. I can't set width and height.
My problem
update
for example "India is my Country" or if any length text it goes outside of canvas that is outside of the background image.
i want to print "India is
my country" if i set width mean i think it goes to next line
#Override
protected void onDraw(Canvas canvas1)
{
Paint paint = new Paint();
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
canvas1.drawColor(Color.BLACK);
canvas1.drawBitmap(resizeImage1,10,5, null);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(25);
paint.setColor(Color.BLUE);
paint.setFakeBoldText(true);
paint.setTextSize(20);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas1.drawText(CameraText, 100,175, paint);
}
}
i have used to create a bitmap as follows and iam able to set the width and height
Bitmap finalImage = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT,
Bitmap.Config.RGB_565);
Bitmap tempImage = Bitmap.createBitmap(IMAGE_WIDTH / 2, IMAGE_HEIGHT,
Bitmap.Config.RGB_565);
Canvas g = new Canvas(finalImage);
Canvas gtemp = new Canvas(tempImage);
g.drawColor(Color.WHITE);
gtemp.drawColor(Color.WHITE);
Paint pnt = new Paint();
pnt.setColor(Color.BLACK);
pnt.setTextAlign(Paint.Align.CENTER);
pnt.setTextSize(40);
pnt.setTypeface(font1);
g.drawText(input, IMAGE_WIDTH / 2, 40, pnt);
Rect grct = new Rect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
Rect grctTemp = new Rect(0, 0, IMAGE_WIDTH / 2, IMAGE_HEIGHT);
gtemp.drawBitmap(finalImage, grct, grctTemp, pnt);
`
i am not sure what you mean but you can try the setStrokeWidth(2.0f)
i hope this helps :D
I have the following code to draw text.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setTextSize(400);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setTextAlign(Align.LEFT);
paint.setStyle(Style.FILL);
String text = "698";
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int textWidth = bounds.width();
int textHeight = bounds.height();
Bitmap originalBitmap = Bitmap.createBitmap(textWidth,
textHeight, Bitmap.Config.ARGB_8888);
Canvas singleUseCanvas = new Canvas(originalBitmap);
singleUseCanvas.drawColor(Color.BLUE);
singleUseCanvas.drawText(text, 0, textHeight, paint);
canvas.drawBitmap(originalBitmap, 0, 0, null);
}
I am getting undesired outcome, which its right and bottom sides are being cropped.
I avoid right side cropping, by using
float textWidth = paint.measureText(text);
Bitmap originalBitmap = Bitmap.createBitmap((int)(textWidth + 0.5),
textHeight, Bitmap.Config.ARGB_8888);
I am getting the following improvement
Yet. My bottom still being cropped. May I know what is the correct way to obtained rendered text height, which is analogy to rendered text width using paint.measureText?
I think i would be helpful to have a look at this post:
Android Paint: .measureText() vs .getTextBounds()
It have a good survey about sizing of rendered text and also text height which is your concern.