Reputation: 2175
I have a long string which I am sure will not fit on the image. So I ended up calculating lines and then writing line by line on the Bitmap
using a Canvas
. The problem is only the first line gets written. I will always be writing on this one image. The length of each line is fixed at 40 characters. Please check the code below:
private Bitmap prepareImageWithText(String text){
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.text_image); // Load your bitmap here
Bitmap aBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); // copy the bitmap because the one from the Resources is immutable.
Canvas canvas = new Canvas(aBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(75);
for(int i=0; i<calculateLines(text); i++) {
int beginFrom = i*40;
int endAt = beginFrom + 40;
if(endAt > text.length()){
endAt = text.length()-1;
}
String writableArea = text.substring(beginFrom, endAt);
canvas.drawText(writableArea, 100, 300+(i*100), paint);
canvas.save();
}
return aBitmap;
}
private int calculateLines(String text){
if(!TextUtils.isEmpty(text)){
int lines = text.length()/40;
return lines;
}
return 1;
}
Upvotes: 0
Views: 301
Reputation: 2075
for(int i=0; i<calculateLines(quote); i++) {
and
if(endAt > text.length()){
These two lines need your attention. Is the "text" and "quote" the same String?
Upvotes: 2
Reputation: 76426
You pass quote
to calculateLines
and you are only doing anything if it is not empty, yet, you use mQuote
's length
there. I think this confusion is the cause of your problem. You need to make sure that the value you pass is the one you want to pass and use the length
of quote
in your calculation instead of mQuote
's length
.
Upvotes: 1