Draw a line with curved edges in Android - java

I am using canvas.drawLine to draw some line in android but the lines are too sharp but i need a curved edges
Here the 1 is what i have and 2 is what i want to achieve, means a line with curved edges rather than straight edges
How can I achieve that ??
EDIT 2:
I am trying to use the Canvas object to to draw a line. but the lines have a sharp edge, I need a rounded off edge
I am using the Paint object
mPaint = new Paint();
mPaint.setColor(Color.BLACK)
Any help would be appreciated great.

Use the Paint.setStrokeCap() method. You need Paint.Cap.ROUND. The default one is Paint.Cap.BUTT. There is a similar Path property that is called path join. It determines how to draw the parts of the path where it's constituent segments join. To set it use Path.setPathJoin(). You might need it in the future.
Good luck.

You can use below
pitchPaint.setStrokeCap(Paint.Cap.ROUND)

Related

opencv java - detect all the rectangles and fill them with an image

I am currently working on a solution for an automatic image edit.
And I have used Canny Edge Detection and Closing.
But What I ultimately want to accomplish is to find all the rectangles from blueprint and fill them with an image what I have.
I want to know the process that I need to take, not the exact code or solution!
Please suggest me what steps should I take to accomplish it, thx.
Image that has the rectangles
Image that needs to go into all the rectangles
what i have done so far
2018-02-12 EDITTED(clean rectangle detected)
// I have done finding the rectangles and drawing lines over them, but the result is not really reliable than I expected(it draws line on rectangles those are not a parking space), and I do not know how to put an image on those rectangle instead of drawing line on them. please help me out!
P.S : Only in JAVA please !
In your case, you don't need Canny. Your image is really clean and edges are really visible already. A simple Threshold, will work better.
For rectangle detection take a look at this example (included with opencv) that uses findcontours and checks the angles: https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp
In your case, you can skip the Canny step because you don't have gradients. You may need to modify the filtering, like side dimensions for your case.
Once the rectangles look good, you just need to copy your image in the location. If rectangles are rotated, you will have to rotate your image as well.
EDIT:
To copy the small image onto the big image you can do the following
Mat submatImg = bigImage.submat(new Rect(x, y, smallImage.width(), smallImage.height());
smallImage.copyTo(submatImg);
If you need to do resizing and rotations, take a look at geometric transformations

How to Draw a continuous line on touch path in libgdx?

I tried using pixmap , but every time i draw a path and increaase drag speed , it shows space betweeen two pixmap circles.
I also tried by saving touch points in an array and drawind all the points by shaperenderer but same thing happened.
I need to draw a smooth line like in some image editor apps.
Right now using this method.
while (cIter.hasNext()) {
penpoint = cIter.next();
shapeRenderer.circle(penpoint.getPosition().x,
penpoint.getPosition().y, 3);
}
but it shows like this.
https://s3.amazonaws.com/pushbullet-uploads/ujCRdpmszeu-lp4uLBpI5MftwHBvmk58QP4w4mrzfkJx/Screenshot_2014-12-14-15-23-41.png
do not know if this is what you want, but maybe you can use it for whatever you want to do, hope it helps.
https://github.com/libgdx/libgdx/wiki/Path-interface-%26-Splines
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/PathTest.java
P.S:maybe you can look on Beizer curves
https://www.google.es/?gws_rd=ssl#q=http://devmag.org.za+/2011/04/05+/bzier-curves-a-tutorial/

How to draw a rotated rectangle

Can anyone share a code how to draw a rectangle like this?
(not vertical, not horizontal, somewhere between)
Because as I see you can only specify 4 ints to function DrawRect(), not 4 points.
The area of rectangle must be sensitive to touch (I use Contains() method)
As I tried, Matrix and Rotate() update only graphics, the rect's area remains the same it was
After transforming as #fortran suggested, you can use one of the Matrix.mapPoints overloads to find out what the new corners of your rectangle are. You'll probably have to find some fancy math and do hit testing yourself. Might be easier to call Matrix.mapPoints() on the inverse of the transformation used to draw the rectangle, passing the touched coordinates, and then hit test on the original rectangle.
Push a rotation transformation, draw the rectangle, pop it.
http://developer.android.com/reference/android/graphics/Canvas.html#rotate%28float,%20float,%20float%29
I found solution - the easiest way is to use Path class, make a free-turned rectangle by points and then create a region method, which has function Contains()
No need for math and hard work, pretty and easy.

How to change the position of a view?

I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).
Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.

Drawing a line connecting two rectangles

I am making my own class diagram app in Swing/AWT but i stopped at this functionality:
I want to draw a line between the Class rectangle that already selected and to the target Class rectangle, but line has a feature which is whenever i move one of the rectangles the line that join them get bend in a straight fashion following the moving rectangle , i hope the following picture demonstrate what i want to achieve:
A general guideline or a sample code is highly appreciated
I don't know Java, but the steps you could follow are these:
Find the middle of each line of the rectangles (should be easy, just avarage x1+x2 and y1+y2)
Determine the edges that are closest to each other using Pythagoras formula on the points you got in the previous step.
Start drawing a line starting at xa,ya (first point you got in the step above), and draw it in the direction away from the rectangle. You should know this direction, because you can know the line segment this point is on.
Do the same for xb,yb (point on the second rectangle). If the lines are in opposite directions, you should draw them to halfway xa-xb or ya-yb (depending on if you're drawing horizontally or vertically). If they are perpendicular (is that the right word?) you should draw them to the point where they cross, so you draw the line from xa,ya to xa,yb or xa,ya to xb, ya, depending if you draw the horizontal or vertical line.
There should be some additional check to see if the rectangles overlap. You should not draw lines in the same direction for example. Maybe it would suffice for you to draw just a diagonal line between the two points in those cases where you cannot determine how to draw these straight lines.
For the implementation you could build a line class that uses the observer pattern to listen to the two rectangles it follows, so it can update itself whenever one of them moves or resizes.
http://java-sl.com/connector.html
Hope this helps.
Try with observer pattern. All lines that are connected with a moving object should be notified with new position of the object and 'bent' properly. Of course, first implement some logic that will connect 2 objects.
try creating a class named "ConnectingLine" or similar. this class will then have several segments (that's the name of these line parts in dia, which is currently my favorite uml modeling tool) which will be calculated one by one. you'll have a sepaparate class for this of course ;) called maybe "LineSegment". i think this should make it easier for you to perform the mathematical calculations required to perform this task.
this could also enable making segments "auto routed or not" easy d(^_^)b

Categories

Resources