24

I am trying to scale views to a certain size but can't quite understand how pivoting works.

Say I want to scale the view upwards only. What value should the "pivotY" hold? In XML, it is a percentage. How is it when applying pivot point programmatically?

Example:

ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", scaleSize);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view, "pivotY", pivotPoint);

AnimatorSet set = new AnimatorSet();
set.PlayTogether(scaleY, pivotY);
Pang
  • 9,564
  • 146
  • 81
  • 122
TalMihr
  • 1,528
  • 1
  • 15
  • 31

3 Answers3

61

Quite simple actually.

If you want to scale upwards one clear option is:

 view.setPivotY(100);

and downwards:

 view.setPivotY(0);

then animate.

TalMihr
  • 1,528
  • 1
  • 15
  • 31
  • 2
    what to do for <11 support? – Amrut Bidri Feb 17 '15 at 11:16
  • 14
    Not quite THAT simple. First, **Setting this property disables this behavior and causes the view to use only the explicitly set pivotX and pivotY values.** Also, the values used aren't percentages, they *Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).* (from https://developer.android.com/reference/android/view/View.html). – CODE-REaD Aug 26 '16 at 20:57
  • @TalMihr Any idea how to scale Left and Right ? – vgokul129 Jul 08 '20 at 13:36
12

Use:

view.setPivotY(view.getMeasuredHeight());

If you need to animate your object from the bottom.

Community
  • 1
  • 1
DoruChidean
  • 7,941
  • 1
  • 29
  • 33
1

Your pivot point takes your view as reference. So a pivot point set at 0,0 means it matches the top left side of your view.

So this animation:

ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f)

Will be affected by pivotY

view.pivotY = 0 // will shrink the view towards the view top
view.pivotY = view.measuredHeight.toFloat() // will shrink the view towards the view bottom
view.resetPivot() // will shrink the view towards the center

In the same way, this animation:

ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f)

Will be affected by pivotX

view.pivotX = 0 // will shrink the view towards the view left
view.pivotX = view.measuredHeight.toFloat() // will shrink the view towards the view right
view.resetPivot() // will shrink the view towards the center
Juan M. Rivero
  • 807
  • 13
  • 18