完成动画步骤:
- 创建
AnimationSet
对象 - 根据需要创建相应的
Animation
对象 - 根据对动画的需求,为
Animation
对象设置相应的数据 - 将
Animation
对象添加到AnimationSet
对象中 - 使用控件对象开始执行
AnimationSet
代码实现动画效果
1、设置xml
2、编写java文件
private class RotateButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
}
private class ScaleButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
}
}
private class AlphaButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
}
private class TranslateButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
}