Android - Making translations and objectAnimator on the same XML file

前端 未结 2 1763
清歌不尽
清歌不尽 2020-12-16 22:52

I\'ve been trying to make a 3D Cube rotation effect while sliding from one fragment to another. First i was using a translate effect (on XML) calling with FragmentTran

相关标签:
2条回答
  • 2020-12-16 23:16

    An alternative to the answer accepted, you could define an animator set, as seen here:

      <item android:state_pressed="true">
        <set>
          <objectAnimator android:propertyName="translationZ"
            android:duration="100"
            android:valueTo="2"
            android:valueType="floatType"/>
            <!-- you could have other objectAnimator elements
                 here for "x" and "y", or other properties -->
        </set>   
      </item>   
    
      <item android:state_enabled="true"
        android:state_pressed="false"
        android:state_focused="true">
        <set>
          <objectAnimator android:propertyName="translationZ"
            android:duration="100"
            android:valueTo="2"
            android:valueType="floatType"/>
        </set> 
      </item> 
    

    How to use StateListAnimator?

    0 讨论(0)
  • 2020-12-16 23:20

    Solved the problem by creating my own methods on the extended FrameLayout. Here's the code from the extended FrameLayout:

    //Rotate from Left to Right turning visible
        public float getRotateLeftRightIn(){
            return getRotationY();
        }
        public void setRotateLeftRightIn(int rotateLeftRightIn){
            setPivotX(getWidth());
            setPivotY(getHeight()/2);
            setRotationY(rotateLeftRightIn);
        }
    

    And on the XML:

    <!-- Rotate. -->
    <objectAnimator
        android:duration="@integer/card_flip_time_full"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:propertyName="rotateLeftRightIn"
        android:valueFrom="@integer/card_flip_rotation_off"
        android:valueTo="0" 
        android:valueType="intType"/>
    

    In this case, @integer/card_flip_time_full stands for the duration of the entire animation and @integer/card_flip_rotation_off stands for the degrees (in this case -90%).

    After this, all I need to do to make this animation to work is, when starting the fragment, set the xml files in the custom animation

    transaction.setCustomAnimations(enter,exit,popEnter,popExit);
    

    Hope this can be useful to some one ^^

    0 讨论(0)
提交回复
热议问题