android之Tween Animation
阅读原文时间:2023年07月08日阅读:1

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


out.xml


其中,out.xml中的zAdjustment属性为z方向的层级,有top、nomal、bottom三个属性,若设置成top,当画面重叠时,处于上层,遮挡其它界面;而bottom则相反,将会被遮挡。

PS:Tween Animation中,组件的真实位置和显示位置不一定相同,因此点击显示位置未必会触发事件。需要根据需求设置初始和结束的相关属性,如果无法满足需求,可考虑使用属性动画。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章