title: Flipping a textview in android in 2 parts, setting some text inbetween
body: <p>Following the tutorial on this page and modifying the code slightly, I have managed to get what I want working (almost).</p>
  
  <p><a href="http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html" rel="nofollow noreferrer">Android Animations 3D flip</a></p>
  
  <p>What I would like to do is flip a textview from 0 to 90 degrees around the y-axis from the center of the textview. Once the textview is a vertical line on the screen, set some text for the textview and rotate back from -90 to 0. Basically giving the effect that you've turned the textview over and revealed the text behind it.</p>
  
  <p>How I am trying to do this is by defining a flip animation that rotates from x to y degrees. The first time I call this I set it from 0 to 90 and set a animation listener. Once the animation completes, the animation listener sets the text on the textview and starts a new animation from -90 to 0.</p>
  
  <p>The second animation looks like it's working as intended, but the first animation appears to be pivoting around the left edge of the textview. Illustrations below (all top down views):</p>
  
  <p><img src="https://i.stack.imgur.com/9TNox.png" alt="animation"></p>
  
  <p>The thick black lines represent where the textview starts (top down view). The red arrowed line indicates the direction the textview is rotating and the dotted line shows where the textview is after animating.</p>
  
  <p>The code that I'm using is as follows:</p>
  
  <p>FlipAnimation (taken straight from the tutorial):</p>
  
  <pre><code>public class FlipAnimation extends Animation {
  
  private final float mFromDegrees;
  private final float mToDegrees;
  private final float mCenterX;
  private final float mCenterY;
  private Camera mCamera;
  
  public FlipAnimation(float fromDegrees, float toDegrees, float centerX, float centerY) {
  
      mFromDegrees = fromDegrees;
      mToDegrees = toDegrees;
      mCenterX = centerX;
      mCenterY = centerY;
  }
  
  @Override
  public void initialize(int width, int height, int parentWidth, int parentHeight) {
  
      super.initialize(width, height, parentWidth, parentHeight);
      mCamera = new Camera();
  }
  
  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
  
      final float fromDegrees = mFromDegrees;
      float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
  
      final float centerX = mCenterX;
      final float centerY = mCenterY;
      final Camera camera = mCamera;
  
      final Matrix matrix = t.getMatrix();
  
      camera.save();
  
      camera.rotateY(degrees);
  
      camera.getMatrix(matrix);
      camera.restore();
  
      matrix.preTranslate(-centerX, -centerY);
      matrix.postTranslate(centerX, centerY);
  }
  </code></pre>
  
  <p>Flip Animation Listener:</p>
  
  <pre><code>public final class FlipAnimationListener implements AnimationListener {
  
  private TextView flippingView;
  private String newText;
  
  public FlipAnimationListener(TextView flippingView, String newText) {
  
      this.flippingView = flippingView;
      this.newText = newText;
  }
  
  public void onAnimationEnd(Animation animation) {
  
      flippingView.setText(newText);
  
      final float centerX = flippingView.getWidth() / 2.0f;
      final float centerY = flippingView.getHeight() / 2.0f;
  
      final FlipAnimation rotation = new FlipAnimation(-90, 0, centerX, centerY);
  
      rotation.setDuration(2000);
      rotation.setFillAfter(true);
      rotation.setInterpolator(new DecelerateInterpolator());
  
      flippingView.startAnimation(rotation);
  }
  
  public void onAnimationRepeat(Animation animation) {}
  
  public void onAnimationStart(Animation animation) {}
  }
  </code></pre>
  
  <p>My flipping code in the activity:</p>
  
  <pre><code>    private void startFlips() {
  
      TextView viewToChange = (TextView) findViewById(R.id.viewToChange);
  
      final float centerX = viewToChange.getWidth() / 2.0f;
      final float centerY = viewToChange.getHeight() / 2.0f;
  
      final FlipAnimation rotation = new FlipAnimation(0, 90, centerX, centerY);
  
      rotation.setDuration(2000);
      rotation.setFillAfter(true);
      rotation.setInterpolator(new AccelerateInterpolator());
      rotation.setAnimationListener(new FlipAnimationListener(viewToChange, "test"));
  
      viewToChange.startAnimation(rotation);
  }
  </code></pre>
  
  <p>Sorry about the long post, hopefully I didn't miss anything! Thanks for looking :)</p>
  
  <p>EDIT: If there is a better way to handle this type of animation, please let me know as well! Regards.</p>