Animation in Android (frame animation, interpolation animation, attribute animation)

Generally speaking, Android animation can be divided into two categories, the original traditional animation and the attribute animation after Android 3.0.
Traditional animation includes: Frame Animation and Tweened Animation.
Let's talk about the use and characteristics of various animations.

Frame animation: is the easiest kind of animation to achieve, it depends on perfect UI resources; the principle of implementation is to play individual pictures together, so as to produce visual animation effect, similar to some ways of making gif animation.

To create an XML file in / res/drawable / directory, animation-list must be used as the root element, item as the image to be displayed in rotation, and duration attribute as the display time.

xml:

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/a_0"
            android:duration="100" />
        <item
            android:drawable="@drawable/a_1"
            android:duration="100" />
        <item
            android:drawable="@drawable/a_2"
            android:duration="100" />
        ...
    </animation-list>

java :

    protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_frame_animation);
            ImageView animationImg1 = (ImageView) findViewById(R.id.animation1);
            animationImg1.setImageResource(R.drawable.frame_anim1);
            AnimationDrawable animationDrawable1 = (AnimationDrawable) animationImg1.getDrawable();
            animationDrawable1.start();
        }

Inter-patch animation: It is to constantly change the image of the object in the scene to produce animation effects (rotation, translation, scaling and gradient). They are alpha (fade in and fade out), translate (displacement), scale (zoom in and out), rotate (rotation).

Implementing Intermediate Animation: An XML File (File in res/anim), a direct code to determine the type of animation
Usually in the form of xml files; code will be easier to write and read, but also easier to reuse.

First, define the following animation implementation under res/anim/folder
xml implementation:

alpha.xml
    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.0" />

scale.xml
    <?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0"/>

java:

Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim);
    img = (ImageView) findViewById(R.id.img);
    img.startAnimation(animation);

Use the set tag to combine multiple animations (code from Android SDK API):

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@[package:]anim/interpolator_resource"
        android:shareInterpolator=["true" | "false"] >
        <alpha
            android:fromAlpha="float"
            android:toAlpha="float" />
        <scale
            android:fromXScale="float"
            android:toXScale="float"
            android:fromYScale="float"
            android:toYScale="float"
            android:pivotX="float"
            android:pivotY="float" />
        <translate
            android:fromXDelta="float"
            android:toXDelta="float"
            android:fromYDelta="float"
            android:toYDelta="float" />
        <rotate
            android:fromDegrees="float"
            android:toDegrees="float"
            android:pivotX="float"
            android:pivotY="float" />
        <set>
            ...
        </set>
    </set>

You can see that composite animation can be nested.

The second way to implement java is:

Related classes:
AlphaAnimation Gradual Transparency Animation Effect

Animation animationAlpha = new AlphaAnimation(0.0f, 1.0f);
animationAlpha.setDuration(3000);
ivAnim.startAnimation(animationAlpha);

Scale Animation Scaling Animation Effect with Gradual Size

Animation animationScale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationScale.setDuration(3000);
ivAnim.startAnimation(animationScale);

Translate Animation Picture Transfer Position Moving Animation Effect

Animation animationTranslate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
animationTranslate.setDuration(3000);
ivAnim.startAnimation(animationTranslate);

Rotate Animation Picture Transfer Rotation Animation Effect

Animation animationRotate = new RotateAnimation(0.0f,+350.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationRotate.setDuration(3000);
ivAnim.startAnimation(animationRotate);

Shortcomings of Interfix Animation: Visual effects have changed, but the values of attributes have not changed, or the real location has not changed, and can interact to
The difference between attribute animation and complementary animation:

Intermediate animation: through the parent container to constantly draw view, view control itself is unchanged;
Attribute animation: Change view by constantly changing the attribute value of your view.

Attribute animation: It was introduced in Android 3.0, which changed the actual attributes of the object; in order to solve the shortcomings of complementary animation.

Related classes: Object Animator, Value Animator, Animator Set, ViewCompat
b. Usage:

  ObjectAnimator anim = ObjectAnimator.ofInt(view,"transtionX",100);
      anim.setDuration(100);
      anim.start();
ViewCompat.animate(view).tranxlaionX(100)
                        .scaleX(1.2f)
                        .setDuration()
                        .start();

Value Animator adds:
Essence: It helps us define an animation execution process, but it doesn't have any animation logic. It allows us to achieve any animation effect, such as you want to change the width and height of a View and the background color. In addition, the countdown effect can be achieved.

Low version compatibility of attribute animation:

Since Android 3.0, attribute animation has been introduced, which can easily do many things that View animation can not do. However, it is impossible to use attribute animation below Android 3.0. If you need to use attribute animation below Android 3.0, you need to use an open source jar package: nineoldandroids.jar (Click Download)

Keywords: Android xml Attribute encoding

Added by rofl on Tue, 16 Jul 2019 02:01:57 +0300