[Animation study notes] Animation-animation (Frame-frame-by-frame/Tween-tween [two implementations])

Premise: novice making, don't spray if you don't like it, please point out any mistakes, thank you!

1. The concept of animation

It can be understood as a process through which objects on the screen change their color, position, size, and direction over time.

Animation idea: Knowing the starting and ending states of the graph, you can change some aspects of the graph in the middle of it.

2. Frame Animation (FrameAnimation)

Frame-by-frame animation is a simple process of displaying a series of images in succession at short intervals, so the end effect is a moving object.

2.1 Use steps

    1. Place the image in the drawable

    2. Get the AnimationDrawable() object

    3. Get the image id by looping

    4. Add the Drawable object of the picture to the AnimationDrawable() object

    5. Start the animation in the listener event

<hr>


Effect display - Newton of ghosts


Frame-by-frame animation, as the name implies, is a frame-by-frame animation, so you need to have a picture source first.

Since it is a picture source, it is not a picture but a group of pictures!

First, you need to create an xml to declare this set of pictures


The creation method is not repeated here

animal.xml code demo

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/frame_0_delay_05s" android:duration="7"/>
    <item android:drawable="@drawable/frame_1_delay_02s" android:duration="7"/>
    <item android:drawable="@drawable/frame_2_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_3_delay_01s" android:duration="7"/>
    <item android:drawable="@drawable/frame_4_delay_02s" android:duration="7"/>
    <item android:drawable="@drawable/frame_5_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_6_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_7_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_8_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_9_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_10_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_11_delay_005s" android:duration="7"/>
    <item android:drawable="@drawable/frame_12_delay_005s" android:duration="7"/>
</animation-list>

Among them, duration means 'duration', that is, the duration of the current picture displayed in milliseconds, here is 7, which means 7ms 

You only need to put an ImageView with an Id in the layout file.

Next in Activity

public class FrameAnimationActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    setContentView 
        (R.layout.activity_frame_animation );
 //         Find the ImageView
 declared in the layout file imageView = (ImageView)findViewById( R.id.imageView );
 //         Set the image collection to the animal.xml just created imageView
 .setBackgroundResource (R .drawable. animal );
 //         Put the image collection into AnimationDrawable
 AnimationDrawable ad = (AnimationDrawable) imageView .getBackground ();
 //         Start ad.start
 ();                                
    }
}

Frame-by-frame animation

<hr>

Next is the motion tweening

There are two ways to implement motion tweening (Java code-based and XML-based)

There are four types of animation for tweening

They are:

  • Gradient Transparency Animation - AlphaAnimation
  • Gradient size scaling animation - ScaleAnimation
  • Screen position movement animation-TranSlateAnimation
  • Screen rotation animation-RotateAnimation

首先介绍基于Java代码的实现方式

效果展示:



代码展示:

布局:

<ImageView
    android:layout_centerInParent="true"
    android:padding="1dp"
    android:background="@color/colorAccent"
    android:id="@+id/imageView_tween"
    android:layout_width="200dp"
    android:layout_height="128dp" />

活动:

public class TweenAnimationActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_animation);

        imageView = (ImageView)findViewById(R.id.imageView_tween);
//        图片随便设置
        imageView.setImageResource(R.drawable.frame_0_delay_05s);
    }

//    移动效果
    public void OnTranslateAnimation(View view){
        TranslateAnimation animation0 = new TranslateAnimation(0.0f,0.0f,-500.0f,0.0f);
//        set Persistence is 5 seconds
        animation0.setDuration(5000);
        imageView.startAnimation(animation0);
//        保持动画完的状态
        animation0.setFillAfter(true);
    }

//    改变透明度效果
    public void OnAlphaAnimation(View view){
        AlphaAnimation animation1 = new AlphaAnimation(0.1f,1.0f);
//        set Persistence is 5 seconds
        animation1.setDuration(5000);
        imageView.startAnimation(animation1);
//        保持动画完的状态
        animation1.setFillAfter(true);
    }

//    旋转效果
    public void OnRotateAnimation(View view){
        RotateAnimation animation2 = new RotateAnimation(0.0f,+360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        set Persistence is 5 seconds
        animation2.setDuration(5000);
        imageView.startAnimation(animation2);
//        保持动画完的状态
        animation2.setFillAfter(true);
    }
//    变大变小效果
    public void OnScaleAnimation(View view){
        ScaleAnimation animation3 = new ScaleAnimation(0.0f,1f,0.0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        set Persistence is 5 seconds
        animation3.setDuration(5000);
        imageView.startAnimation(animation3);
//        保持动画完的状态
        animation3.setFillAfter(true);
    }


//    效果集合
    public void OnAnimationSet(View view){

        TranslateAnimation animation0 = new TranslateAnimation(0.0f,0.0f,-500.0f,0.0f);
        AlphaAnimation animation1 = new AlphaAnimation(0.1f,1.0f);
        RotateAnimation animation2 = new RotateAnimation(0.0f,+360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ScaleAnimation animation3 = new ScaleAnimation(0.0f,1f,0.0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

//        活动集合,装入活动,是一次性同时执行,不是一个接一个执行
        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(animation0);
        animationSet.addAnimation(animation1);
        animationSet.addAnimation(animation2);
        animationSet.addAnimation(animation3);
//        set Persistence is 5 seconds
        animationSet.setDuration(5000);
        imageView.startAnimation(animationSet);

    }

}

演示完毕,demo已上传

链接:点击打开链接 密码:94fp

解压密码:csdn



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325541380&siteId=291194637