Android 动画的使用,以及组合动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37794706/article/details/84790162

点击下载demo

效果图:

                          

一、Android 四种类型的animation

javaCode动画类型 xml动画类型 描述 属性
AlphaAnimation alph 渐变透明

浮点型值:

fromAlpha:动画起始时透明度

toAlpha:动画结束时透明度

duration:动画持续时间

说明:

     0.0表示完全透明

     1.0表示完全不透明

扫描二维码关注公众号,回复: 5429298 查看本文章

     以上取值0.0-1.0之间的float数据类型的数字

duration:    

      为长整形,时间单位为毫秒

ScaleAnimation scale 渐变尺寸伸缩

iinterpolator:指定一个动画的插入器

我这里列出三种动画插入器:(可自行测试一下)

      accelerate_decelerate_interpolator  加速-减速

      accelerate_interpolator                     加速

      decelerate_interpolator                     减速

fromXScale:动画起始时,x坐标上的伸缩尺寸

toXScale:动画结束时,x坐标上的伸缩尺寸

fromYScale:动画起始时Y坐标上的伸缩尺寸

toYScale:动画结束时,Y坐标上的伸缩尺寸

说明:

       以上四个是数据浮点型数值,float

        0.0表示收缩到没有

        1.0表示正常无伸缩

         值<1.0表示收缩

         值>1.0表示放大

pivotX:动画相对于物件的x坐标的开始位置

pivotY:动画相对于物件的Y坐标的开始位置

说明:

       以上两个属性值  :从0%-100%中取值

      50%为物件的X或Y方向坐标上的中点位置

duration:动画持续时间(长整型数值,单位毫秒)

fillAfter:当设置为true,该动画转化在动画结束后被应用

  

TranslateAnimation transtate 转换位置移动

fromXDelta:动画起始时,X坐标上的位置

toXDelta:动画结束时,X坐标上的位置

fromYDelta:动画起始时,Y坐标上的位置

toYDelta:动画结束时,Y坐标上的位置

duration:动画持续时间,时间以毫秒为单位

说明:

    没有指定fromXType,toXType,fromYType,toYType时        候,默认是以自己为为相对参照物

RotateAnimation rotate 画面旋转

interpolator:指定一个动画的插入器

        我这里列出三种(可自行测试)

       accelerate_decelerate_interpolator:加速-减速

       accelerate_interpolator:加速

       decelerate_interpolator:减速

fromDegrees:动画起始时物件的角度

toDegrees:动画结束时物件旋转的角度

说明:

       当角度为负数——表示逆时针旋转

       当角度为正数——表示顺时针旋转

       [负数from——to正数:顺时针旋转]

       [负数from——to负数:逆时针旋转]

       [正数from——to正数:顺时针旋转]

       [正数from——to负数:逆时针旋转]

pivotX:动画相对于物件的X坐标的开始的位置

pivotY:动画相对于物件的Y坐标的开始位置

说明:

       以上两个属性值:从0%——100%中取值

      50%为物件的X或Y方向坐标上的中点位置

duration:动画持续时间,单位为 

2.在javaCode中

二、在xml中的使用:在res下新建一个anim文件夹,创建对应的xml动画文件

1.alpha.xml(渐变透明)

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

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />


    <!--浮点型值:
 fromAlpha:动画起始时透明度
 toAlpha:动画结束时透明度
 duration:动画持续时间
 说明:
      0.0表示完全透明
      1.0表示完全不透明
      以上取值0.0-1.0之间的float数据类型的数字
 duration:    
       为长整形,时间单位为毫秒-->

</set>

2.rotate.xml

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

    <!--画面转移旋转动画效果-->

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+350" />

    <!--
        rotate 旋转动画效果
          属性:interpolator 指定一个动画的插入器
                在我试验过程中,使用android.res.anim中的资源时候发现
                有三种动画插入器:
                   accelerate_decelerate_interpolator   加速-减速 动画插入器
                   accelerate_interpolator               加速-动画插入器
                   decelerate_interpolator               减速- 动画插入器
                其他的属于特定的动画效果

          浮点数型值:
               fromDegrees 属性为动画起始时物件的角度
               toDegrees   属性为动画结束时物件旋转的角度 可以大于360度


               说明:
                        当角度为负数——表示逆时针旋转
                        当角度为正数——表示顺时针旋转
                        (负数from——to正数:顺时针旋转)
                        (负数from——to负数:逆时针旋转)
                        (正数from——to正数:顺时针旋转)
                        (正数from——to负数:逆时针旋转)

               pivotX     属性为动画相对于物件的X坐标的开始位置
               pivotY     属性为动画相对于物件的Y坐标的开始位置

               说明:        以上两个属性值 从0%-100%中取值
                            50%为物件的X或Y方向坐标上的中点位置

           长整型值:
               duration  属性为动画持续时间
               说明:       时间以毫秒为单位

       -->
