Android Animation ObjectAnimator Pivot from center
Asked Answered
D

3

10

Another simple question that I can't seem to wrap my head around. I want an animation using ObjectAnimator to scale upwards from the center. However, I'm not sure how to set the PivotX/Y properties as any value I apply doesn't seem to affect the view. When I was using a scaleanimation, it worked fine but I must use an ObjectAnimator here.

I've tried

ObjectAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX",0.0f,1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY",0.0f,1.0f);

//I've tried a wide range of values from 0,0 to 0,0.5, to 0.5, 0.5 but none of them do anything
ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",0,1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",0,1f);

//I've also tried view.setPivotX(0.5f) but that didn't do anything either

animatorSet.playTogether(scaleX,scaleY,pivotX,pivotY);
animatorSet.start();

I'm just not really sure how to make it scale from the center. I've tried not even using pivot but that didn't do anything either.

Any help is appreciated,

Thanks

** EDIT **

The following sort of works, except it isn't completely centered it instead grows towards the top left but still sort of centered. Its hard to describe. I tried using 0.5f and it didn't work either

ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",1f);
Dikdik answered 23/6, 2016 at 23:19 Comment(0)
M
7

I was having a similar problem scaling a vector animation defined in XML. It now scales from its center after placing the pivotX & pivotY settings in the Drawable instead of the ObjectAnimator, like so:

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:aapt="http://schemas.android.com/aapt" >
    <aapt:attr name="android:drawable">
        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="100dp"
            android:height="100dp"
            android:viewportWidth="100"
            android:viewportHeight="100">
            <group
                android:name="scaleGroup"
                android:pivotX="50"
                android:pivotY="50"
                android:scaleX="1.0"
                android:scaleY="1.0" >
                <path
                    android:name="v"
                    android:strokeColor="#00e9bd"
                    android:strokeWidth="6"
                    android:pathData="M 50 13 C 70.4345357437 13 87 29.5654642563 87 50 C 87 70.4345357437 70.4345357437 87 50 87 C 29.5654642563 87 13 70.4345357437 13 50 C 13 29.5654642563 29.5654642563 13 50 13 Z" />
            </group>
        </vector>
    </aapt:attr>

    <target android:name="scaleGroup"> *
        <aapt:attr name="android:animation">
            <set>
                <objectAnimator
                   android:duration="1000"
                   android:propertyName="scaleX"
                   android:valueFrom="0.5"
                   android:valueTo="1.0" />
               <objectAnimator
                   android:duration="1000"
                   android:propertyName="scaleY"
                   android:valueFrom="0.5"
                   android:valueTo="1.0" />
            </set>
        </aapt:attr>
    </target>
</animated-vector>
Monseigneur answered 6/11, 2017 at 20:9 Comment(0)
A
5

If you want to scale upwards one clear option is:

view.setPivotY(100);

and downwards:

view.setPivotY(0);

then animate.

Agone answered 3/5, 2017 at 13:48 Comment(0)
N
1

pivotX and pivotY should be 1/2 the viewportWidth and viewportHeight respectively if you want to scale from the center.

These properties should be set on the group tag of your vector drawable. This group tag should be outside of the path tag that you want to scale, similar to the answer above.

Nazarite answered 20/9, 2018 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.