Android frame animation patching animation attribute animation

There are three types of animation in Android:
  1 frame animation
  2 animation of mending room
  3 attribute animation

1 frame animation

Look at the picture first

The common method is xml resource file
Create the animation.xml file under res/drawable

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    >
    <!--false  Represent loop play   true For once-->

    <item android:drawable="@drawable/animate_01" android:duration="150"/>
    <item android:drawable="@drawable/animate_02" android:duration="150"/>
    <item android:drawable="@drawable/animate_03" android:duration="150"/>
    <item android:drawable="@drawable/animate_04" android:duration="150"/>
    <item android:drawable="@drawable/animate_05" android:duration="150"/>
    <item android:drawable="@drawable/animate_06" android:duration="150"/>
    <item android:drawable="@drawable/animate_07" android:duration="150"/>
    <item android:drawable="@drawable/animate_08" android:duration="150"/>
    <item android:drawable="@drawable/animate_09" android:duration="150"/>
    <item android:drawable="@drawable/animate_10" android:duration="150"/>
    <item android:drawable="@drawable/animate_11" android:duration="150"/>
    <item android:drawable="@drawable/animate_12" android:duration="150"/>
    <item android:drawable="@drawable/animate_13" android:duration="150"/>
    <item android:drawable="@drawable/animate_14" android:duration="150"/>
</animation-list>

Import in interface layout

<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/animation"
        />

Run to get the effect in the figure.

How to code

  ImageView imageView = (ImageView) findViewById(R.id.image_view);

        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_01),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_02),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_03),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_04),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_05),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_06),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_07),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_08),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_09),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_10),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_11),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_12),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_13),150);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.animate_14),150);


        imageView.setBackground(animationDrawable);
        //Loop Playback
        animationDrawable.setOneShot(false);
        //start
        animationDrawable.start();
        //Stop it
//        animationDrawable.stop();

2 TweenAnimation

There are four kinds of mending Animation:

 1 AlphaAnimation gradient animation

 2 RotateAnimation rotation animation

 3 ScaleAnimation scale animation

 4 TranslateAnimation

####AlphaAnimation gradient animation

    //From transparent to opaque
    AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
    //time
    alphaAnimation.setDuration(1500);
    //Set the number of repetitions after the animation is executed once
    alphaAnimation.setRepeatCount(0);
    mImageViewCat.startAnimation(alphaAnimation);

RotateAnimation rotation animation

    //Rotate 90 degrees clockwise with the top left corner as the center
    //RotateAnimation rotateAnimation = new RotateAnimation(0,90);
    //Rotate 90 degrees clockwise with the center of the picture as the center
    RotateAnimation rotateAnimation = new RotateAnimation(0,90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(1500);
    //Set the number of repetitions after the animation is executed once
    rotateAnimation.setRepeatCount(2);
    //Save state after animation execution false do not save true save
    rotateAnimation.setFillAfter(false);
    mImageViewCat.startAnimation(rotateAnimation);

ScaleAnimation scale animation

    //Zoom in from 0 to 1 centered on the center of the picture
    ScaleAnimation scaleAnimation = new ScaleAnimation(
            0,1.0f, 0, 1.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setDuration(2000);
    mImageViewCat.startAnimation(scaleAnimation);

TranslateAnimation translate animation

    //xPan the width of the picture in the right direction yDirection pan down the height of the picture
    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
    translateAnimation.setDuration(2000);
    mImageViewCat.startAnimation(translateAnimation);

AnimationSet animation set

    //Gradient rotation zoom translation at the same time
    AnimationSet animationSet = new AnimationSet(false);
    AlphaAnimation alphaAnimationS = new AlphaAnimation(0,1);
    RotateAnimation rotateAnimationS = new RotateAnimation(0,90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    ScaleAnimation scaleAnimationS = new ScaleAnimation(
            0,1.0f, 0, 1.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    TranslateAnimation translateAnimationS = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);

    animationSet.addAnimation(alphaAnimationS);
    animationSet.addAnimation(rotateAnimationS);
    animationSet.addAnimation(scaleAnimationS);
    animationSet.addAnimation(translateAnimationS);
    animationSet.setDuration(3000);
    mImageViewCat.startAnimation(animationSet);

3 property animation

Gradual change

//The first parameter specifies the control to be animated
//The second parameter specifies the properties of the control
//The third parameter is variable length
ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"alpha",1,0,1);
animator.setDuration(3000);
animator.start();

rotate

         //Rotate, first turn clockwise, and then turn anticlockwise
                //withzDirection is axis rotation
                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"rotation",0,360,0);
                //withxDirection is axis rotation
//                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"rotationX",0,360,0);
                //withyDirection is axis rotation
//                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"rotationY",0,360,0);
                animator.setDuration(3000);
                animator.start();

zoom

         //Zoom in2Times smaller to original size
                // Width zoom in and out
                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"scaleX",1,2,1);
                //Height zoom in and out
//                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"scaleY",1,2,1);
                animator.setDuration(3000);
                animator.start();

translation

        //translation
                // xDirection translation
                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"translationX",1,100,1);
                //yDirection translation
//                ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViewCat,"translationY",1,100,1);
                animator.setDuration(3000);
                animator.start();

aggregate

         ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mImageViewCat,"alpha",1,0,1);
                ObjectAnimator rotaionAnimator = ObjectAnimator.ofFloat(mImageViewCat,"rotation",0,360,0);
                ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mImageViewCat,"scaleX",1,2,1);
                ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(mImageViewCat,"translationX",1,100,1);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(alphaAnimator,rotaionAnimator,scaleXAnimator,translationXAnimator);
                animatorSet.setDuration(3000);
                animatorSet.start();

Tags: Android xml Attribute encoding

Posted on Mon, 04 May 2020 14:44:55 -0700 by plasmagames