I have an android View X and Y where X has to be manipulated based on the dimensions of Y (specifically, X and all its contents must be scaled up to Y without its aspect ratio being changed). I do this by working out the new (larger) size of X and setting scaleX and scaleY accordingly. Please tell me if there is a better way! If not, my second problem is - I would like this to be done in Activity.onCreate or onStart or onResume. But in these the dimensions of X and Y are reported as 0.0. Is there any way I can do this on activity start after Y is laid out so its dimensions are not 0.0.
Regards,
Steve Kucera
Any view in android goes through several steps before it appears on screen. Firat of all it goes to measure() - here will be field measuredHeight and measuredWidth. Then it goes to layout() and only after this method finish view has params width and height. So, you can provide your own view, that will contains both X and Y, and there, while measure and layout, change width or height of child X.
Related
I hava a imageview. I put on specific position(specific x and y) on my constraint layout. I want to move(fly with animation) this imageview from random x y positions(for example out of screen) to specific positions that i put before on constraint layout.
I have tried translate animation. it didn't work.
{imageView4.animate().yBy(-200).y(imageView4.getY()).xBy(-200).x(imageView4.getX()).setDuration(4000);}
Method yBy(value) allow you to move element to value relatively from current coordinate. Method y(value) allow you to move element to abslute value coordinate. So you need write this code:
imageView4.setX(startValueX);
imageView4.setY(startValueY);
imageView4.animate().x(endValueX).y(endValueY).setDuration(4000);
Notice that values given in pixels. If you need dp you have to convert px in dp.
I have a smartphone of 2560 x 1440 px. Now I am using this function for my TextView:
int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);
It is supposed to give me the total x and y coordinates in pixels.
My TextView is pretty much in the middle of the screen, so supposedly at
(1280,770). But the function returns [69, 1111].
How can that be? If that is not the way of doing that, then what is?
I have a smartphone of 2560 x 1440 px... My TextView is pretty much in the middle of the screen, so supposedly at (1280,770).
Not correct, unless you talk about the center of the TextView. Any view is rendered on screen within its rectangle, so its coordinates are considered to be of [left, top, right, bottom].
The GetLocationInWindow() method returns (left, top) coordinates of a view. So (x = 69, y = 1111) looks to be meaningful for the left top corner of your TextView located in the middle of screen.
Important note: GetLocationInWindow() returns the coordinates w.r.t the root view, not actual window. You should take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.
I have the next question:
Im developing an app that when i move a imageView and drop it, if the view drops under the half height of the screen goes to a (X,Y) position and if is over the half height screen, goes to another position.
I need to calculate the half of the screen generic, so i use the next code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
halfHeight = displaymetrics.heightPixels / 2;
This works great, im trying in a screen 1920x1080, the code returns 540.
But when im going to see if when i drop the view is under or over the half, here is what i dont understand. i get the Y position of the view and is ok, what i dont understand is why the Y = 0 is not on the TOP of the screen, if i move the view to the top, i get a negative Y position like -260.
Someone can explain me why this happen?
Is there i way that the (0,0) position starts in the top left of the screen?
Greets, hope you understand
If you call getX() or getY() then you are getting relative values for x and y (relative to the view that the call was dispatched from). If you call getRawX() or getRawY() it will give you the absolute position of the view relative to the device's screen.
Most likely you are getting negative values of -260 because you are dragging the ImageView 260 away from the view in which the call was made (perhaps the view on the top of the screen has a height of 260). If you are trying to use getX() or getY() to calculate the middle of the screen then you would have to take all sizes of all views into consideration but I think you want to use getRawX() and getRawY()
An angle of 0 degrees correspond to the geometric angle of 0 degrees (3 o'clock on a watch.)
try using translate to translate the co-ordinates to top of the screen.
http://developer.android.com/reference/android/graphics/Canvas.html#translate(float, float)
I want to know if there is any way to modify text height and width separately when drawing with canvas. To set text size you can simply do paint.setTextSize(x);but it change text size in X and Y and I need to change text size in X and Y separately as you do with paint.setTextScaleX(x);but there isn't anything like paint.setTextScaleY(y);.
Is any way to implement this or does it already exists in Android ?
Thank you
There is no setTextScaleY method, because there is no need for one. setTextSize multiplies both X and Y scale factors, and setTextScaleX multiplies the X scale factor only. So you could reach any desired scaling scaleX, scaleY this way:
setTextSize(scaleY);
setTextScaleX(scaleX/scaleY); //setTextScaleX scales according to the CURRENT size (setTextScaleX(1) does nothing).
I have an onTouchListener on an ImageView and I use event.getX() or getY().
My goal is to display an image and launch a dialog or something when the user touch a particular part of my image.
The problem is that with different screen, the X et Y values change for the same part of my image view.
How can I get the real position of the event in pixel on every screen ?
For instance I would like to display an Android face, and do something when the user click in his eyes...
Write your code to detect the touch at a specified point using a fixed resolution, e.g. 480x640. Then get the resolution of the device the app is running on using DisplayMetrics. Calculate xScale and yScale (e.g. DisplayMetrics.widthPixels / 480) and multiply your x and y by these scale factors.