Putting an angle on image view - java

I'd like to set an angle on an image view and have that angle be generated randomly, and set the ancho point of the image view to the center of the android screen. After that I'd like the computer to generate a spot from a certain distance of the middle to the end of the screen on that angle and set a button to appear there. I'm not sure if eclipse has a quick automatic way to do that.
Thanks.
I've added a picture to help. I'd like the arrow to point to a random angle and then a button to appear on that angle but outside of the circle (the circle is imaginary just showing that it needs to appear outside of a certain distance from the center.

You can use setRotation method for rotate a view in android.
image.setRotation(90); // instead of 90 you can give your generated value.
// But make sure the value should be float.
image.setRotationX(90); // if you want rotate depends x axis
image.setRotationY(90); // if you want rotate depends y axis
also take a look on here
I hope this will help you.

Related

How to start an Activity in specific coordinates

My Activity has a Canvas with both width and height equal to 6000. When I start the Activity the upper left coordinate of my screen is (0,0) so screen' center is about (30,60). What I want is start the Activity with coordinates (3000,3000) in the center of screen
any solution?
Update 1:
I used this Kotlin code:
var fondo = Lienzo(this) //fondo is the Canvas View (6000,6000)
val scrollV = ScrollView(this)
val hscrollV = HorizontalScrollView(this)
scrollV.addView(fondo)
hscrollV.addView(scrollV)
layaout1.addView(hscrollV) //layaout1 is a RelativeLayout
More code would be nice since I cannot see where you are declaring the position but if you are hard-coding the coordinates, I would suggest instead of using the same variables for width and height and just putting them in the coordinates /2 so if your variables are width and height, it should be kind of like this depending on what you are using:
.setPosition(width/2, height/2);
If this isn't useful then please provide more info or more code to see where your mistake is :)
That's not how Canvases work in Android. You don't declare how big you want it to be and it magically scales to the screen. If you're in the View's onDraw function, the Canvas passed in is the size of the View, in physical pixels. You need to scale your drawing to it. If you're drawing to an offscreen bitmap first and then blitting that to the screen, its your job to scale the bitmap via a matrix when you blit it. Also, in Android the coordinate system is 0,0 as the upper left hand corner. Unless it greatly eases your drawing I suggest you not fight it.
However you could use a matrix transformation on the Canvas to change that. First you'd want to scale the matrix by 6000/view.getHeight() in the y and 6000/view.getWidth() in the x. Then you'd want to translate it by -3000 in the x and -3000 in the y. That should scale it to the view and move origin to center.

How to create a counter that updates via a mouse drag?

Lets say I have a circle, and if the user drags his mouse clockwise along the path of the circle the counter increases, and if he drags the mouse counter-clockwise it decreases. What is the best way to implement this in Java? I imagine trig will be needed so the program knows when the user is dragging clockwise or counter-clockwise, right? Not looking for code examples, just help with theory so I can begin with the right approach.
As you probably have access to the circle, get its center point coordinates.
Then get the coordinates of the mouse. After that you compute the angle between the vector and the x-axis.
You do so by first setting the circle point as the center of the imaginary coordinate system and then shifting the mouse coordinates to this system. After that you apply atan2 on the new mouse coordinates. The result is the desired angle.
Point center = ...;
Point mouse = ...;
Point shiftedMouse = new Point(mouse.x - center.x, mouse.y - center.y);
double angle = Math.atan2(shiftedMouse.y, shiftedMouse.x);
At this point you probably need to convert the result of angle to degrees or something like that, if you like. You may take a look at Wikipedia#atan2 for this.
Of course you can also leave it in the format (-pi, pi] and work with that, if you know what it means.
Now you track how this angle changes. If it increases, then the mouse is moving counter-clockwise; if it decreases, then clockwise and so on (or maybe the other way around, just try it). Take care of the bound where after 359° 0° and then 1° comes.

Tracing a position in a circle if the view is a square

So, I am making a cutom PieChartView and now I want to hilight the piece of the chart I clicked on, however I don't know how to detect which piece of the pie chart I clicked on since the whole View is actually a square. Could anyone point me in the right direction?
Use atan2 to convert the vector with respect to the center of the chart into an angle. Turning that angle into a piece of the pie should be simple, but depends on how you represent your pie.

Absolute Coordinates for my Graphic Component

I am doing a project for a contest and in android for a tablet. By wanting to make the user interface more friendly I am using some meters to display speed, torque, temperature and current in a nice way.
So...I inserted the imageview's coresponding to the meters and now it's time to set that pins ( the place from where the indicator rises (it's a line on a circle) ).
I have put my meters images in relative layouts and set parameters for everyone relative to screen resolution.
My problem now is that I want to center a image of that pin inside the meter.
I tryed almost everything I can find to get the ablsolute coordinate x and y but no succes. I need something that can give me the exact location of the meter meening X and Y coordinates. To be more specific and I really hope I make myself undearstood I give the next example:
I have a screen...lets say 600x600 and I pinned a imageview in the center.
Now...I want to get the real coordinates for the imageview on the surface..so I want to get X = 300 - imageWidth/2, Y = 300 - ImageHeight/2 or something like that. So...When I put in a random position from xml layout using just the margin left and margin top parameters I didnt used hardcoded coordinates but now I need to get fixed coordinates beacouse I want to center anoter image inside the parent image.
Or another example: basicly I want to center a image inside a image and I think to do that I need fixed coordinates of the parent's image.
Thank you for your time, any critcs are much apreciated.
I just found a solution. It's not really my favorite but if I put my image inside a RelativeLayout and the other image centered in this RelativeLayout..it works like a charm.2 . Anyone has a better ideea?

Why ImageView's getX, getY, setX and setY do not represent the actual value in the Relative Layout?

I am trying to make a simple app just to understand better how to use Touch events on Android. Right now, my Activity just moves an ImageView to the coordinates of the touch on the screen (the MotionEvent's coordinates). I could manage to do this by applying a simple trick to set the new position of the image to a value that is made of the touch coordinates relative to the position of the image (getX() and getY()). The dx and dy variables are responsible to store this relative values so the touch keeps constant within the image.
Now I want to put 2 ImageViews in the Activity and have only two fixed spots for ImageViews and once the user drags one ImageView over the other ImageView's center, the two images switch places, but to do that correctly I would like to get the real coordinates of the image center. I commented the parts of the code that make it work correctly to show the issue. As you can see in the first picture when the view is created, the Image Coordinates are simply (0, 0), no matter where it is (as long as the initial position is defined as a layout parameter like CENTER_IN_PARENT).
As you can see in the second picture and as far as I observed ImageView's getX() and getY() return the coordinate as if the top-left corner of the image was in the top-left corner of the DISPLAY when the image is located at the top-left corner of the ACTIVITY WINDOW.
I would like to understand why that happens and if anyone knows how to actually get the coordinates of ImageView that can be directly compared to the coordinates of MotionEvent.
Pictures:
FIRST PICTURE
SECOND PICTURE
I still don't know exactly why getX(), getY(), setX(), and setY() do not represent the position of the View on the screen, but I figured out how to get the actual location of the view objects on the screen: getLocationOnScreen(coordinates). Where coordinates is a 2 position int array (int[2]).
For example, to get the real value correspondent to the top-left corner of my ImageView image I simply need to do something like:
int[] img_coordinates = new int[2];
ImageView image = (ImageView) findViewById(R.id.myImage);
image.getLocationOnScreen(img_coordinates);
And then I can actually compare these coordinates with the Touch-event's getRawX and getRawY.
If I want to get the center point of my view I just need to do this:
x_center = (double)img_coordinates[0] + image.getWidth()/2.0;
y_center = (double)img_coordinates[1] + image.getHeight()/2.0;
I hope this helps other people too, it took me a while to find this approach and I am actually not sure if it is the best approach but it does the trick for me.

Categories

Resources