安卓动画实现

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

取消自带动画效果

    <item name="android:windowAnimationStyle">@style/NoAnimation</item>
    
  
    <style name="NoAnimation">
        <item name="android:activityOpenEnterAnimation">@null</item>
        <item name="android:activityOpenExitAnimation">@null</item>
        <item name="android:activityCloseEnterAnimation">@null</item>
        <item name="android:activityCloseExitAnimation">@null</item>
    </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值