基础篇:android动画效果之scale使用

来源:互联网 发布:手机淘宝装修软件 编辑:程序博客网 时间:2024/05/19 18:15

基础篇–android动画效果(一)

记录4种动画的具体使用

此篇文章包括alpha、scale、translate、rotate在xml中和代码中的使用


渐变动画
alpha
scale

画面转换动画
translate
rotate


scale缩放

xml使用

<scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"//动画的持续时间    android:fromXScale="0.5"//控件起始的缩放,当前为0.5倍,1.0代表正常大小    android:fromYScale="0.5"    android:interpolator="@android:anim/decelerate_interpolator"//加速减速插入器    android:pivotX="50%p"//控件起始的位置,50代表当前view的左上角再加50px为x轴起始点;50%代表当前view左上角加view的50%宽度为x轴起始点;50%p代表当前view左上角加父控件的50%宽度作为x轴的起始点    android:pivotY="50%p"//同理    android:toXScale="0.5"//当动画结束时,x轴方向相对原始的缩放比例,当前显示为0.5倍    android:toYScale="1.0" />

其他属性

  • android:fillAfter 在动画结束后,true为保持最后的状态
  • android:fillBefore 动画结束后,true为保持原始状态
  • android:fillEnabled 与fillBefore效果一样
  • android:repeatCount 动画效果的重复次数
  • android:repeatMode 动画重复类型(需要和repeatcount一起使用),reverse:动画回放效果;restart:动画从头开始播放
    android:repeatCount="5"    android:repeatMode="restart"

code使用

ScaleAnimation的源码

这里写图片描述

前4个参数为该view的缩放
第1、3个参数即代表fromXScale、fromYScale,第2,4个参数代表toXScale、toYScale。

后4个参数为该view的位移
第5、7个参数的pivotXType和pivotYType有多种类型

  • ScaleAnimation.RELATIVE_TO_PARENT:相对于父控件的,如果对应的pivotXValue的数值为0.1,则view的左上角位置在原始位置向左平移(0.1* 父控件的宽度)为x轴的初始位置,pivotXValue的数值为负数,则向右平移(0.1* 父控件的宽度)为x轴的初始位置。同理pivotYValue为正数,向上平移,为负数向下平移;(具体看效果和实践)
    animation_scale_code=new ScaleAnimation(2.0f,1.0f,2.0f,1.0f,ScaleAnimation.RELATIVE_TO_PARENT,0.1f,ScaleAnimation.RELATIVE_TO_PARENT,0.1f);    animation_scale_code.setDuration(2000);

这里写图片描述

  • ScaleAnimation.ABSOLUTE:绝对坐标,pivotXValue为正数,原始坐标向左平移,反之向右;pivotYValue为正数,坐标向上平移,反之向下。
animation_scale_code=new ScaleAnimation(2.0f,1.0f,2.0f,1.0f,ScaleAnimation.ABSOLUTE,-200f,ScaleAnimation.ABSOLUTE,200f);

这里写图片描述

  • ScaleAnimation.RELATIVE_TO_SELF:相对于自己,pivotXValue为正数,向左平移(pivotXValue*自身宽度),反之向右;pivotYValue为正数,向上平移(pivotYValue*自身高度),反之向下。
animation_scale_code=new ScaleAnimation(2.0f,1.0f,2.0f,1.0f,ScaleAnimation.RELATIVE_TO_SELF,1f,ScaleAnimation.RELATIVE_TO_SELF,1f);

这里写图片描述

0 0