Android Drawing a Rectangle right to left (rtl) - android

I'm building a drawing app in android and I noticed a problem while drawing a rectangle. the rectangle should be drawn according to the syntax
new Rect(left, top, right, bottom)
now my logic works by taking the start point (startX, startY) and end point (endX, endY) and I draw using:
new Rect(startX,startY, endX, endY);
assuming the user is drawing from the top left corner to the bottom right.
The Problem
now the problems happens when the user tries to draw the rectangle form right to left or from bottom to top. working on the first problem (right to left):
it means the startX is now pointing to the right (rather than left). so the code will be trying to draw with this syntax now:
new Rect(right, top, left, bottom)
Which will fail to draw the rectangle.
In other words, startX is larger than endX which shouldn't be while drawing the rectangle.
Resolution
so I used the following code to check before drawing the rectangle:
int smallX,largeX,smallY,largeY;
if(startX>endX){ smallX=endX; largeX=startX; }else{ smallX=startX; largeX=endX; }
if(startY>endY){ smallY=endY; largeY=startY; }else{ smallY=startY; largeY=endY; }
rect = new Rect(smallX, smallY,largeX,largeY);
And that will ensure than the first parameter will always point to the left and the others are good as well.
My question here... Is there another way around to fix this problem?

I don't think so, because Canvas is settted by coordinates and the coordinate (0,0) is top left and grow to bottom right, and the renderer follow that logic, start top left an go line after line to the bottom.

Related

Android canvas Drawable position translation

I Created my custom view in which i placed one drawable, I need to change its position. I have 2 Rectangles(Rect) which have the top, bottom left, right. Now I need to give translation effect by Changing Rect.
Lets suppose Rect1 have top = 100 & Rect2 have top = 200. So I need to do translation from 100 to 200 with anim duration.
Any help is appreciated.

Open PopupWindow and drawing from RightTop to LeftBottom, instead of LeftTop to RightBottom

I know I can use:
myPopup.showAtLocation(layout, Gravity.NO_GRAVITY, x, y);
// OR
myPopup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, x, y);
To open a PopupWindow that's drawn from [x, y] as the Top-Left of the PopupWindow, drawn towards to Bottom-Right.
What I want instead however, is to draw from [x, y] as the Top-Right of the PopupWindow, drawn towards the Bottom-Left.
Here is a picture to make it more clear (The dot is my [x, y] position and the rectangle is my PopupWindow. The first picture shows how it's normally done, and the second is what I want to achieve.):
So how do I correctly calculate the x and y of the second picture's gray point's location, while knowing the black point's location? I know the y can stay the same, but the x should be changed to something like:
x minus PopupWindow-width
The problem is that the PopupWindow's width and height are both set to wrap_content, so I don't know the size until after I draw it.
Does this mean I have to draw it (but make it invisible at first), then calculate the new x with the PopupWindow's MeasuredWidth in it's ViewTreeObserver's OnGlobalLayoutListener (to know when it's done rendering and the MeasuredWidth is known), then apply this new x and then make it Visible? Or is there an easier way to just let it draw at the correct position?
PS: I've also changed Gravity.NO_GRAVITY to Gravity.TOP|Gravity.RIGHT, if the PopupWindow is out of the screen it will automatically place it at the border of the Right/Top side (whichever side it's out of the screen).
You could get the size of your popup window by overriding the onMeasure method of the popup window (note that you have to subclass a View in order to do this). After that, you can calculate the offset of the x and y coordinates. Hope this helps.

Path . lineTo draws only half of length

Can somebody help me understand how does path.lineTo(x,y) actually work. I am trying to draw a simple straight line in a finger paint type of app. Here is an example I tried:
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(480, 800);
But the line goes only from 0,0 to 240, 400 - always only a half of distance as I move my finger over the screen. (the real programme has startX, startY and x,y touch coordinates instead of 0,0 & 480,800)
Maybe this piece of code will work for you:
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
Path mPath= new Path();
mPath.moveTo(0, 0);
mPath.lineTo(480, 800);
p.setColor(0xff800000);
canvas.drawPath(mPath,p);
Also, the (480, 800) coordinate, is it still inside your screen?
The Path commands work exactly how you expect them to work and the above command should draw a line from top left to bottom righ corners in a view fully covering a 480x800 screen .
Check the follwoing:
The view covers all screen
The is no other view overlaping it
The coordinates are relative to top left coordinate of view. Check that view top left is on top left of screen
Regards.
Thanks to Goz and others who helped me truobleshoot this, it came out that Paint was doing it with this settings:
mPaint.setPathEffect(new CornerPathEffect(1000))
When this is removed or set to null, lineTo goes all the way. One just did not expect at first that Paint would do this but this corner rounding effect can shorten a straight line.

How to center an shape created through canvas?

I am using android java to create an application that uses the accelerometer, i have created a shape that does kinda move with the accelerometer but it stays in the top left hand corner and moves there, can anyone help me with how can i move it to the center of the screen and set bounds
I am assuming you are using a canvas to draw that stuff if so canvas has a method called getwidth() and also getheight()
what you can do is when you are drawing it to the canvas set the location point x and y coordinates as
canvas.getWidth()/2
and canvas.getHeight()/2
something like this
canvas.drawBitmap(bitmap, canvas.getWidth()/2, canvas.getHeight()/2, null);
I hope you got the idea.
canvas.drawRect(left, top, right, bottom, paint)
use this function if you wanna draw a rectangle
Parameters:
left The left side of the rectangle to be drawn
top The top side of the rectangle to be drawn
right The right side of the rectangle to be drawn
bottom The bottom side of the rectangle to be drawn
paint The paint used to draw the rect
Use the same logic in this method and you are golden ;)

Android screen coordinate system : how to set (0,0) at the bottom left side of android screen as first quadrent of xy plane

how to set the screen coordinate system of android screen as first Quadrant of the XY plane
,iwant the (0,0) position to be at bottom left , and i wanna know if i can use the trignometric equation on android screen as Android XY plane is not like the Xy plane
I don't think there's a way to do it that would affect the entire system, such as the XML layout files. But if you just want to draw with a Canvas, you can use translate() and scale().
First use translate() to slide the canvas down so 0,0 is at the bottom. Now the top of the screen would be a negative number, so call scale() to flip it around. Now 0,0 is still at the bottom, and the top of the screen is a positive number.
I'm working with information from this answer and its comments. Use something like:
canvas.save(); // need to restore after drawing
canvas.translate(0, canvas.getHeight()); // reset where 0,0 is located
canvas.scale(1, -1); // invert
... // draw to canvas here
canvas.restore(); // restore to normal
And yes, you can use normal 2D trigonometric functions with the XY coords. You can do it even if they're not translated, you just have to think it through more carefully.
I don't know that you're going to have much luck changing where (0,0) is located, but you could set a constant that accounts for such. myY = y minus screenHeight so (x, myY) adjusts y to the bottom of the screen and works from there +/-.
look up canvas.scale(xs,ys,xp,yp)
xp and yp are the new coordinates that you set for your (0,0) point.

Categories

Resources