RectF is not painted on canvas - android

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);

Related

Android draw rect round with drawPath and drawRoundRect result in different result

When I draw path in android, the line seems too thin. So I made a experiment:
draw a round rect using drawPath and using drawRoundRect.
here is my code:
//setPaint
mStrokeWidth = 1;
mPaint.setStrokeWidth(mStrokeWidth);
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
//draw using drawRoundRect
mStrokeWidth = 1;
#Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
mRect.set(bounds.left + mStrokeWidth / 2,
bounds.top + mStrokeWidth / 2,
bounds.right - mStrokeWidth / 2,
bounds.bottom - mStrokeWidth / 2
);
canvas.drawRoundRect(mRect, 10, 10, mPaint);
}
//draw using drawPath
mStrokeWidth = 1;
#Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
mRect.set(bounds.left + mStrokeWidth / 2,
bounds.top + mStrokeWidth / 2,
bounds.right - mStrokeWidth / 2,
bounds.bottom - mStrokeWidth / 2
);
Path path = new Path();
path.addRoundRect(mRect, 10, 10, Path.Direction.CCW);
canvas.drawPath(path, mPaint);
}
here is the result:
the left is using drawRoundRect and the right using drawPath
you can see that the left is thicker than the right. I want to know what caused the phenomenon?

Android canvas draw

How to draw on canvas two texts with relative positioning?
Example:
User: John Doe
Status: foreigner
Code used produce text "one on top of another" issue:
Canvas canvas = page.getCanvas();
int titleBaseLine = 72;
int leftMargin = 54;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText(user, leftMargin, titleBaseLine, paint);
canvas.drawText(userName, leftMargin, titleBaseLine, paint);
Final answer:
int titleBaseLine = 60;
int leftMargin = 20;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(40);
Paint paintBold = new Paint();
paintBold.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
paintBold.setColor(Color.BLACK);
paintBold.setTextSize(40);
canvas.drawText(userText, leftMargin + leftMargin, titleBaseLine, paintBold);
canvas.drawText(username, leftMargin + paintBold.measureText(userText) + leftMargin + 10, titleBaseLine, paint);
canvas.drawText(statusText, leftMargin + leftMargin, titleBaseLine * 2, paintBold);
canvas.drawText(statusName, leftMargin + paintBold.measureText(statusText) + leftMargin + 10, titleBaseLine * 2, paint);

Android Wear(Long Textview)

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

Android Custom View drawing got bad quality

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);

Filling intersection area of two circles in Android

My application includes a calculatePosition(), which draws four circles:
private void calculatePosition(){
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
float ratio = (float) (640/3.45);
float radiusB1 = Calculations.convertscale(scale, position[0] * ratio);
float radiusB2 = Calculations.convertscale(scale, position[1] * ratio);
float radiusB3 = Calculations.convertscale(scale, position[2] * ratio);
float radiusB4 = Calculations.convertscale(scale, position[3] * ratio);
canvas.drawCircle(broadCaster1x, broadCaster1y, radiusB1, paint);
canvas.drawCircle(broadCaster2x, broadCaster2y, radiusB2, paint);
canvas.drawCircle(broadCaster3x, broadCaster3y, radiusB3, paint);
canvas.drawCircle(broadCaster4x, broadCaster4y, radiusB4, paint);
}
I want to know whether it is possible in Android to fill intersecting areas between two circles as shown in the figure below. If yes, is there any basic example of how to obtain it ?.
Use 'Canvas.clipPath'
For Example:
Path pathA = new Path();
pathA.addCircle(xCenterA, yCenterA, radiusA, Direction.CW);
Path pathB = new Path();
pathB.addCircle(xCenterB, yCenterB, radiusB, Direction.CW);
canvas.clipPath(pathA);
canvas.clipPath(pathB, Region.Op.DIFFERENCE);

Categories

Resources