Android animation: pivot doesn't work the first time

Well I have an image centered inside a RelativeLayout

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgHomePlayPause"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:visibility="gone" />

      

Also I have this animation:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="2.0"
    android:toYScale="2.0"
    android:duration="400"
    android:pivotX="50%"
    android:pivotY="50%"/>
</set>

      

The problem is when the animation starts the first time, it scales from the top left corner, not the center. All subsequent times, everything is fine. Any ideas?

+3


source to share


1 answer


The problem can be caused when calculating pivotX and pivotY .

I needed to set View visibility to INVISIBLE instead of GONE . This made me use RelativeLayout because I had to switch between the two Views .

In your case, just change the initial visibility to invisible like this:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgHomePlayPause"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="invisible" />

      



After starting the animation for the first time, you can set the visibility to GONE and the animation will still work.

Output:

I think calculating the width and height of an element will only work if it has been drawn once. When the visibility of the elements is set to GONE , it will not be drawn at all and no width and height can be calculated.

+6


source







All Articles