android之Tween Animation
2024-10-19 07:10:49
android Tween Animation有四种,AlphaAnimation(透明度动画)、ScaleAnimation(尺寸伸缩动画)、TranslateAnimation(位移动画)、RotateAnimation(旋转动画)。
1、先来展示这四种动画通用的属性:
android:duration="2000" //动画播放时长(毫秒)
android:fillBefore="true" //动画播放完毕后是否显示第一帧
android:fillAfter="false" //动画播放完毕后是否显示最后一帧
android:repeatCount="infinite" //动画重复次数。infinite或负数时无限循环
android:repeatMode="reverse" //restart为正向重复,reverse为反向重复
android:startOffset="2000" //延迟播放时间(毫秒)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" //插补器,控制播放速率变化
在java代码中开启动画
ImageView animIv= (ImageView) findViewById(R.id.id_anim_iv);
Animation animation =AnimationUtils.loadAnimation(this, R.anim.set); //加载动画
animIv.startAnimation(animation); //开启动画
2、Alpha
android:fromAlpha="0.1" //初始透明度
android:toAlpha="1.0" //结束透明度
0为完全透明,1为不透明。
3、Scale
android:fromXScale="0.0" //横向初始比例
android:fromYScale="1.0" //纵向初始比例
android:pivotX="50%" //横向缩放中心点
android:pivotY="50%" //纵向缩放中心点
android:toXScale="1.0" //横向结束比例
android:toYScale="1.0" //纵向结束比例
0为不显示,1为正常比例。
4、Translate
android:fromXDelta="10" //初始X坐标
android:fromYDelta="10" //初始Y坐标
android:toXDelta="100" // 结束X坐标
android:toYDelta="100" //结束Y坐标
坐标为相对坐标,单位为px。
5、Rotate
android:fromDegrees="0" //初始角度
android:toDegrees="+359" //结束角度
android:pivotX="50%" //横向旋转中心
android:pivotY="50%" //纵向旋转中心
重复旋转时匀速转动,设置线性插补器
animation.setInterpolator(new LinearInterpolator());
6、动画监听
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
7、我们经常在一些App上看到一些Activity之间跳转的动画效果,其实我们可以用Tween动画来实现。
Intent intent=new Intent(MainActivity.this,SecActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in, R.anim.out);
在overridePendingTransition(R.anim.in, R.anim.out);中,两个参数分别为SecActivity显示动画和MainActivity消失动画。
下面两个动画实现向右平移的效果:
in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="-100%"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0" />
</set>
out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:zAdjustment="bottom">
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="0" />
</set>
其中,out.xml中的zAdjustment属性为z方向的层级,有top、nomal、bottom三个属性,若设置成top,当画面重叠时,处于上层,遮挡其它界面;而bottom则相反,将会被遮挡。
PS:Tween Animation中,组件的真实位置和显示位置不一定相同,因此点击显示位置未必会触发事件。需要根据需求设置初始和结束的相关属性,如果无法满足需求,可考虑使用属性动画。
最新文章
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
- ASP.NET - Web API,从简单类型到复杂类型的参数传递用例,以及传递简单string类型的解决办法
- 修复 OS X 的系统盘出现 Invalid Node Structure 问题
- 总结common-dbutils.jar
- IntelliJ IDEA 在网页修改数据,但是在浏览器刷新的时候,不能读取到修改之后的数据
- 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions
- 从github下载某个git库的4种方法
- UWP深入学习四:动画及图像
- CodeForces 483C Diverse Permutation
- 【SQL Server】系统学习之三:逻辑查询处理阶段-六段式
- Python 结巴分词(2)关键字提取
- MySQL5.6-Tomcat7环境变量的配置
- mvc4中的过滤器
- 使用 Router 实现的模块化,如何优雅的回到主页面
- 第06周-接口、内部类与Swing
- spring data jpa 学习笔记
- artTemplate使用
- p86商空间也是Banach空间
- Zabbix appliance manual
- PAT 1117 Eddington Number [难]