Hi Guys am drawing an text using canvas, and i need to change the textview everyhour,
if the textview extends the width it should be displayed in the next line
For example , i have a text like "Good Morning" and "Good Morning rachel ! welcome you"
The first textview Good morning will be correctly displayed in Single line but the second textview i need to print by two line
how can i draw it
String mytext = "hi how are you how was the day "
canvas.drawText(mytext , x, centerX, mTextPaint);
scale = resources.getDisplayMetrics().density;
TextPaint TextPaint=new TextPaint();
TextPaint.setARGB(255, 255, 255, 255);
TextPaint.setTextSize(28f);
TextPaint.setTypeface(myfonttime);
StaticLayout mTextLayout;
mTextLayout = new StaticLayout(myText, TextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int textHeight = mTextLayout.getHeight()- (int) (16 * scale +5.0);
int textWidth = canvas.getWidth() - (int) (16 * scale);
float x = cWidth / 2f - textWidth/2f - bounds.left;
float y = cHeight / 2f - textHeight/2f;
canvas.save();
canvas.translate(x, y);
mTextLayout.draw(canvas);
canvas.restore();
for example you have String like this
String text = "This is\nmulti-line\ntext";
Then draw in textview like this
canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);
Second way.
TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
// calculate x and y position where your text will be placed
textX = ...
textY = ...
canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();
for more refer this link
Related
What i am trying to do: I am trying to set ... after 7 characters in canavs for text
How to achieve this ?
private void drawText(Canvas canvas, float tmpAngle, float sweepAngle, String mStr) {
float cx = (mRadius) / 2 + mPadding;
float cy = (mRadius) / 2 + mPadding;
float radius = mRadius / 2 + mPadding;
float x = cx - radius + (mPadding * 2);
float y = cy;
float textWidth = radius - (mPadding * 10);
TextPaint textPaint = new TextPaint();
textPaint.set(this.mTextPaint);
textPaint.setColor(Color.WHITE);
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTStd-Heavy.otf");
Typeface bold = Typeface.create(plain, Typeface.BOLD);
textPaint.setTypeface(bold);
float angle = tmpAngle + sweepAngle / 2;
canvas.save();
canvas.rotate(180+(angle), cx, cy); // +180 for start from right
canvas.drawText(mStr, x, y, textPaint);
canvas.restore();
}
You can do something like this,
TextPaint textPaint = new TextPaint();
// textPaint attributes
CharSequence ellipsizedText = TextUtils.ellipsize("Your text", textPaint, width,
TextUtils.TruncateAt.END);
canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), x0, y0, textPaint);
But in your case you need it after 7 characters, better just check the text length and append ... after the text.
I'm overriding View on Android to do some custom drawing, but the rendered view has not very good quality, especially the text. I have found a couple of solutions, but none worked for me. Attaching the code here. Thank you for any help.
#Override
protected void onDraw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
Point a = new Point(0, 0);
Point b = new Point(width / 2, height);
Point c = new Point(width, 0);
Path trianglePath = new Path();
trianglePath.moveTo(a.x, a.y);
trianglePath.lineTo(b.x, b.y);
trianglePath.lineTo(c.x, c.y);
trianglePath.lineTo(a.x, a.y);
Paint paint = new Paint();
paint.setStrokeWidth(1);
paint.setColor(Color.parseColor("#7A7A7A"));
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
canvas.drawPath(trianglePath, paint);
Paint textPaint = new Paint();
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(height / 6);
textPaint.setTypeface(TypefaceUtils.getTypeface(getContext(), TypefaceUtils.AVAILABLE_FONTS.ROBOTO_THIN));
String receive = getContext().getString(R.string.receive_password).split("\n")[0];
String password = getContext().getString(R.string.receive_password).split("\n")[1];
Rect receiveBounds = new Rect();
Rect passwordBounds = new Rect();
textPaint.getTextBounds(receive, 0, receive.length(), receiveBounds);
textPaint.getTextBounds(password, 0, password.length(), passwordBounds);
canvas.drawText(
receive,
(width - receiveBounds.width()) / 2,
height / 3 - receiveBounds.height() + receiveBounds.height() / 5,
textPaint);
canvas.drawText(
password,
(width - passwordBounds.width()) / 2,
height / 3 + receiveBounds.height(),
textPaint);
canvas.scale(1, 1);
}
Try setting textpaint.setAntiAlias(true);
This question already has answers here:
How to draw text on canvas?
(3 answers)
Closed 8 years ago.
I am writing an application that consists of a gauge view. I downloaded from here and i want to draw text inside the gauge view (that is at the center of the gauge view) how can i draw a text inside a gauge view please help me.
After my friends answers in stack overflow i did it like this but it did not works please make it work
private void drawGauge()
{
if (null != mBackground)
{
mBackground.recycle();
}
/*--For Semi circle put getHeight()/2--*/
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
newBitmap = Bitmap.createBitmap(mBackground.getWidth(), mBackground.getHeight(), Config.ARGB_8888);
Canvas newCanvas = new Canvas(newBitmap);
final Canvas canvas = new Canvas(mBackground);
final float scale = Math.min(getWidth(), getHeight());
canvas.scale(scale, scale);
canvas.translate((scale == getHeight()) ? ((getWidth() - scale) / 2) / scale : 0,(scale == getWidth()) ? ((getHeight() - scale) / 2) / scale : 0);
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.WHITE);
paintText.setTextSize(50);
paintText.setTextAlign(Align.CENTER);
paintText.setStyle(Style.FILL);
Rect rectText = new Rect();
paintText.getTextBounds(captionString, 0, captionString.length(), rectText);
newCanvas.drawText(captionString, 0, rectText.height(), paintText);
//canvas.drawText("MedeQuip", 200, 500, shadowpaint);
//drawRim(canvas);
//drawFace(canvas);
For vipluv:
protected void onDraw(final Canvas canvas) {
drawBackground(canvas);
final float scale = Math.min(getWidth(), getHeight());
canvas.scale(scale, scale);
canvas.translate((scale == getHeight()) ? ((getWidth() - scale) / 2) / scale : 0, (scale == getWidth()) ? ((getHeight() - scale) / 2) / scale : 0);
if (mShowNeedle) {
drawNeedle(canvas);
}
if (mShowText) {
drawText(canvas);
}
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.WHITE);
paintText.setTextSize(150);
paintText.setTextAlign(Align.CENTER);
paintText.setStyle(Style.FILL);
Rect rectText = new Rect();
paintText.getTextBounds(captionString, 0, captionString.length(), rectText);
canvas.drawText(captionString, rectText.width(), rectText.height(), paintText);
computeCurrentValue();
}
In the onDraw() function in their GaugeView.java file, add a canvas.drawText() just before the call to computeCurrentValue().
Use your own Paint object, and make sure you have initialized its textsize and color properties.
I have a small question. I'm painting some RectFs on my canvas, but nothing happened.
I paint one filled RectF and another stroked RectF => filled background with a border.
For the first "field" everything is ok, but not for the second "field" (rect2).
The positions are checked. The last text is printed in that RectF (rect2) where the rect is not painted.
I uploaded an image on my FTP server. For the description of my problem it might be easier to understand.
Here is my code. I think it is a logical error?
// Fill rect
rect.set(fPosition, fPositionY, pts[2] - 10, fRectHeight);
paint.setColor(Color.GREEN);
paint.setStyle(android.graphics.Paint.Style.FILL);
paint.setStrokeWidth(0);
canvas.drawRoundRect(rect, 10, 10, paint);
// Border
paint.setColor(Color.BLACK);
paint.setStyle(android.graphics.Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRoundRect(rect, 10, 10, paint);
// Text
float fTextSize = fRectHeight - 40;
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(fTextSize);
float textHeight = textPaint.descent() - textPaint.ascent();
float textOffset = (textHeight / 2) - textPaint.descent();
canvas.drawText(dataStrings.getGewicht(), rect.centerX(), rect.centerY() + textOffset, textPaint);
// Leistung pro Stunde
rect2 = new RectF();
paint = new android.graphics.Paint();
fPositionY = fPositionY + fRectHeight + 50;
float fRectWidth = pts[2] / 2;
fRectHeight = (float) (pts[17] / 5);
// Fill rect
rect2.set(fPosition, fPositionY, fRectWidth, fRectHeight);
paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
paint.setStrokeWidth(0);
canvas.drawRoundRect(rect2, 10, 10, paint);
// Border
paint.setColor(Color.BLACK);
paint.setStyle(android.graphics.Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRoundRect(rect2, 10, 10, paint);
// Text
fTextSize = fRectHeight - 40;
textPaint = new TextPaint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(fTextSize);
textHeight = textPaint.descent() - textPaint.ascent();
textOffset = (textHeight / 2) - textPaint.descent();
canvas.drawText(dataStrings.getLeistung(), rect2.centerX(), rect2.centerY() + textOffset, textPaint);
What is the best way for draw a number inside circle drawed on canvas by drawCircle?
Then this circle can be dragged by user, when circle is empty I donĀ“t have problems.
This will draw text centered at centerX, centerY
Rect rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);
float x = centerX - rect.width() / 2;
FontMetrics fm = paint.getFontMetrics();
float y= centerY - (fm.descent + fm.ascent) / 2;
canvas.drawText(text, x, y, paint);