I would like to convert text into bitmap, coded as follows:
Code:
public Bitmap convertTextToBM(String strength, int maxWidth, int maxHeight, String text)
{
TextView tv = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(maxWidth, maxHeight);
tv.setLayoutParams(layoutParams);
tv.setText(""+text);
tv.setTextColor(Color.BLACK);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, (maxHeight/2));
//strength
if (strength.equals("bold"))
{
tv.setTypeface(null, Typeface.BOLD);
}
tv.setGravity(Gravity.CENTER);
Bitmap testB = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, maxWidth, maxHeight);
tv.draw(c);
return testB;
}
Screenshot:
Question:
How to make the text drawn at center of the available space? Thanks!
One thing you can do is to try to create this in XML. Try to set the gravity to CENTER_VERTICAL | CENTER_HORIZONTAL, but the other thing is, when you create the XML, you can set the layout_gravity to CENTER_VERTICAL | CENTER_HORIZONTAL (you can do it through code as well but it's more complicated).
Instead of
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(maxWidth, maxHeight);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER);
Use
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER_VERTICAL |Gravity.CENTER_HORIZONTAL);
I have used another method to achieve a similar effect:
public Bitmap drawText(int textSize, String textColor, String strength, int textWidth, int maxHeight, String text)
{
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLACK);
//strength
if (strength.equals("bold"))
{
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
Related
I have a TextView which contains dynamic text (of varying length). In order to enable smooth pinch and zoom, I would like to convert this TextView to an ImageView and display that ImageView instead of the TextView. So I tried to set up the TextView programmcially like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_view);
String text = getIntent().getStringExtra(SongbookListFragment.SONG_TEXT_ARGS);
song_textview = new AppCompatTextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
song_textview.setLayoutParams(layoutParams);
song_textview.setPadding(PADDING,PADDING,PADDING,PADDING);
song_textview.setText(Html.fromHtml(text));
song_textview.setTextColor(Color.BLACK);
song_textview.setBackgroundColor(Color.TRANSPARENT);
song_textview.setHorizontallyScrolling(true);
song_textview.setTextSize(16);
}
My song_view.xml uses the zoomable PhotoView which extends ImageView:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/imageview_songtext"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
Now in onWindowFocusChanged method of the activity, I convert the TextView to a Bitmap and display it within the PhotoView like this:
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
int width = this.getWindow().getDecorView().getWidth();
int height = this.getWindow().getDecorView().getHeight();
Bitmap bitmapHolder = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmapHolder);
if (song_textview != null) {
song_textview.layout(0, 0, width, height);
song_textview.draw(c);
PhotoView songPhotoView = findViewById(R.id.imageview_songtext);
songPhotoView.setImageBitmap(bitmapHolder);
}
}
This works well except that I do not know how to set the height of the Bitmap within onWindowFocusChanged. If I use this.getWindow().getDecorView().getHeight() like in my code sample above, the length of the text is cut because this.getWindow().getDecorView().getHeight() returns the screen height and not the actual height of the TextView. Of course, the TextView is not added to the layout which is why there is no measured height. So how can I get the full length of the TextView to display it in my Bitmap?
Just call below setText method.
public void setText(String text){
Paint paint = new Paint();
paint.setTextSize(30);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(text, 0, text.length(), result);
int width= result.width();
//background color red
int color=Color.RED;
Bitmap img=drawText(text, width, color);
songPhotoView.setImageBitmap(img);
//background transparent
int colorT=Color.TRANSPARENT;
Bitmap img1=drawText(text,width,colorT);
songPhotoView.setImageBitmap(img1);
}
public Bitmap drawText(String text, int textWidth, int color) {
// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.parseColor("#ff00ff"));
textPaint.setTextSize(30);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
I need to create a canvas in which i have add two images as BITMAP and a TEXTVIEW at the bottom right with multiple line.
Here is my code which i have tried -
drawnBitmap = Bitmap.createBitmap(1000, 1500, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(drawnBitmap);
canvas.drawColor(getResources().getColor(R.color.creamColor));
// JUST CHANGE TO DIFFERENT Bitmaps and coordinates .
canvas.drawBitmap(b, 0, 0, null);
canvas.drawBitmap(b2, 630, 250, null);
//for more images :
// canvas.drawBitmap(b3, 0, 0, null);
// canvas.drawBitmap(b4, 0, 0, null);
/*Draw text*/
//TextPaint paintText=new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.BLACK);
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setTextSize(50);
Typeface tf;
tf = Typeface.createFromAsset(getAssets(), "HelveticaNeueLTStd-Lt.otf");
paintText.setTypeface(tf);
RelativeLayout layout = new RelativeLayout(this);
layout.setBackgroundColor(Color.GREEN);
layout.setDrawingCacheEnabled(true);
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setText(INNERTEXT);
textView.setTextSize(30);
textView.setWidth(300);
textView.setTextColor(Color.BLACK);
textView.setTypeface(tf);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout.addView(textView);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
// To place the text view somewhere specific:
// canvas.translate(500, 750);
canvas.drawBitmap(layout.getDrawingCache(), 500, 750, null);
// layout.draw(canvas);
I have divided the canvas into 4 equal parts, but the textview inside the 4. part is not centered.
image is set in imageview. I draw text successfully on image with the help of below code.
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.awais, myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
// canvas.drawCircle(60, 50, 25, paint);
canvas.drawText(String.valueOf(f), h, w, paint);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
but I want draw Buttons on image with different height and width.
If you already define your imageView inside relative layout in xml than just instantiate it by findViewById
//RelativeLayout relativeLayout =(RelativeLayout)(findViewById(.....));
otherwise dynamically create relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.setLayoutParams(lp);
relativeLayout.addView(imageview);
//than create button dynmaically
Button b = new Button(this);
b.setText("Button");
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);
relativeLayout.addView(b);
**Edit:**
In case you want arbitrary width and height than set button with and height like this
int width = 0; // assign your width
int height = 0; // assign your height
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
width,height);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);
This Example Work Correctly for conver text to image left align (TextPaint.Align.LEFT)
but when in change it to TextPaint.Align.RIGHT converted image was empty.
Convert text to image file on Android
this is my code:
final TextPaint textPaint = new TextPaint()
{
{
setColor(Color.rgb(0, 0, 0));
setTextAlign(TextPaint.Align.RIGHT);
setTextSize(textSize);
setTypeface(NormalFont(getApplicationContext()));
setAntiAlias(true);
}
};
final Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
final Bitmap bmp = Bitmap.createBitmap(bounds.width(), bounds.height()+buttonPadding, Bitmap.Config.ARGB_8888); //use ARGB_8888 for better quality //RGB_565
final Canvas canvas = new Canvas(bmp);
canvas.drawText(text, 0, textSize, textPaint);
return bmp;
}
Im trying to draw a string on top of an Image,The code works but the transparency is not obtained i have used several values for alpha,but does not work.
paint.setAlpha(alpha);
Can some one tell me what are the range of values for transparency or what im doing wrong here
public static Bitmap drawtext(Bitmap src, String txt,int alpha) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setAlpha(alpha);
paint.setColor(Color.RED);
paint.setTextSize(18);
paint.setAntiAlias(true);
paint.setUnderlineText(true);
canvas.drawText(txt, 20, 25, paint);
return result;
}
See:
http://developer.android.com/reference/android/graphics/Paint.html#setColor(int)
The setColor will overwrite the alpha value you just set before that call. That should work:
paint.setColor(Color.RED);
paint.setAlpha(alpha);
Try this code or download demo here
public Bitmap drawText(String text, int textWidth, int color) {
// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.parseColor("#ff00ff"));
textPaint.setTextSize(30);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
set the returned image to imageview
//background transparent
int colorT=Color.TRANSPARENT;
Bitmap img1=drawText(text,width,colorT);
img2.setImageBitmap(img1);