不断学习,做更好的自己!💪
视频号 | ||
欢迎打开微信,关注我的视频号:KevinDev | | |
前言
Android中动画分为3种:
- Tween Animation(补间动画):通过对场景的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画。
- Frame Animation(逐帧动画):顺序播放事先做好的图像,是一种画面转换动画。
- Property Animation(属性动画):通过动态地改变对象的属性从而达到动画效果,属性动画为 API 11 新特性。
原理
- 平移动画(Translate)
- 缩放动画(scale)
- 旋转动画(rotate)
- 透明度动画(alpha)
使用
1. 平移动画(Translate)
- 动画效果 xml 文件,/res/anim/translate_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset ="1000"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "true"
android:repeatMode= "restart"
android:repeatCount = "0"
android:fromXDelta="0"
android:toXDelta="500"
android:fromYDelta="0"
android:toYDelta="500" />
- 代码
public class MainActivity extends AppCompatActivity {
private TextView mAnim;
private Button mPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
// 方式二:通过 TranslateAnimation 实现
Animation animation = new TranslateAnimation(0,500,0,500);
animation.setDuration(3000);
mAnim.startAnimation(animation);
}
private void initView() {
mAnim = findViewById(R.id.tv_anim);
mPlay = findViewById(R.id.btn_play);
}
private void initEvent() {
mPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 方式一:通过加载 xml 文件实现
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_anim);
mAnim.startAnimation(animation);
}
});
}
}
2. 缩放动画(Scale)
- 动画效果 xml 文件,/res/anim/scale_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset ="1000"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "true"
android:repeatMode= "restart"
android:repeatCount = "0"
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"/>
- 代码
public class MainActivity extends AppCompatActivity {
private TextView mAnim;
private Button mPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
// 方式二:通过 ScaleAnimation 实现
Animation animation = new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);
mAnim.startAnimation(animation);
}
private void initView() {
mAnim = findViewById(R.id.tv_anim);
mPlay = findViewById(R.id.btn_play);
}
private void initEvent() {
mPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 方式一:通过加载 xml 文件实现
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);
mAnim.startAnimation(animation);
}
});
}
}
3.旋转动画(Rotate)
- 动画效果 xml 文件,/res/anim/rotate_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="+360"
android:repeatCount="10"
android:repeatMode="restart"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/overshoot_interpolator" />
- 代码
public class MainActivity extends AppCompatActivity {
private TextView mAnim;
private Button mPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
// 方式二:通过 RotateAnimation 实现
Animation animation = new RotateAnimation(0,270,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);
mAnim.startAnimation(animation);
}
private void initView() {
mAnim = findViewById(R.id.tv_anim);
mPlay = findViewById(R.id.btn_play);
}
private void initEvent() {
mPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 方式一:通过加载 xml 文件实现
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_anim);
mAnim.startAnimation(animation);
}
});
}
}
4.透明度动画(Alpha)
- 动画效果 xml 文件,/res/anim/alpha_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset ="1000"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "true"
android:repeatMode= "restart"
android:repeatCount = "0"
android:interpolator="@android:anim/overshoot_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
- 代码
public class MainActivity extends AppCompatActivity {
private TextView mAnim;
private Button mPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
// 方式二:通过 AlphaAnimation 实现
Animation animation = new AlphaAnimation(1,0);
animation.setDuration(3000);
mAnim.startAnimation(animation);
}
private void initView() {
mAnim = findViewById(R.id.tv_anim);
mPlay = findViewById(R.id.btn_play);
}
private void initEvent() {
mPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 方式一:通过加载 xml 文件实现
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_anim);
mAnim.startAnimation(animation);
}
});
}
}
5. 组合动画
- 动画效果 xml 文件,/res/anim/combination_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset ="1000"
android:fillBefore = "true"
android:fillAfter = "false"
android:fillEnabled= "true"
android:repeatMode= "restart"
android:repeatCount = "0"
android:interpolator="@android:anim/overshoot_interpolator"
>
<!--设置旋转动画,语法同单个动画-->
<rotate
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:repeatCount="infinite"
/>
<!--设置平移动画,语法同单个动画-->
<translate
android:duration="10000"
android:startOffset = "1000"
android:fromXDelta="-50%p"
android:fromYDelta="0"
android:toXDelta="50%p"
android:toYDelta="0" />
<!--设置透明度动画,语法同单个动画-->
<alpha
android:startOffset="7000"
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
<!--设置缩放动画,语法同单个动画-->
<scale
android:startOffset="4000"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5" />
</set>
- 代码
public class MainActivity extends AppCompatActivity {
private TextView mAnim;
private Button mPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
// 方式二:通过 AnimationSet 实现
AnimationSet animation = new AnimationSet(true);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(1);
//子动画1: 旋转动画
Animation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
// 子动画2:平移动画
Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, -0.5f,TranslateAnimation.RELATIVE_TO_PARENT,
0.5f,TranslateAnimation.RELATIVE_TO_SELF,0,TranslateAnimation.RELATIVE_TO_SELF,0);
translate.setDuration(10000);
// 子动画3:透明度动画
Animation alpha = new AlphaAnimation(1,0);
alpha.setDuration(3000);
alpha.setStartOffset(7000);
// 子动画4:缩放动画
Animation scale = new ScaleAnimation(1,0.5f,1,0.5f,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scale.setDuration(1000);
scale.setStartOffset(4000);
animation.addAnimation(rotate);
animation.addAnimation(translate);
animation.addAnimation(alpha);
animation.addAnimation(scale);
mAnim.startAnimation(animation);
}
private void initView() {
mAnim = findViewById(R.id.tv_anim);
mPlay = findViewById(R.id.btn_play);
}
private void initEvent() {
mPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 方式一:通过加载 xml 文件实现
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.combination_anim);
mAnim.startAnimation(animation);
}
});
}
}