Android tween animation (with Demo Case)

Demo Minamoto码

Click: Github-Animation

Tween (View Animation)

  • Category: transparent animation, rotation animation, displacement animation, zoom animation, a combination of animation
  • Writing: code dynamically set properties, use xml file attributes defined in two ways

Tween does not change the position of the controls

If you need to define a motion tween between XML
First, create a directory under the resource path res: anim (name can not be wrong)
Second, create xml file in res / anim directory, the root node (according to the article there is correspondence between the motion tweens defined specific examples of code)

Transparent animation (AlphaAnimation)

Code Example 1. (code set properties)

//创建一个透明动画
Animation animation = new AlphaAnimation(1, 0);
//添加属性:设置重复播放一次(总共播放两次)
animation.setRepeatCount(1);
//设置动画重复播放的模式
animation.setRepeatMode(Animation.RESTART);
//添加属性:动画播放时长(两次播放时长)
animation.setDuration(2000);
animation.setFillAfter(true);
//设置动画
ivView.startAnimation(animation);
//设置播放监听(监听:开始播放时、播放结束时、重复播放时)
//animation.setAnimationListener(Animation.AnimationListener listener);

Code Example 2. (set attributes in XML)

  • R.anim.anim_my_alpha
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="2000"
       android:fromAlpha="1.0"
       android:repeatCount="1"
       android:fillAfter="false"
       android:repeatMode="reverse"
       android:toAlpha="0.0">
</alpha>
  • Call XML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_alpha);
ivView.startAnimation(animation);

Rotation animation (RotateAnimation)

Code Example 1. (code set properties)

RotateAnimation animation = new RotateAnimation(0, 360);
//RotateAnimation animation = new RotateAnimation(0, 360,
//        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.RESTART);
animation.setFillAfter(true);
ivView.startAnimation(animation);

Code Example 2. (set attributes in XML)

  • R.anim.anim_my_rotate
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:fillAfter="false"
        android:repeatMode="reverse"
        android:toDegrees="360">
    <!--以自身中心为点,旋转360度-->
    <!--android:pivotX="50%"-->
    <!--android:pivotY="50%"-->
    <!--加'p'是以父窗体的中心点为参照物-->
    <!--android:pivotX="50%p"-->
    <!--android:pivotY="50%p"-->
</rotate>
  • Call XML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_rotate);
ivView.startAnimation(animation);

Displacement animation (TranslateAnimation)

Code Example 1. (code set properties)

//如果参照物是:Animation.RELATIVE_TO_PARENT,那么移动的距离:是父窗体宽高的多少倍
//TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
//        Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.4f);
TranslateAnimation animation = new TranslateAnimation(300, -300, 0, 0);
animation.setDuration(2000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.RESTART);
//设置动画执行完毕后,是否停留到最后的位置上
//true:停留在最后的位置上; false:返回原来的位置上
//即便停留在最后的位置上,但控件的实际位置并没有改变(可以通过给控件设置点击事件判断)
animation.setFillAfter(true);
ivView.startAnimation(animation);

Code Example 2. (set attributes in XML)

  • R.anim.anim_my_translate
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="2000"
           android:fillAfter="false"
           android:fromXDelta="0"
           android:fromYDelta="0"
           android:repeatCount="1"
           android:repeatMode="reverse"
           android:toXDelta="0"
           android:toYDelta="30%p">
    <!--X轴不变,Y轴方向向下移动距离:父窗体高度的30%-->
    <!--带%:相当于父窗体或自身的宽高。不带%:直接就是移动多少像素-->
    <!--带p:相对于父窗体的。不带p:相对于自己-->
</translate>
  • Call XML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_translate);
ivView.startAnimation(animation);

Zoom animation (ScaleAnimation)

Code Example 1. (code set properties)

ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.RESTART);
animation.setFillAfter(true);
ivView.startAnimation(animation);

Code Example 2. (set attributes in XML)

  • R.anim.anim_my_scale
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="2000"
       android:fromXScale="1"
       android:fromYScale="1"
       android:pivotX="50%"
       android:pivotY="50%"
       android:repeatCount="1"
       android:fillAfter="false"
       android:repeatMode="reverse"
       android:toXScale="2"
       android:toYScale="2">
    <!--以自身为参照物,X、Y轴均扩大2倍-->
</scale>
  • Call XML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_scale);
ivView.startAnimation(animation);

A combination of animation (AnimationSet)

Code Example 1. (code set properties)

AnimationSet set = new AnimationSet(true);
//透明
AlphaAnimation alpha = new AlphaAnimation(1, 0);
alpha.setRepeatCount(1);
alpha.setRepeatMode(Animation.RESTART);
set.addAnimation(alpha);
//旋转
RotateAnimation rotate = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setRepeatCount(1);
rotate.setRepeatMode(Animation.RESTART);
set.addAnimation(rotate);
//位移
TranslateAnimation translate = new TranslateAnimation(0, 0, -200, 200);
translate.setRepeatCount(1);
translate.setRepeatMode(Animation.RESTART);
set.addAnimation(translate);
//缩放
ScaleAnimation scale = new ScaleAnimation(1, 2, 1, 2,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setRepeatCount(1);
scale.setRepeatMode(Animation.RESTART);
set.addAnimation(scale);

set.setDuration(3000);
ivView.startAnimation(set);

Code Example 2. (set attributes in XML)

  • R.anim.anim_my_set
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
            android:duration="2000"
            android:fromAlpha="1.0"
            android:repeatCount="1"
            android:fillAfter="false"
            android:repeatMode="reverse"
            android:toAlpha="0.0"/>
    <rotate
            android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="1"
            android:fillAfter="false"
            android:repeatMode="reverse"
            android:toDegrees="360"/>
    <translate
            android:duration="2000"
            android:fromXDelta="0"
            android:fromYDelta="0"
            android:fillAfter="false"
            android:repeatCount="1"
            android:repeatMode="reverse"
            android:toXDelta="0"
            android:toYDelta="30%p"/>
    <scale
            android:duration="2000"
            android:fromXScale="1"
            android:fromYScale="1"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="1"
            android:fillAfter="false"
            android:repeatMode="reverse"
            android:toXScale="2"
            android:toYScale="2"/>
</set>
  • Call XML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_set);
ivView.startAnimation(animation);

PS:
tween: I stepped pit

Frame Animation: click here

Guess you like

Origin www.cnblogs.com/io1024/p/11584606.html