接上一篇,这里我们介绍AnimationSet,也就是组合渐变。
如下是我们用到的类:
android.view.animation.AnimationSet
上一篇我们介绍了透明动画效果(AlphsAnimation)、移动动画效果(TranslateAnimation)、旋转动画效果(RotateAnimation)、缩放动画效果(ScaleAnimation);那么可不可以将这几种动画效果混在一块用呢?可以的,这个时候我们就用到了AnimationSet,它可以将一种或多种动画效果放在一起运行。
AnimationSet也继承Animation,常见方法如下:
方法 | 描述 |
---|---|
AnimationSet(boolean shareInterpolator) | 构造函数 |
void addAnimation(Animation a) | 添加定义好的动画效果 |
void setDuration(long durationMillis) | 设置播放时长(毫秒) |
void setRepeatCount(int repeatCount) | 设置重放次数 |
void setRepeatMode(int repeatMode) | 设置重放模式(reverse表示倒序回放,restart表示从头播放) |
void setInterpolator(Interpolator i) | 设置插值器 |
void setFillAfter(boolean fillAfter) | 控件动画结束时是否保持动画最后的状态 |
void setFillBefore(boolean fillBefore) | 控件动画结束时是否还原到开始动画前的状态 |
void setStartOffset(long startOffset) | 调用start函数之后等待开始运行的时间,单位为毫秒 |
void setAnimationListener(AnimationListener listener) | 动画的事件监听 |
这里有一个差值器(interpolator)的概念,我们后续再介绍。
我们同样采用xml load和在Activity中直接设置两种方式实现。
首先新建animation.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000" />
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:duration="2000" />
<scale
android:fromXScale="1"
android:toXScale="10"
android:fromYScale="1"
android:toYScale="10"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:duration="2000" />
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="320"
android:repeatCount="1"
android:repeatMode="reverse"
android:duration="2000" />
</set>
然后在上一篇Domo的基础上修改Activity,代码如下:
package com.example.animationsetdomo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
imageView = findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(count == 0){
imageView.startAnimation(animation);
count = 1;
}else if(count == 1){
AnimationScale();
count = 0;
}
}
});
}
void AnimationScale(){
//构造方法的入参如果是“ture”,则代表使用默认的interpolator,如果是“false”则代表使用自己定义interpolator
AnimationSet animationSet = new AnimationSet(true);
//透明度从0到1
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
//旋转2圈
RotateAnimation rotateAnimation = new RotateAnimation(0,720,
Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
//放大2倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1f,10,1f,10,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setRepeatCount(1);
scaleAnimation.setRepeatMode(Animation.REVERSE);
//平移
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.ABSOLUTE,0, Animation.RELATIVE_TO_PARENT,0.305f,
Animation.ABSOLUTE,0, Animation.RELATIVE_TO_PARENT,0.305f);
translateAnimation.setRepeatCount(1);
translateAnimation.setRepeatMode(Animation.REVERSE);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(2000);
animationSet.setRepeatMode(Animation.REVERSE);
animationSet.setFillAfter(false);
animationSet.setFillBefore(true);
animationSet.cancel();
animationSet.reset();
imageView.startAnimation(animationSet);
}
}
Domo代码: