android 动画

总结android的动画的使用。

分类

android动画总的来说分成2个类别:

  • Tweened Animation ,该类提供了旋转,移动,缩放,和淡入淡出等效果。
  • Frame-by-Frame Animation,也就是帧动画,这个类可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个个的显示。(下一篇将着重实现)

实现

接下来着重讲第一类的动画,也就是AlphaAnimaton,RotateAnimaton,ScaleAnimaton以及TranslateAnimaton。这4类动画在具体实现时都有两种方法,分别是代码中实现和通过在xml文件中定义代码中引入。

在代码中实现时具体的代码片段:`

   // 创建一个AnimationSet对象  true代表使用annimation自己的interpolater,false是使用自己的
       AnimationSet animationSet1 = new AnimationSet(true);
   //创建对应的动画
   //参数1:从哪个旋转角度开始
   //参数2:转到什么角度
   //后4个参数用于设置围绕着旋转的圆的圆心在哪里
   //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
   //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
   //参数5:确定y轴坐标的类型
   //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
      RotateAnimation rAnimation = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
                rAnimation.setDuration(1000);
                animationSet1.addAnimation(rAnimation);
      mImage.startAnimation(animationSet1);
   AnimationSet animationSet3 = new AnimationSet(true);
                //参数1:x轴的初始值
                //参数2:x轴收缩后的值
                //参数3:y轴的初始值
                //参数4:y轴收缩后的值
                //参数5:确定x轴坐标的类型
                //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
                //参数7:确定y轴坐标的类型
                //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
                ScaleAnimation scaleAnimation = new ScaleAnimation(
                        0, 0.1f,0,0.1f,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                scaleAnimation.setDuration(1000);
                animationSet3.addAnimation(scaleAnimation);
                mImage.startAnimation(animationSet3);

当然也可以是两个动画同时使用。但是目前执行效果看是,当第一个动画走完之后,会紧接着运行第二个动画时是从第一个动画的结束位置开始的。

  AnimationSet animationSet = new AnimationSet(true);
                //创建对应的动画
                TranslateAnimation tAnimation = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                tAnimation.setDuration(1000);
                tAnimation.setFillBefore(true);
                //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
                AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                //设置动画执行的时间
                alphaAnimation.setDuration(5000);
                animationSet.addAnimation(tAnimation);
                animationSet.addAnimation(alphaAnimation);
                mImage.startAnimation(animationSet);

在xml中实现步骤:

  1. 在res目录下建立一个anim的文件夹
  2. 创建xml文件,并首先加入set标签
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="1500"/>
</set>
  rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
        fromDegrees:开始的角度
        toDegrees:结束的角度,+表示是正的
        pivotX:用于设置旋转时的x轴坐标
        例
           1)当值为"50",表示使用绝对位置定位
           2)当值为"50%",表示使用相对于控件本身定位
           3)当值为"50%p",表示使用相对于控件的父控件定位
        pivotY:用于设置旋转时的y轴坐标
      -->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"/>
</set>
  scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
   <!--
       起始x轴坐标
           止x轴坐标
           始y轴坐标
           止y轴坐标
           轴的坐标
           轴的坐标
     -->
   <scale
       android:fromXScale="1.0"
       android:toXScale="0.0"
       android:fromYScale="1.0"
       android:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="1000"/>
</set>

  translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
           始x轴坐标
           止x轴坐标
           始y轴坐标
           止y轴坐标
      -->
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>
</set>

3 在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象

       Animation animation = AnimationUtils.loadAnimation(
         MainActivity.this, R.anim.animation);
        // 启动动画
        mImage.startAnimation(animation);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值