Androidのトゥイーンアニメーション(デモケース付き)

Demo源码

クリック:Githubの-アニメーション

トゥイーン(ビューアニメーション)

  • カテゴリ:透明アニメーション、回転アニメーション、変位アニメーション、ズームアニメーション、アニメーションの組み合わせ
  • 執筆:コード動的に設定されたプロパティ、二つの方法で定義されたXMLファイルの属性を使用します

Tweenは、コントロールの位置を変更しません。

あなたがXML間のモーショントゥイーンを定義する必要がある場合は
まず、リソースパスの下にディレクトリを作成RES:アニメーション(名前が間違っていることはできません)
第二に、 RES /アニメーションディレクトリにXMLファイルを作成し、ルートノード(記事によると定義されたモーショントゥイーンの間に対応がありますコードの具体例)

透明アニメーション(AlphaAnimation)

コード例1(コードセット特性)

//创建一个透明动画
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);

コード例2(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>
  • コールXML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_alpha);
ivView.startAnimation(animation);

回転アニメーション(RotateAnimation)

コード例1(コードセット特性)

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);

コード例2(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>
  • コールXML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_rotate);
ivView.startAnimation(animation);

変位アニメーション(TranslateAnimation)

コード例1(コードセット特性)

//如果参照物是: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);

コード例2(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>
  • コールXML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_translate);
ivView.startAnimation(animation);

ズームアニメーション(ScaleAnimation)

コード例1(コードセット特性)

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);

コード例2(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>
  • コールXML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_scale);
ivView.startAnimation(animation);

アニメーションの組み合わせ(AnimationSet)

コード例1(コードセット特性)

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);

コード例2(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>
  • コールXML
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_set);
ivView.startAnimation(animation);

PS:
トゥイーン:私はステップピット

フレームアニメーション:こちらをクリックしてください

おすすめ

転載: www.cnblogs.com/io1024/p/11584606.html