Creating a 3D flip animation in Android using XML

后端 未结 8 1728
萌比男神i
萌比男神i 2020-12-22 20:25

I have created a 3D flip of a view using this android tutorial However, I have done it programmatically and I would like to do it all in xml, if possible. I am not talking

8条回答
  •  醉梦人生
    2020-12-22 20:59

    1. The simplest way to do it is using ViewPropertyAnimator

      mImageView.animate().rotationY(360f);
      

      Using the fluent interface you can build more complex and exciting animation. E.g. you can enable hardware acceleration just call withLayer() method(API 16). More here

    2. If you want to figure out how to create 3d flick animation, please follow here and here

    3. I implemended my own solution only for a research. It includes: cancelation, accelleration, support API >= 15 and is based on Property Animation. The entire animation includes 4 parts, 2 for each side. Every objectAnimator has a listener that defines current animation index and represents an image in the onAnimationStart and current play time value in the onAnimationCancel. It looks like

      mQuarterAnim1.addListener(new AnimatorListenerAdapter() {
          @Override
          public void onAnimationStart(Animator animation) {
              mQuarterCurrentAnimStartIndex = QUARTER_ANIM_INDEX_1;
              mImageView.setImageResource(mResIdFrontCard);
          }
      
          @Override
          public void onAnimationCancel(Animator animation) {
              mQuarterCurrentAnimPlayTime = ((ObjectAnimator) animation).getCurrentPlayTime();
          }
      });
      

      For start set call

      mAnimatorSet.play(mQuarterAnim1).before(mQuarterAnim2)
      

      If AnimatorSet was canceled we can calculate delta and run the reverse animation relying on the current index animation and the current play time value.

      long degreeDelta = mQuarterCurrentAnimPlayTime * QUARTER_ROTATE / QUARTER_ANIM_DURATION;
      
      if (mQuarterCurrentAnimStartIndex == QUARTER_ANIM_INDEX_1) {
          mQuarterAnim4.setFloatValues(degreeDelta, QUARTER_FROM_1);
          mQuarterAnim4.setDuration(mQuarterCurrentAnimPlayTime);
      
          mAnimatorSet.play(mQuarterAnim4);
      }
      

    A full code snippet you can find here

提交回复
热议问题