Search code examples
androidandroid-animationandroid-cardviewflipobjectanimator

Flip Up Animation


I'm trying to make a flipping animation in Android. I am using this code:

<set xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- Set alpha to 0 before animation -->
  <objectAnimator
    android:valueFrom="1.0"
    android:valueTo="0.0"
    android:propertyName="alpha"
    android:duration="0"/>

  <!-- Rotate -->
  <objectAnimator
    android:valueFrom="180"
    android:valueTo="0"
    android:propertyName="rotationY"
    android:interpolator="@android:interpolator/accelerate_decelerate"
    android:duration="@integer/card_flip_time_full"/>

  <!-- Half-way through the rotation set the alpha to 1 -->
  <objectAnimator
    android:valueFrom="0.0"
    android:valueTo="1.0"
    android:propertyName="alpha"
    android:startOffset="@integer/card_flip_time_half"
    android:duration="1"/>
</set>

However, I want it to flip from top to bottom. Something like this:

Flip

How do I do this?


Solution

  • You can do it programmatically. Here is the C# from Xamarin. Can't test the Java right now but it will be very similar.

    ImageView block = FindViewById<ImageView>(Resource.Id.imgBlock);
    ObjectAnimator blockSpin = ObjectAnimator.OfFloat(block, "rotationX", 0, 360);
    imageCard.PivotX = 0.5f; //so it spins on center
    
    float scale = metrics.Density;
    imageCard.SetCameraDistance(8000*scale);
      /*Set camera distance as required to avoid distortion. 
         Multiplying by scale keeps distance the same across devices. */
    blockSpin.SetDuration(500);
    blockSpin.Start();