A series of

  • Animation | Android frame animation is analysed
  • Shallow of animation animation | Android attributes
  • Animation | animation analyses between Android to fill
  • Animation | Android transition animations

What is frame animation

Baidu Encyclopedia explains

Frame-by-frame animation is a common form of animation (Frame By Frame). Its principle is to decompose animation actions in “continuous keyframes”, that is, to draw different content on each Frame of the timeline Frame By Frame, so that it can be continuously played into animation. Because of frame by frame animation frames the content is different, not only to add to the burden of production and the amount of the final output file is big, but its advantage is clear: frame by frame animation has very big flexibility, can be almost any want to the content of the performance, and it similar to the movie playback mode, very suitable for the performance of animation. Examples include sharp turns of characters or animals, flowing hair and clothes, walking, talking, and elaborate 3D effects

Classification of animation

In Android development, animation is divided into two categories: attribute animation and view animation. View animation can be subdivided into frame animation and tween animation

  • Attribute animation
  • View the animation
    • The frame of animation
    • Filling between animation

How to use frame animation

In Android development, frame animation is used, and its core class is AnimationDrawable.

There are two ways to animate frames, either by writing static XML to an AnimationDrawable, or by adding keyframes to an AnimationDrawable object after it has been created

Create an XML resource file

Drawable in XML creates a resource whose root node is animation-list

Place each frame in order

For example, create a new anim_frame.xml


      
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <! -- Oneshot plays only once -->
    <! -- duration Delay time in milliseconds -->
    <item android:drawable="@drawable/farm_0" android:duration="100" />
    <item android:drawable="@drawable/farm_1" android:duration="100" />
    <item android:drawable="@drawable/farm_2" android:duration="100" />

</animation-list>
Copy the code

Code sample



class FrameAct : AppCompatActivity(a){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ainm_frame)


        // Approach 1 uses XML

        // Find the resource for the frame animation
        val animationDrawable: AnimationDrawable =
            ContextCompat.getDrawable(this, R.drawable.anim_frame) as AnimationDrawable
        // Set image background
        img_frame.background = animationDrawable
        // You can also set the ImageView background directly in XML


        // Approach 2 dynamic code addition

        val animationDrawable2 = AnimationDrawable()

        // Add image information
        for (index in 0.2.) {
            val resId = resources.getIdentifier("farm_$index"."drawable", packageName)
            animationDrawable2.addFrame(resources.getDrawable(resId, null), 100)}// Set the loop to play
        animationDrawable2.isOneShot = false
        // Set AnimationDrawable to imageView
        img_frame_2.setImageDrawable(animationDrawable2)


        btn_start.setOnClickListener {
            // Start playing frame animation
            animationDrawable.start()
            animationDrawable2.start()
        }

        btn_stop.setOnClickListener {
            // Stop playing frame animation
            animationDrawable.stop()
            animationDrawable2.stop()
        }


    }
}
Copy the code

Attribute Value Meaning

parameter meaning
oneshot Whether to play only once False: Play only once. True: loops
duration Playing time
drawable Image resources

Frame animation advantages and disadvantages analysis

Advantages: For very complex animations, Lottie will be used for very complex animations in real projects

Disadvantages: because many pictures are played frame by frame, the consumption of memory is relatively large.