Common usage of view animation

Posted by silviuchingaru on Sat, 26 Feb 2022 10:26:46 +0100

Types of animation
Android animation can be roughly divided into the following three types:
Draw animation
View animation
Property animation
Generally speaking, attribute animation is the preferred method because it is more flexible and provides more functions. View animation and frame by frame animation seem to withdraw from the stage of history, but sometimes they are very convenient to use.

Let's take a look at the four kinds of view animation
1. Translate

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"//Attribute is animation duration           
        explain: Time in milliseconds
  
        android:fromXDelta="100%"//Attribute is the position on the X coordinate at the beginning of the animation 
        android:fromYDelta="100%"//The attribute is the position on the Y coordinate at the beginning of the animation
        android:toXDelta="0"//Attribute is the position on the X coordinate at the end of the animation
        android:toYDelta="0"//The attribute is the position on the Y coordinate at the end of the animation
           
       be careful:      
       Not specified fromXType toXType fromYType toYType When,
       The default is to take yourself as the relative reference            
  
        android:interpolator="@android:anim/accelerate_interpolator"
        />//Interpolator is an interpolator that controls how fast the animation changes to complete the animation
</set>

2. Scale

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="500"
        android:fromXScale="0.0"//The attribute is the telescopic dimension in the X coordinate at the beginning of the animation
        android:fromYScale="0.0"When the attribute is the beginning of the animation Y Telescopic dimension on coordinates
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"//Property is the starting position of the animation relative to the X coordinate of the view
        android:pivotY="50%"//Property is the starting position of the animation relative to the Y coordinate of the view
        android:toXScale="1.0"//The attribute is the telescopic dimension in the X coordinate at the end of the animation
        android:toYScale="1.0"//The attribute is the telescopic size in the Y coordinate at the end of the animation / >
        
</set>

fromXScale,fromYScale,toXScale,toYScale
The above four attribute values

                  0.0 Indicates no shrinkage

                  1.0 Indicates normal no expansion    

                  Value less than 1.0 Indicates contraction 

                  Value greater than 1.0 Represents magnification

pivotX,pivotY
The above two attribute values are taken from 0% - 100%
50% is the midpoint position on the X or Y coordinate of the view

3. Rotate

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="500"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

</set>

We won't talk about the properties already introduced above. Let's talk about its private properties
Floating point value:
fromDegrees: the attribute is the angle of the object at the beginning of the animation
toDegrees: the attribute is that the rotation angle of the object at the end of the animation can be greater than 360 degrees
Note: when todegrees from degrees > 0, it indicates clockwise rotation
When todegrees fromdegrees < 0, it indicates counterclockwise rotation

pivotX="50%"
The x-axis coordinate of the rotation center, which can be value (50%), percentage (50%)
Percentage p (50%p)
When the value is a numerical value, the zoom starting point is the coordinates of the upper left corner of the View plus specific numerical pixels.
When the value is percentage, it indicates that the specific percentage of View width is added to the upper left corner of the current View.
When the value is percentage p, it indicates that the specific percentage of the parent control width is added to the upper left corner coordinate of the View.

4. Transparency (alpha)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
  
</set>
fromAlpha Attribute is transparency at the beginning of the animation
toAlpha   Attribute is transparency at the end of the animation
 explain:
0.0 Indicates complete transparency
1.0 Indicates complete opacity
 The above value is taken as 0.0-1.0 Between float Number of data types

5. Some public methods
XML attribute: android:repeatCount
Association method: setRepeatCount(int)
Description: sets the number of times the animation is repeated. The default value is 0

XML attribute: android:repeatMode
Association method: setRepeatMode(int)
Note: set the mode of animation repetition. The values can be: restart (1), which means sequential playback, and reverse (2), which means reverse playback when repeating

XML attribute: android:fillAfter
Association method: setFillAfter(boolean)
Note: indicates whether to keep the post animation state after the animation is completed. true keeps the post animation state, false restores the original state, and the default value is false

XML attribute: android:fillBefore
Association method: setFillBefore(boolean)
Note: indicates whether to keep the state before the animation after the animation. True restores the original state. false keeps the state after the animation. The default value is true

XML attribute: android:fillEnabled
Association method: setFillEnabled(boolean)
Note: if set to true, the fillBefore setting is taken into account

XML attribute: android:startOffset
Association method: setStartOffset(long)
Note: set the delay time of start (unit: ms). The default value is 0

6. The following is mainly about interpolator
Acceleratedeelerateinterpolator changes slowly at the beginning and end of the animation, and accelerates in the middle

Acceleration interpolator changes slowly at the beginning of the animation, and then starts to accelerate

The AnticipateInterpolator starts backward and then swings forward

The AnticipateOvershootInterpolator starts by throwing a certain value backwards and forwards, and then returns the last value

Bounce interpolator pops up at the end of the animation

The CycleInterpolator animation loops a specific number of times, with the rate changing along a sinusoidal curve

DecelerateInterpolator is fast and then slow at the beginning of the animation

LinearInterpolator changes at a constant rate

Overshoot interpolator swings forward a certain value and then returns to its original position

7.overridePendingTransition()

Realize the animation when two activities switch. Use in Activity
There are two parameters: entry animation and exit animation.

be careful
1. Must be called immediately after StartActivity() or finish().

val intent = Intent(this, ClassroomPowerControl::class.java)
                startActivity(intent)
                overridePendingTransition(R.anim.loginenter, R.anim.anim_no)
override fun finish() {
        super.finish()
        overridePendingTransition(0,R.anim.alpha)
    }

The following is the pit encountered. When you cut from ActivityA to Activity B, because you set the animation for ActivityA to enter Activity B and did not set the animation for leaving ActivityA, there will be a black screen in the middle, and you only need to add another leaving animation.
as follows
overridePendingTransition(R.anim.loginenter, R.anim.anim_no)
R.anim. Login is the animation of entering Activity B
R.anim.anim_no is the animation that leaves ActivityA, but in fact it has no substantive animation, but only plays a delayed effect.
Note that the duration of both animations should be the same.
as follows
R.anim.loginenter:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="100%" android:toYDelta="0" /> </set>

R.anim.anim_no

<?xml version="1.0" encoding="utf-8"?>

8. If you want to achieve the effect of animation sequence execution
You need to set the following two properties
XML attribute: android:fillAfter
Association method: setFillAfter(boolean)
Note: indicates whether to keep the post animation state after the animation is completed. true keeps the post animation state, false restores the original state, and the default value is false
XML attribute: android:startOffset
Association method: setStartOffset(long)
Note: set the delay time of start (unit: ms). The default value is 0

such as

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <set android:fillAfter="true">
        <alpha
            android:duration="500"
            android:fromAlpha="1.0"
            android:toAlpha="0.5" />
        <scale
            android:duration="500"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="0.1"
            android:toYScale="0.1" />

    </set>
    <set
        android:fillAfter="true"
        android:startOffset="500">
        <translate
            android:duration="400"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toXDelta="100%" />
    </set>
    <set
        android:fillAfter="true"
        android:startOffset="900">
        <rotate
            android:duration="700"
            android:fromDegrees="0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="-360" />

    </set>
</set>

Previously, I set it in the rotate, translate, scale and alpha tags, but I found it impossible. Later, I moved it to the set tag.

Topics: Android xml