Android Canvas.drawCircle in the center of the screen - android

I am using Canvas.drawCircle to draw a circle in Android laout.
Method gets 3 parameters - first two are position - x and y.
Is it possible to skip hardcoded position of the circle and draw it centered ?

Following code can be used to get the width and height of the screen.
int width = this.getWidth();
int height = this.getHeight();
To draw circle in the middle of screen you can call :
Canvas.drawCircle(width/2, height/2)

You can paint a circle centered to the screen like this:
Display disp = ((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
canvas.drawCircle(disp.getWidth()/2, disp.getHeight()/2, radius, paint);

Assuming you are extending the View class:
int CentreX = (this.getWidth() / 2);
int CentreY = (this.getHeight() / 2);

Related

Android: How to draw line with rounded corner?

How can I draw line/arc with rounded corners as you can see on the picture below?
I need to draw this on Canvas.
I think you can workaround this by drawing three lines with a partial overlap:
two external lines with Paint.Cap.ROUND
one inner line with Paint.Cap.BUTT
Assuming your input data is
float lineWidth = 20;
float lineRadius = 100;
float cornerRadius = 2;
You go as follows,
float width, radius;
// Draw outer lines
paint.setStrokeCap(Paint.Cap.ROUND);
width = cornerRadius * 2;
// Draw inner
radius = lineRadius - lineWidth/2f + cornerRadius;
canvas.draw(...)
// Draw outer
radius = lineRadius + lineWidth/2f - cornerRadius;
canvas.draw(...)
// Draw center
paint.setStrokeCap(Paint.Cap.BUTT);
width = lineWidth - 2f*cornerRadius;
radius = lineRadius;
canvas.draw(...)
You might need to slightly alter the arc angle for the center line (it must be cornerRadius longer, on each side) but that is easy.

Using the canvas.drawText() while creating a Custom View

I am creating a Custom View where I have to draw text at specific positions on the canvas. For using canvas.drawText() we have to create a paint object that has to be passed as a parameter while calling the drawText(). If I want to get the text to be rendered as centrally aligned, I am using the setTextAlign() call while making the paint object.
statPaint.setTextAlign(Paint.Align.CENTER);
Also when I call the drawText(), I am required to set the starting point for the text to be rendered, as shown below.
canvas.drawText(stat1,paddingLeft,paddingTop,statPaint);
paddingLeft and paddingTop tell the drawText() where to draw the text from. Passing these two parameters is mandatory for the function call. But I still am not able to get the text centrally aligned. How do I get this text centrally aligned.
Also if I want to get two different texts in the same line, the first one being centrally aligned in the first half of the screen, and the second text, centrally aligned in the second half of the screen, how do I do that? The screen is divided into halves vertically.
Thanks in advance.
Here is how to center text in any rectangle (just change left/right/top/bottom)
// Allocated once as a member var, not in each onDraw
private Rect rect = new Rect();
// Inside onDraw
int left = 0;
int right = getWidth();
int top = 0;
int bottom = getHeight();
paint.getTextBounds(text, 0, text.length(), rect);
FontMetrics fm = paint.getFontMetrics();
float x = left + (right - left - rect.width()) / 2;
float y = top + (bottom - top) / 2 - (fm.descent + fm.ascent) / 2;
canvas.drawText(text, x, y, paint);

Drawing a square in the center of a canvas

I'm trying to figure out how to draw a Square within my onDraw method in Android.
The square must be positioned in the exact center of the canvas
(Not the screen)
The padding/spacing on the left and right hand side of the square should be
equal
The padding/spacing on the top and bottom of the square should be equal
The size of the square should be relatively large, about 90% of the
canvas's width
Here's what I have so far.
//this.rect is an instance of Rect() which later gets called in the canvas.drawRect() method
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
int padding = (width / 10);
this.size = width - padding;
this.rect.set(padding,padding,size,size);
}
The code above draws the square but I'm not sure how to get it to center in the canvas. I am also open to using another technique that does not involve using a Rect.
What properties do I need to set to this Rect() in order for the canvas.drawRect(rect,paint) to draw the rectangle directly in the center of the canvas?
Edit:
Terribly drawn example of what I want to achieve
Assuming width is the width of the canvas, I guess you're missing substracting the padding twice.-
this.size = width - padding * 2;
EDIT
Since we're talking about a rectangle here, you'll need to do some more changes to your code, and calculate different top and left padding .-
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
int paddingLeft = (width / 10);
size = width - paddingLeft * 2;
int paddingTop = (height - size) / 2;
this.rect.set(paddingLeft,paddingTop,size,size);
EDIT 2
Maybe a clearer approach would start calculating the size of your square.-
size = width * 0.9f;
int paddingLeft = (width - size) / 2;
int paddingTop = (height - size) / 2;
this.rect.set(paddingLeft,paddingTop,size,size);

how to get bounds of a scaled view?

It seems that setScaleX or setScaleY don't actually change left,top,right,bottom properties. getX and getY remain unchanged too.
So if I scale a view whats the easiest way to get 4 corner coordinates of the newly scaled view?
I tried getHitRect but that doesn't give me the right answer. I am trying to avoid manually calculating the new bounds based on existing transformations (rotation and scale with pivots factored in).
After exploring the view api, it looks like there is no direct API method that does this.
However you can easily get the new points by grabbing the transform matrix of the view and using that to get the new bounds.
Something like this:
Matrix m = view.getMatrix();
Rect bbox = new Rect();
view.getDrawingRect(bbox);
m.mapRect(bbox);
If you want to operate on (x,y) coordiantes directly there is a matrix.mapPoints that will achieve the same result.
I believe if you get the width and height and multiply it by the scales, you'll get the scaled width and height.
int scaledWidth = getWidth() * getScaleX();
int scaledHeight = getHeight() * getScaleY();
int newLeft = getLeft() + (scaledWidth / 2);
int newRight = newLeft + scaledWidth;
int newTop = getTop() + (scaledHeight / 2);
int newBottom = newTop + scaledHeight;
This is assuming that you scaled with a pivot x and y at the center of the view. Things gets far more complicated if you have pivots in strange areas.

Centering a texture LibGDX

I am trying to center a 256px X 256px image in LibGDX. When i run the code I'm using it renders the image in the upper right hand corner of the window. For the camera's height and width I use Gdx.graphics.getHeight(); and Gdx.graphcis.getWidth(); . I set the cameras position to the camera's width divided by the two and its height divided by two... this should put it in the middle of the screen right? when I draw the texture, I set it's position to the camera's width and height divided by two -- so it's centered..or so I think. Why doesn't the image draw in the center of the screen, is there something I'm not understanding?
Thanks!
It sounds as if your camera is ok.
If you set the textures position, you set the position of the lower left corner of that texture. It is not centered. Therefore if you set it to the coordinates of the center of the screen, its extends will cover the space to the right and the top of that point. To center it, you need to subtract half of the textures width from the x, and half of the textures height from the y coordinate. Something along these lines:
image.setPosition(Gdx.graphics.getWidth()/2 - image.getWidth()/2,
Gdx.graphics.getHeight()/2 - image.getHeight()/2);
You should draw your texture at the camera position - half the dimensions of the texture...
For example:
class PartialGame extends Game {
int w = 0;
int h = 0;
int tw = 0;
int th = 0;
OrthographicCamera camera = null;
Texture texture = null;
SpriteBatch batch = null;
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getheight();
camera = new OrthographicCamera(w, h);
camera.position.set(w / 2, h / 2, 0); // Change the height --> h
camera.update();
texture = new Texture(Gdx.files.internal("data/texture.png"));
tw = texture.getwidth();
th = texture.getHeight();
batch = new SpriteBatch();
}
public void render() {
batch.begin();
batch.draw(texture, camera.position.x - (tw / 2), camera.position.y - (th / 2));
batch.end();
}
}

Categories

Resources