I have a RelativeLayout with an ImageView and 8 TextView's in it. I want to position labels around this image dynamically. I thought the best approach would be to rotate the image at 45 degree intervals and place a label at each position, however the Image/RelativeLayout doesn't redraw (even if invalidate is called) until the next predetermind onDraw call, and all the TextViews get put in the message queue for positioning after that, so all the TextViews get positioned ontop of each other at the 315 degree mark.
I tried posting a Runnable to the RelativeLayout in a hope it would be added to the message queue after the onDraw call but unfortunately it didn't work and I'm running out of ideas.
My current code is bellow to give an idea of what I'm trying to do:
public void setupMenuText() {
final float scale = getResources().getDisplayMetrics().density;
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos0), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos1), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos2), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos3), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos4), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos5), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos6), (72*scale)+0.5f);
setupMenuItemText((TextView)findViewById(R.id.menu_text_pos7), (72*scale)+0.5f);
}
public void setupMenuItemText(TextView pT, float pY) {
relativeLayout.setRotation(relativeLayout.getRotation()+45);
pT.setText("Some Text");
pT.setRotation(0);
pT.setGravity(Gravity.CENTER_HORIZONTAL);
pT.setX(0);
pT.setY(pY);
}
EDIT: Thanks to JSchools for the answer, wasn't exactly what I needed but pointed me in the right direction and info was accurate.
For anyone else with the same problem, centering the text inside the TextView wont cut it. I manually had to set the size of the TextView to much higher than its intended use so that it would accurately be positioned relative to the image.
You need to set the rotation of each individual TextView. You seem to be rotating the container multiple times, but since all the TextViews are inside the container, and they all have zero rotation relative to the container, they seem stacked. Don't rotate the container at all. You will also need to set the position of the TextViews so that they will be arranged in a circle. Simply rotating them will stick them on top of each other.
Related
Trying to rotate my LinearLayout for about 270deg, but view get's cut off.
layout.rotation = 270f
Also tried to set layerType but doesn't work also
layout.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
Did i miss something?
How do i can rotate it properly?
I have a layout that looks something like this
Above is a setup which contains a Frame layout which contains a View and an Image View inside of it. The View contains a drawing which is a circle.
I will have the access to the value/size of the radius of the circle at runtime.
I am trying to show an Image using an ImageView inside that circle, but I do not want the Image to cross the circle. I will change the position of the ImageView every second.
Can anyone help me to achieve the logic on how to place the Image inside the circle so that it does not cross that.
With the help of #Selvin I solved the issue.
This is how I solved it
int x=XPOSITION;
int y=YPOSITION;
int r=RADIUS;
int r2=0;
int xy2=0;
Now this is how I am check if the point is inside the circle or not.
Formula = r^2>(x^2+y^2)
r2=r*r;
xy2=(x*x)+(y*y);
if(r2>xy2)
{
Point is inside the circle
}
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.
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?
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.