Android 中 动画效果实现
16lz
2021-01-23
Android中提供了四种动画效果:
AlphaAnimation,
RotateAnimation,
ScaleAnimation,
TranslateAnimation
- Alpha 透明度改变的动画效果:
AlphaAnimation(float fromAlpha, float toAlpha);这个很好理解,两个参数一个是起始透明度,一个是结束透明度
float类型参数 由0.0 --> 1.0, 透明度由完全透明-->完全不透明
之后还要设置动画持续时间: setDuration(long durationMillis);
long类型参数为毫秒数。时间越长,动画也就越慢了~
- Rotate 旋转动画效果:
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
float fromDegrees, 起始角度
float toDegrees, 结束角度
特别说明:
fromDegrees < toDegrees 顺时针转,反之,逆时针。
int pivotXType, 动画在X轴相对于物件位置类型,与下面的pivotXValue结合,确定X轴上旋转中心。
float pivotXValue,结合上面说明:如果pivotXType=Animation.ABSOLUTE,则此参数为旋转中心在屏幕上X轴的值;
int pivotYType, Y轴同上,不解释
float pivotYValue),Y轴同上,不解释
举例:
如果pivotXType=Animation.RELATIVE_TO_PARENT,则参数pivotXValue为旋转中心在父控件水平位置百分比,如0.5表示在父控件水平方向中间位置;
如果pivotXType=Animation.RELATIVE_TO_SELF,则参数pivotXValue为旋转中心在控件自身水平位置百分比,如果X和Y的Value都设置为0.5,则该控件以自身中心旋转。
- Scale 缩放动画效果:
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);四个from,to 是起始和结束时控件大小比例。如fromX=0.5, toX=1,则控件的水平方向从一半大小变到控件本身大小。
intpivotXType, float pivotXValue和RotateAnimation中代表的意思差不多,就是指明了缩放是从哪里开始的。
举例:
设 置 p ivotXType= Animation. RELATIVE_TO_SELF , pivotXVal ue=0.0,pivotYType=Animation.RELATIVE_TO_SELF,pivotYValue=0.5,缩放就从 控件的垂直方向中部以及水平方向左边开始了。当然如果fromX=0,toX=0,那么水平方向上是没变化的。
- Translate 移动动画效果:
其中
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);四个参数分别是起始和结束时的水平、垂直方向位置。注意:默认是以控件本身问参照的,比如设置为(0,0,100,50)是在水平方向移动100px,垂直方向移动50px;
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue);与前面几个效果的参数含义相似,fromXType类似上面的pivotXType,是参照类型,fromXValue就是那pivotXValue了,是具体值。
举例:设置(Animation.RELATIVE_TO_PARENT, 0.5,RELATIVE_TO_PARENT, 1.0,RELATIVE_TO_PARENT, 0.0,RELATIVE_TO_PARENT, 1.0); 则表示从父控件水平居中、垂直最上方移动到父控件的右下方,由于设置的是该控件左上顶点的位置,所以此时该控件应该是出去父控件了(这个我还没亲自试过,偷懒了。。大家可以拿来试试,欢迎反馈~)。
最后补充一下:当pivotXType 为Animation.ABSOLUTE时,后面的Value代表的是px值,其他情况Value代表比例,1.0=100%。
更多相关文章
- Android 获取控件高度宽度三种方法,防止0的出现
- [android]组件和控件的区别
- 【Android】自定义控件系列之TextView设置Drawable的大小
- android获取控件的几种方法
- Android简单的计算控件使用
- Android使用控件Spinner实现下拉菜单列表
- Android 实现一个http 带参数登录
- android 自定义水平的ProgressBar
- Android 控件最上层显示方法