Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android scale animation: pivot doesn't work first time

Well, I have image view centered inside 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 animation starts first time, it scales from the top left corner, not from the center. All the next times it works just fine. Any ideas?

like image 229
Vadym Kovalenko Avatar asked Jul 22 '15 08:07

Vadym Kovalenko


1 Answers

The problem might be caused when calculating the pivotX and pivotY.

I had to set the View visibility to INVISIBLE instead of GONE at the beginning. This forced me to use a RelativeLayout because I had to toggle between two View.

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 the first start of the animation you are able to set the visibility to GONE and the animation will still work.

Conclusion:

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

like image 177
Yoraco Gonzales Avatar answered Oct 16 '22 23:10

Yoraco Gonzales