欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓动画实现

程序员文章站 2022-05-04 19:41:57
...

在res下创建一个anim包

透明、缩放、旋转、位移动画

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
        
            //透明动画
            case R.id.btn_alpha_animaction:
                Animation alpha = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_animaction);
                imageView.startAnimation(alpha);
                break;
            //旋转动画
            case R.id.btn_rotate_animaction:
                Animation rotate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_animaction);
                imageView.startAnimation(rotate);
                break;
            //缩放动画
            case R.id.btn_scale_animaction:
                Animation scale = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_animaction);
                imageView.startAnimation(scale);
                break;
            //位移动画
            case R.id.btn_translate_animaction:
                Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_animaction);
                translate.setFillAfter(true);
                imageView.startAnimation(translate);
                break;
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--透明动画-->
    <alpha
        android:duration="1000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--旋转动画-->
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="180"
        android:pivotX="50%"
        android:pivotY="50%"
        />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

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

    <!--位移动画-->
    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="200"
        android:toYDelta="0"
        />
</set>

动画合集

AnimactionSet set =new Animaction(true);
添加动画
set.addAnimaction();
set.addAnimaction();
set.addAnimaction();
set.addAnimaction();
执行动画
ImageView.startAnimaction(set);

布局动画

lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.list_anim));
                        //模式
                        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
                        recycley.setLayoutAnimation(lac);
                        //开启动画
                        recycley.startLayoutAnimation();
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--缩放-->
    <scale
        android:duration="500"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0"
        />
    <!--透明-->
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1.0"
        />
</set>

切换动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--从左到右进入-->
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0%p"
        android:duration="400"
        />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--从左到右退出-->
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="100%p"
        android:duration="400"
        />
</set>

逐帧动画
在drawable创建一个xml
android:oneshot="true"循环播放

<?xml version="1.0" encoding="utf-8"?>
<!--逐帧动画-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true"
    >
    <item android:drawable="@drawable/frame_animaction_1" android:duration="100"/>
    <item android:drawable="@drawable/frame_animaction_2" android:duration="100"/>
    <item android:drawable="@drawable/frame_animaction_3" android:duration="100"/>
</animation-list>
 AnimationDrawable drawable = (AnimationDrawable)getResources().getDrawable(R.drawable.frame_animaction);
 imageView.setImageDrawable(drawable);
 drawable.start();