View动画
16lz
2021-12-04
一.效果图
二.使用 xml 文件实现方式
通用属性:
- android:duration 动画持续时间,以毫秒为单位
- android:fillAfter 如果设置为true,控件动画结束时,将保持动画最后时的状态
- android:fillBefore 如果设置为true,控件动画结束时,还原到开始动画前的状态
- android:fillEnabled 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
- android:repeatCount 重复次数
- android:repeatMode 重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
- android:interpolator 设定插值器,其实就是指定的动作效果,比如弹跳效果等
-
渐变
自身属性:
- android:fromAlpha 动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
- android:toAlpha 动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
示例:
anim 文件:
<?xml version="1.0" encoding="utf-8"?>
调用:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_alpha); animation.setFillAfter(true);//设置保持动画后的状态 mIvIcon.startAnimation(animation);
- 平移
自身属性:
- android:fromXDelta 起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
- android:fromYDelta 起始点Y轴从标,可以是数值、百分数、百分数p 三种样式;
- android:toXDelta 结束点X轴坐标
- android:toYDelta 结束点Y轴坐标
示例:
<?xml version="1.0" encoding="utf-8"?>
- 缩放
自身属性:
- android:fromXScale 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
- android:toXScale 结尾的X方向上相对自身的缩放比例,浮点值;
- android:fromYScale 起始的Y方向上相对自身的缩放比例,浮点值,
- android:toYScale 结尾的Y方向上相对自身的缩放比例,浮点值;
- android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。
- android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。
示例:
<?xml version="1.0" encoding="utf-8"?>
-
旋转
自身属性:
- android:fromDegrees 开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
- android:toDegrees 结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
- android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p
- android:pivotY 缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p
示例:
<?xml version="1.0" encoding="utf-8"?>
-
动画集合
示例:
<?xml version="1.0" encoding="utf-8"?>
三.使用代码实现
- 渐变
//声明一个渐变动画, 参数1代表起始透明度,参数2代表结束透明度 //1代表完全不透明,0是完全透明AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f); //设置持续时间 alphaAnimation.setDuration(2000); //设置动画后的状态 alphaAnimation.setFillAfter(true); //设置重复次数 alphaAnimation.setRepeatCount(2); //设置重复模式 alphaAnimation.setRepeatMode(Animation.REVERSE); //设置动画监听 alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); mIvIcon.startAnimation(alphaAnimation);
- 平移
//(int fromXType, 表示X的起始值类型,该类型指定相对长度的类型//Animation.RELATIVE_TO_PARENT 相对于父亲的长度//Animation.RELATIVE_TO_SELF 相对于自身的长度//Animation.ABSOLUTE 表示绝对类型,传入长度的绝对值//float fromXValue, 表示的是动画开始的时候,X的起始位置 (值由类型确定)////int toXType, 表示X的结束值类型,该类型指定相对长度的类型//float toXValue,表示的是动画结束的时候,X的结束位置 (值由类型确定)//// y的起始类型, y的起始值 y的结束类型, y的结束值 //int fromYType, float fromYValue, int toYType, float toYValueTranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f , Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 1f); translateAnimation.setDuration(2000); translateAnimation.setFillAfter(true); mIvIcon.startAnimation(translateAnimation);
- 缩放
//float fromX, 起始X的大小, 值为自身长度的倍数////float toX, 结束X的大小, 值为自身长度的倍数//float fromY,//float toY,//int pivotXType, 缩放中心点的X位置的类型, 相对长度类型//float pivotXValue, 圆心的X的值 (值由类型确定) ,相对长度类型//int pivotYType, float pivotYValue ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f); scaleAnimation.setFillAfter(true); scaleAnimation.setDuration(2000); mIvIcon.startAnimation(scaleAnimation);
- 旋转
//申明一个旋转动画 参数1:起始角度 参数2:旋转的度数 起始值小于结束值 顺时针, 大于 逆时针//RotateAnimation rotateAnimation = new RotateAnimation(-180, -90);//RotateAnimation rotateAnimation = new RotateAnimation(0, 180, 150, 0);//float fromDegrees,起始角度 //float toDegrees, 结束角度 起始值小于结束值 顺时针旋转, 大于 逆时针旋转//int pivotXType, 表示圆心X位置的相对类型, 相对长度类型////float pivotXValue, 圆心的X的值 (值由类型确定)////int pivotYType, float pivotYValue //设置旋转中心在图片的中心 RotateAnimation rotateAnimation = new RotateAnimation(0, 180 ,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); mIvIcon.startAnimation(rotateAnimation);
- 集合
//先声明一个动画集合 AnimationSet animationSet = new AnimationSet(true); //声明需要的动画 RotateAnimation rotateAnimation = new RotateAnimation(0, 180 ,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f); //将每个动画添加进动画集合 animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(3000); animationSet.setFillAfter(true); mIvIcon.startAnimation(animationSet);
更多相关文章
- Android(安卓)的相关文件类型
- Android(安卓)支持多屏幕机制
- android sqlite 数据类型
- 界面编程之基本界面组件(7)ImageView(图像视图)
- Day3.4--Android简单UI控件之ImageView以及ScaleType的使用
- 设置提示信息,输入值类型,输入框引入图片,设置输入框的形状
- [Android] ContentProvider实例详解
- Android(安卓)支持多屏幕机制
- Android多分辨率适配框架使用指南