Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Animation ObjectAnimator Pivot from center

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);
like image 836
sl133 Avatar asked Jun 23 '16 23:06

sl133


2 Answers

If you want to scale upwards one clear option is:

view.setPivotY(100);

and downwards:

view.setPivotY(0);

then animate.

like image 130
0leg Avatar answered Nov 05 '22 22:11

0leg


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>
like image 36
c4augustus Avatar answered Nov 05 '22 21:11

c4augustus