Android动画学习之补间动画

Android动画学习笔记之补间动画

首先看看补间动画的共同属性:

Duration:动画持续的时间(单位:毫秒)   
fillAfter:设置为true,动画转化在动画被结束后被应用  
fillBefore:设置为true,动画转化在动画开始前被应用  
interpolator:动画插入器(加速、减速插入器)  
repeatCount:动画重复的次数  
repeatMode:顺序动画(restart)/倒序动画(reverse)  
startOffset:动画之间时间间隔  

对于动画的创建一般有两种方式:

第一种是在res/新建一个anim文件夹,然后在其下面分别建立四种动画  
第二种方式是通过java代码的方式创建  

在补间动画中我们通常有以下四种动画:

  1. 位移动画
    创建方式一:
    在anim文件下新建一个translate资源文件
<?xml version="1.0" encoding="utf-8"?>
        <translate xmlns:android="http://schemas.android.com/apk/res/android"

        android:fromXDelta="0" //设置从x的哪个点起,也可以设置为百分比,以控件的宽为基准
        android:toXDelta="100" //设置移动到目标x点
        android:fromYDelta="0" //设置从y的哪个点起,也可以设置为百分比,以控件的高为基准
        android:toYDelta="0" //设置移动到目标y点
        android:repeatCount="2" //动画重复两次,实际上你会发现是重复3次  
                                                    //(restart模式下执行相同方向3次)
                                //这里的意思是,在首次执行完之后再重复2次
                                //所以总的执行次数为 n + 1次
                                //如果是reverse模式下,则反序移动也算一次,所以在反序模式下
                                //往同一方向只有两次,加上反序的一次刚好就是3次

        android:repeatMode="restart" //沿当前方向顺序移动一次,没有反序移动,
                                    //如果设置为reverse则有一次顺序/反序移动的操作,
        android:duration="2000" //完成该动作需要2秒
        >
        </translate>

        //通过以下代码注册该动画
         private void creatTranslateByInflate(){
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
            mCircle.startAnimation(animation);
        }
创建方式二:(代码创建)
private void creatTranslateByCode() {
            TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0);
            animation.setDuration(2000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(2);
            mCircle.startAnimation(animation);
        }
  1. 旋转动画
    创建方式一:
<?xml version="1.0" encoding="utf-8"?>
        <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="0"//起始的角度数,
            android:toDegrees="180"//终止的角度,顺时针(起始度 < 终止的角度) 逆时针
                                                        //(起始度 > 终止的角度)
                                    //这里就是顺时针的旋转180
            android:pivotX="50%" //旋转中轴的x点,50%表示以控件宽为基准,在控件的中间x点
            android:pivotY="50%" //旋转中轴的y点,50%表示以控件高为基准,在控件的中间y点
            android:duration="2000" 
            android:repeatMode="reverse"
            android:repeatCount="2"

        >
        </rotate>

        //通过以下代码注册该动画
        private void createRotateByInflate(){
                Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
                mCircle.startAnimation(animation);
        }
创建方式二:  (代码创建)
private void createRotateByCode() {
            float pivotX = mCircle.getWidth() / 2.0f;
            float pivotY = mCircle.getHeight() / 2.0f;
            RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY);
            animation.setDuration(2000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(2);
            mCircle.startAnimation(animation);
        }
  1. 缩放动画

    创建方式一:

<?xml version="1.0" encoding="utf-8"?>
        <scale xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromXScale="0.1"//
            android:toXScale="1.0" //
            android:fromYScale="0.1"
            android:toYScale="1.0"
            android:pivotY="50%"
            android:pivotX="50%"
            android:duration="2000"
            android:repeatMode="reverse"
            android:repeatCount="2">
        </scale>

        //通过以下代码注册该动画
        private void createScaleByInflate(){
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
            mCircle.startAnimation(animation);
        }
创建方式二:(代码创建)
private void createScaleByCode() {
            //创建动画Animation.RELATIVE_TO_PARENT 以父容器为参照物 
            //Animation.RELATIVE_TO_SELF 以自己为参照物, 如果以父容器为参照物会导致控件移动
            float pivotX = mCircle.getWidth() / 2.0f;
            float pivotY = mCircle.getHeight() / 2.0f;
            ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY);
            animation.setDuration(2000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(2);
            mCircle.startAnimation(animation);
        }
  1. 渐变动画

    创建方式一:

<?xml version="1.0" encoding="utf-8"?>
        <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromAlpha="0.1"
            android:toAlpha="1.0"
            android:repeatMode="reverse"
            android:repeatCount="2"
            android:duration="2000">
        </alpha>

        //通过以下代码注册该动画
        private void createAlphaByInflate(){
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
            mCircle.startAnimation(animation);
        }
创建方式二:(代码创建)

        private void createAlphaByCode() {
            AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
            animation.setDuration(2000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(2);
            mCircle.startAnimation(animation);
        }

以上的四种可以单独使用也可以结合起来使用,如果要结合起来使用的话,直接在anim文件夹下创建set集合,然后将需要结合的动画
放在一起即可

如下示例:

<?xml version="1.0" encoding="UTF-8"?>

        <set xmlns:android="http://schemas.android.com/apk/res/android">

            <rotate android:toDegrees="3600" android:pivotY="50%" 
            android:pivotX="50%" android:fromDegrees="0"
           android:duration="2000"/>

            <translate android:duration="3000" android:toYDelta="100%" 
            android:toXDelta="150%"   
            android:fromYDelta="-150%" android:fromXDelta="-100%"/>

            <alpha android:duration="5000" android:toAlpha="1.0" 
            android:repeatMode="reverse" 
            android:repeatCount="3" android:fromAlpha="0.5"/>

        </set>

基本的创建方式,以及基本属性都在这里了,至于如何实现一个具有美感的效果图,那就看个人的设计感了。

最后看看运行结果图
这里写图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值