Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a pivotX or pivotY on an ObjectAnimator defined in XML?

I have the following definition for an animation in res/animator/grow.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:ordering="sequentially">
     <set>       
       <objectAnimator
           android:propertyName="scaleX"
           android:duration="150"
           android:valueFrom="1.0"
           android:valueTo="1.5"
           android:valueType="floatType" />
       <objectAnimator
           android:propertyName="scaleY"
           android:duration="150"
           android:valueFrom="1.0"
           android:valueTo="1.5"
           android:valueType="floatType" />
     </set>
</set>

What I want is for the pivotX and pivotY to be in the center of the object, rather than the upper left hand corner. I know I could accomplish this via code, but I'd rather not do that. Instead, I'd like to specify it in the XML file. I thought about doing this:

<objectAnimator
  android:propertyName="pivotX"
  android:valueTo="50%" />
<objectAnimator
  android:propertyName="pivotY"
  android:valueTo="50%" />

But, this seems like it's going to try and animate that property as well, which is not what I want. Instead, I want it to set the pivots and then perform the animation. Can I do this in XML, and if so, how?

like image 264
jwir3 Avatar asked Nov 27 '22 15:11

jwir3


1 Answers

Old question without an answer and I didnt find an answer anywhere else, so I'll post my solution here:

Logic:

  • in the start animation use a animation to set the pivot(s) to your desired values with a duration=0
  • add durations to all other animations
  • in the end animation reset the pivot(s) by using startOffset and duration=0

Optinally, don't use durations and don't reset the pivot... In this case, the pivot will just stay as it is, which in most cases is irrelevant.

Code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="0"
                android:propertyName="pivotX"
                android:valueTo="0" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="0.85" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="0.85" />
        </set>
    </item>
    <item android:state_pressed="false">
        <set>

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.0" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.0" />

            <objectAnimator
                android:duration="0"
                android:propertyName="pivotX"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="0" />

        </set>
    </item>
</selector>
like image 71
prom85 Avatar answered Dec 04 '22 06:12

prom85