Click Listener on canvas arc - android

I want to draw arc on canvas with progress percentage on circle and need to perform click on both the color (blue and green).
How to make click listener for the same.

You have two circle for grean line
Two circle makes green area(difference of inner and outer circle) say circles G1(inner), G2(outer)
first find circles sector area of G1 and G2 say GS1 and GS2.
Now find touch point form on touch event say p1. If p1 lie in GS2 but not in GS1 than you have touched on green line
Similarly you have two circle for Blue line and you can find touch point
See this link also - Efficiently find points inside a circle sector

Related

How to select one shape from many shapes in canvas and move that particular shape in android canvas?

In the android canvas, I have 5 shapes. In my case, I need to select one of them and move to new position. The same time need to find which one select by touch.
If You have drawn shapes by x, y coordinate and by width height then implement OnTouchListener interface and take touch of whole screen. Take touch coordinate x and y and match on onTouch implement method in which shape coordinate are touched.

Android : Move arrow's end point and start point on user's touch event

I have Relative layout with background #mipmap/arrow. I am adding this relative layout (On user touch) in another relative layout which is having ImageView (using imageRelativeLayout.addView(arrowRelativeLayout)).
Goal :
1) I want to add arrow on ImageView after onTouchEvent() to particular (x,y) position. (This is working perfectly)
2) After adding arrow, I want to move arrow's end Point or start Point as per user's touch
3) when user touch on arrow's end point then it should move to respective (x,y) position but start point should be fixed
4)when user touch on arrow's start point then it should move to resp. (x,y) position but end point should be fixed
Arrow Head is Start Point & Arrow tail is End Point
Please Help me
Thanks in advance
So add some invisible views and anchor them to the start end location of the arrows, when a user presses this move them to the new location and then update the image view.
It depends on your graphics if the arrow is edge to edge or corner to corner within the rect, i would suggest corner to corner then you just have to set the layout parameters with the correct top/left and bottom/right also Rotate / Flip it depending on your start end points, but you only need to rotate by 90,180,270 degrees.
This method will mean that your arrow head will be squashed or stretched, if you want you can include a seperate arrow head which uses the angle generated and rotates to this angle, anchoring with the end of the arrow line that you want.

How to detect shape collision - Android

I would like to detect collisions between shapes dynamically drawn on a canvas (SurfaceView) for an Android game.
I can easily use intersect method of Rect or RectF objects but the result is not very good (see picture below where I have a "false" detection).
I don't want to use Bitmap so it's impossible to use the "pixel perfect" method.
Do you know a way to do this for circle, rect, triangle and other basic shapes intersection ?
Thx for help ;)
For a good collision detection you have to create your own models behind. In those models you specify the conditions that two objects colide.
For example, a circle is described by the center position and by the radius. A square is described by the left down corner and by the edge length.
You don' t have to describe all possible poligons, you can use the so called bounding boxes, meaning that, for a complex random poligon you can use a square or whathever shape fits it best(also you can use multiple shapes for a single object).
After you have the objects in mind you compute the condition that each one of them will colide with all other shapes including itself.
In your example The sphere and the square colides if the distance between any corner of the square is greater than the circle's radius.
Here you can read more http://devmag.org.za/2009/04/13/basic-collision-detection-in-2d-part-1/
This problem can get very complex, keep it simple if you want something simple.
Here is a directly applicable method I use in my own game to detect circle and rectangle intersection. It takes the ball (which is a view in this case) and the rectangle (also a view) to be checked for collision with the ball as parameters. You can put the method in a Timer and set the interval you want the circle and rectangle to be checked for collision.
Here is the method:
public boolean intersects(BallView ball, Rectangle rect) {
boolean intersects = false;
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR() <= rect.getTheRight() &&
ball.getY() + ball.getR() <= rect.getTheBottom() &&
ball.getY() - ball.getR() >= rect.getTheTop())
{
intersects = true;
}
return intersects;
}
getR() gets the circle's radius
getX() gets the center of the circle's X position value
getTheLeft() gets the rectangle's left X value
getTheRight() gets the rectangle's right X value
getTheTop() gets the rectangle's top Y value
getTheBottom() gets the rectangle's bottom Y value
If you can't directly use this method in your code you can still conjecture the logic it entails to implement it where it would work for you. It detects all collisions without using pseudo-collision detection like a collision box for the circle.
Good luck! And if you have any questions feel free to ask, I'm here to help!
To know if a polygon in 2d is colliding with a circle, you can test, for each of its lines, where is the point on the line that is closest to the center of the circle (this might help).
Then, check if the point you found is between to two corners that make the line - that is, that the point is actually on the line, and not just on its continuation - and if the distance of that point to the center of the circle is smaller or equal to the radius of the circle. If both are true for any of the lines of the polygon, you have a collusion. You also have to check for the edge cases where the corners of the polygon might be in, or touching the circle.
For two circles, this is easier. Check the distance between the centers, and compare it to the sum of their radiuses. If the distance is smaller or equal to the sum, you have a collusion.

How I can make a custom clickeable area?

how I can make a custom clickeable area/button? For example, If I have a semicircle as the image below, how I can make the whole area red, blue or gray are clickable and independent?
Thank you for your advices.
you can use this example :
https://mega.co.nz/#%21lMUHQTAQ%21Tr3i-3iYnsNt8-2DshNbwBAObJkZFU7NJGYMu5RBxGw
Or :
https://mega.co.nz/#!0M9HUTKZ!cMe8iDz4BnV5aPc92eIRTG8JHqMHSntSUi_0YelfU7M
As already mentioned – make the whole view as one, and calculate in the onTouchlistener().
I would transform the touch coordinates in polar coordinate system (polar angle, radial distance).
Then just check if the radial distance is between inner circle and outer circle radius. If it is, then some button was clicked. You can determine which button was clicked from polar angle (e.g. 0-120 degrees = red button, 120-240 degrees = gray button, 240-360= blue button).

Glow Effect on a Bitmap set as Background of a custom view in android

I have created a Class extending View class. I have set the background in XML which is being called as setBackgroundDrawable(getBackground()) in my onDraw() method. This background in 3 concentric circles.
I have the radius for each concentric circle and i am drawing a small orange colored touchCircle when user touches in any of the ring using canvas.drawCircle(touchX, touchY, touchRadius, mTouchPaint); .
My question is i want to show a glow effect on both sides of the touch Circle within the ring(on which onTouch operation is performed). That means, suppose if the user touches in 3rd ring, a glow effect will be produced of some color around the touchCircle. It will travel from the touch circle to some distance(on both sides of touchCircle) within the ring and then fades out.
I have calculated the angle substended by the touch cordinate with respect to centre(0-360 degrees). My thinking is i will increase/decrease the angle (for left and right glow). But i am confused as in how the glow effect will be produced in the background image.
Can anyone please guide how to proceed further on this .

Categories

Resources