How to change pivotX, pivotY in android? - android

I add view dynamically then use view.setX(100) and view.setY(100).
I want setPivotX and setPivotY for use scale animation.
I try ViewHelper.setPivotX, AnimatorProxy.setPivotX, view.setPivotX but not working. Some one help me

Be v a view,
v.setPivotX(0);
v.setPivotY(0);
Will set the pivot point at (0,0) (left-top)
v.setPivotX(v.getWidth() / 2);
v.setPivotY(v.getHeight() / 2);
Will set the pivot point at the view center
v.setPivotX(v.getWidth());
v.setPivotY(v.getHeight());
Will set the pivot point at the bottom right.
After setting the pivot point, you can then rotate the view to see it's working:
v.setRotationX((float)45);
v.setRotationY((float)45);

Related

Android calculate rotationY based on view right edge

To get view right edge after rotating it around Y-axis with fixed pivot point I'm using the below code and that is working like it should.
view.setPivotX(0);
view.setPivotY(view.getHeight() / 2);
view.animate().rotationY(rotationAngle).start();
view.getMatrix().mapRect(rectF);
int rightEdge = rectF.right;
Now what I want to do next is to calculate rotationAngle so the right edge of the view will be a specified value (let say 1000px). Any idea how to reach this ?

android objectanimator scaleX only in one direction

I am using ObjectAnimator and cant figure out how to scale in x axe only in one direction for example to right side. Because when im scaling it scales both ways and then my ImageView is out of screen. I know i could add transitionX to prevent going off-screen but a better solution would be scaling in one direction.
ObjectAnimator headAnim = ObjectAnimator.ofFloat(imageHead, "scaleX", 1f, 1.5f);
headAnim.setDuration(1000);
headAnim.start();
Just set the pivot point of the View, using View.setPivotX() and View.setPivotY(). Set it to 0,0 to achieve the effect you want.
The param of View.setPivotX/Y is a float value. It is the absolute value of width or height, not the percent.

What is programmatically equivalent to android:layout_centerInParent="true" of RelativeLayout?

I place an ImageView of a pin in the center of the layout by using android:layout_centerInParent="true" in my RelativaLayout XML file.
Now I wish to draw the green dot at the same position as the pin on the canvas.
NOTE: the green dot is NOT a view. It is drawn on canvas by canvas.drawCircle();
That is, I have to programmatically get the coordinates of the pin.
So how can I get the coordinates of android:layout_centerInParent="true" with codes?
My guess is
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
You can get layoutParams by relativeLayout.getLayoutParams(), and don't forget to setLayoutParams back when you're done modifying it.
To get the width and height of the parent you can do this.
RelativeLayout parent = (RelativeLayout) findViewById(R.id.yourRelativeLayout);
int width = parent.getWidth();
int height = parent.getHeight();
Then you can divide these numbers by 2 and set that to as your green dot's coordinates and it should appear in the middle of your screen. For this to work your canvas size has to be the same as the relative layout.
But beware, you need to call getWidth() and getHeight() methods after the activity has been created, else you will end up getting zero. See this answer
So, the canvas you are drawing to is in a view that is contained by the RelativeLayout, but you want to draw the dot at the center of the RelativeLayout?
Yes, that is exactly what I am trying to do!
Assuming the canvas view is a direct child of the RealtiveLayout, this should work.
You can get the layout's center by using getWidth() / 2 and getHeight() / 2 on the layout as others mentioned. However, you also have to figure out where the origin of the canvas is. For this you can just use getLeft() and getTop() on the canvas view. Then you just subtract the center x from left, and center y from top to get your final spot.
Example:
Assume each grid line is 1. The RelativeLayout is the large black rectangle, and the Canvas view is the blue one. The center dot's coordinates are 4,6. Using left/top, you get 1,4 for the canvas origin(red dot). Subtract, and you get 3,2, which are the local canvas coordinates for the green dot.
Drawing the dot in the center of the canvas should just be a matter of dividing the width and height of the view by two. If you want it to be at the base of the pin (as opposed to the true center), then just add half the height of the pin to the circle's y value.

Android: Manually Scaling a View

Is there a way to manually scale a view while making sure the position translates properly? The only way I can scale it right now is to update the LayoutParams by essentially multiplying the width and height my a scale factor. This works okay but I'm not sure about how to translate the position properly. Further, I want to scale the view with the pivot being the center as well. I'm actually able to perform the behavior I want with by using the ScaleAnimation like so,
ScaleAnimation anim = new ScaleAnimation(fromX, toX,
fromY, toY, ScaleAnimation.RELATIVE_TO_SELF,
0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
However, the actual view's bounds are not adjusted. The majority of scaling implementations and help always deal with an ImageView so I figured it's worth asking for a View only. I'd be greatly appreciative if any has any ideas on this matter.
It sounds like you want to scale a View, but scale it from the center of the View. This is tricky because the View is positioned based on it's top and left offset from the top left corner of it's parent.
So -- if you have a view that is 100px from the top, and 100px from the left, and the size is 50px by 50px - and you want to scale it by +10%:
width = 50px * 1.1 = 55px;
height = 50px * 1.1 = 55px;
but those extra 5px will be added to the right and bottom of the View, right? so we need to change the offset of the View to adjust:
top = 100px - ((100px * .1) / 2) = 97.5px
(same for the left, obviously).
using this pattern, you can scale the view in a way that keeps the center point consistent.

Rotating android view without affecting adjacent views

I'm rotating a view containing an arrow in my app using the Matrix class. However, the arrow doesn't rotate around its center but moves a bit horizontally and vertically when rotating. I've experimented with margins and padding but without success.
Any hints much appreciated.
The setRotate method in Matrix defaults to the (0,0) point of the view, which is the top left corner. You can set the point you want to rotate around by using the setRotate(float angle, float px, float py) method in the Matrix class. The x and y parameters are local to the view so you can get the center point from the bounds of the view or from getWidth and getHeight.
I think the translation works as you expect. It is the way you draw the resulting image which leads to imprecision. I have just answered a similar question Android problem with Image Rotate and Matrix

Categories

Resources