</set>

​

3.scale.xml

​
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--渐变尺寸伸缩动画-->

    <scale
        android:duration="3000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <!--
         尺寸伸缩动画效果 scale
           属性:interpolator 指定一个动画的插入器
            在我试验过程中,使用android.res.anim中的资源时候发现
            有三种动画插入器:
                accelerate_decelerate_interpolator  加速-减速 动画插入器
                accelerate_interpolator        加速-动画插入器
                decelerate_interpolator        减速- 动画插入器
            其他的属于特定的动画效果
          浮点型值:

                fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
                toXScale   属性为动画结束时 X坐标上的伸缩尺寸

                fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
                toYScale   属性为动画结束时Y坐标上的伸缩尺寸

                说明:
                     以上四种属性值

                        0.0表示收缩到没有
                        1.0表示正常无伸缩
                        值小于1.0表示收缩
                        值大于1.0表示放大

                pivotX     属性为动画相对于物件的X坐标的开始位置
                pivotY     属性为动画相对于物件的Y坐标的开始位置

                说明:
                        以上两个属性值 从0%-100%中取值
                        50%为物件的X或Y方向坐标上的中点位置

            长整型值:
                duration  属性为动画持续时间
                说明:   时间以毫秒为单位

            布尔型值:
                fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用

    -->

</set>

​

4.translate.xml

​
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--位置移动动画效果-->

    <translate
        android:duration="3000"
        android:fromXDelta="30"
        android:fromYDelta="30"
        android:toXDelta="-80"
        android:toYDelta="300" />
    <!--
         translate 位置转移动画效果
            整型值:
                fromXDelta 属性为动画起始时 X坐标上的位置
                toXDelta   属性为动画结束时 X坐标上的位置
                fromYDelta 属性为动画起始时 Y坐标上的位置
                toYDelta   属性为动画结束时 Y坐标上的位置
                注意:
                         没有指定fromXType toXType fromYType toYType 时候,
                         默认是以自己为相对参照物
            长整型值:
                duration  属性为动画持续时间
                说明:   时间以毫秒为单位



        -->


</set>

​

xml中动画效果的使用方式:

  alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
  heartImg.startAnimation(alphaAnimation);
  alphaAnimation.setAnimationListener(this);

三、在javaCode中的使用

1.AlphaAniamtion(渐变透明)

//第一个参数:动画开始时透明度,第二个参数:动画结束时候透明
 alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
 alphaAnimation.setDuration(3000);//设置时间
 heartImg.startAnimation(alphaAnimation);//启动动画
 alphaAnimation.setAnimationListener(this);//设置监听

2.ScaleAnimation(尺寸伸缩)



//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸    
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸     
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸    
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸  
/*说明:
                    以上四种属性值    
                    0.0表示收缩到没有 
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型  
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型   
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置

scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);//设置动画时间
heartImg.startAnimation(scaleAnimation);//启动动画
scaleAnimation.setAnimationListener(this);//设置监听

3.TranslateAnimation(位置移动)

//第一个参数fromXDelta为动画起始时 X坐标上的移动位置    
//第二个参数toXDelta为动画结束时 X坐标上的移动位置      
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置     
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
translateAnimation = new TranslateAnimation(30, -80, 30, 300);
translateAnimation.setDuration(3000);//设置动画持续时间
heartImg.startAnimation(translateAnimation);//启动动画
translateAnimation.setAnimationListener(this);//设置动画监听事件

4.RotateAnimation(旋转动画)

//第一个参数fromDegrees为动画起始时的旋转角度    
//第二个参数toDegrees为动画旋转到的角度   
//第三个参数pivotXType为动画在X轴相对于物件位置类型  
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型   
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置

rotateAnimation = new RotateAnimation(0, +350.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5F);
rotateAnimation.setDuration(3000);//设置动画持续时间
heartImg.startAnimation(rotateAnimation);//启动动画
rotateAnimation.setAnimationListener(this);//设置动画监听

四:组合动画

1.xml中组合动画(这里就只写一个简单了)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="2000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

2.javaCode中组合动画,

主要使用到AnimationSet这个对象

 animationSet = new AnimationSet(false);
                Animation alphAnimation = new AlphaAnimation(1.0f, 0.0f);
                Animation scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(2000);
                alphAnimation.setDuration(2000);
                animationSet.addAnimation(alphAnimation);
                animationSet.addAnimation(scaleAnimation);
                imageView.startAnimation(animationSet);

如有不对之处,还望各位大神指导,小女子在此谢过了!

猜你喜欢

转载自blog.csdn.net/m0_37794706/article/details/84790162