下面列出了android.view.animation.AnimationSet#addAnimation() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Animation createShrinkAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,
long startOffset, long duration, Interpolator interpolator) {
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
final long preDuration = duration / 2;
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setStartOffset(startOffset);
rotateAnimation.setDuration(preDuration);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
Animation translateAnimation = new RotateAndTranslateAnimation(0, toXDelta, 0, toYDelta, 360, 720);
translateAnimation.setStartOffset(startOffset + preDuration);
translateAnimation.setDuration(duration - preDuration);
translateAnimation.setInterpolator(interpolator);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
@Override
public Animation createOptionDisappearAnimation(OptionButton optionButton, int index) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation= new TranslateAnimation(
0
,getYMenuButton().getX() - optionButton.getX()
,0
,getYMenuButton().getY() - optionButton.getY()
);
translateAnimation.setDuration(getOptionSD_AnimationDuration());
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(getOptionSD_AnimationDuration());
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
//设置动画延时
animationSet.setStartOffset(60*(getOptionPositionCount() - index));
return animationSet;
}
public ListenableFuture<Void> hide() {
final SettableFuture<Void> future = new SettableFuture<>();
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, slideToCancelView.getTranslationX(),
Animation.ABSOLUTE, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0));
animation.addAnimation(new AlphaAnimation(1, 0));
animation.setDuration(MicrophoneRecorderView.ANIMATION_DURATION);
animation.setFillBefore(true);
animation.setFillAfter(false);
slideToCancelView.postDelayed(() -> future.set(null), MicrophoneRecorderView.ANIMATION_DURATION);
slideToCancelView.setVisibility(View.GONE);
slideToCancelView.startAnimation(animation);
return future;
}
private void display(float x) {
this.startPositionX = x;
this.lastPositionX = x;
recordButtonFab.setVisibility(VISIBLE);
recordButtonFab.setX(getWidthAdjustment() + getOffset(x));
AnimationSet animation = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1f, 0.5f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.addAnimation(scaleAnimation);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setDuration(ANIMATION_DURATION);
animation.setInterpolator(new OvershootInterpolator());
recordButtonFab.startAnimation(animation);
}
public static void shakeCurtain(View v) {
AnimationSet set = new AnimationSet(false);
Animation anim1 = getTranslateAnimation(0, 0, 0, -200, 110);
Animation anim2 = getTranslateAnimation(0, 0, -200, 0, 80);
Animation anim3 = getTranslateAnimation(0, 0, 0, -50, 25);
Animation anim4 = getTranslateAnimation(0, 0, -50, 0, 25);
anim1.setStartOffset(20);
anim2.setStartOffset(230);
anim3.setStartOffset(360);
anim4.setStartOffset(400);
set.addAnimation(anim1);
set.addAnimation(anim2);
set.addAnimation(anim3);
set.addAnimation(anim4);
v.startAnimation(set);
}
private void useCodeAnimation(ViewAnimator pages) {
// 用代码定义一个动画
AnimationSet slideAnimationSet = new AnimationSet(true);
// 平移动画
TranslateAnimation slide = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT,
0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
// 缩放动画
ScaleAnimation scale = new ScaleAnimation(10, 1, 10, 1);
// 把平移和缩放动画加入动画集合
slideAnimationSet.addAnimation(slide);
slideAnimationSet.addAnimation(scale);
// 持续时间设置为1000ms
slideAnimationSet.setDuration(1000);
// 设置动画
pages.setInAnimation(slideAnimationSet);
}
public static void shakeCurtain(View v) {
AnimationSet set = new AnimationSet(false);
Animation anim1 = getTranslateAnimation(0, 0, 0, -200, 110);
Animation anim2 = getTranslateAnimation(0, 0, -200, 0, 80);
Animation anim3 = getTranslateAnimation(0, 0, 0, -50, 25);
Animation anim4 = getTranslateAnimation(0, 0, -50, 0, 25);
anim1.setStartOffset(20);
anim2.setStartOffset(230);
anim3.setStartOffset(360);
anim4.setStartOffset(400);
set.addAnimation(anim1);
set.addAnimation(anim2);
set.addAnimation(anim3);
set.addAnimation(anim4);
v.startAnimation(set);
}
/**
* 隐藏减号的动画
*/
private Animation getHiddenAnimation() {
AnimationSet set = new AnimationSet(true);
RotateAnimation rotate = new RotateAnimation(0, 720, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
set.addAnimation(rotate);
TranslateAnimation translate = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0
, TranslateAnimation.RELATIVE_TO_SELF, 2f
, TranslateAnimation.RELATIVE_TO_SELF, 0
, TranslateAnimation.RELATIVE_TO_SELF, 0);
set.addAnimation(translate);
AlphaAnimation alpha = new AlphaAnimation(1, 0);
set.addAnimation(alpha);
set.setDuration(500);
return set;
}
private void initAnimations() {
Animation inScaleAnimation = new ScaleAnimation(1.2f, 1, 1.2f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation inAlphaAnimation = new AlphaAnimation(0.0f, 1f);
AnimationSet inAnimationSet = new AnimationSet(true);
inAnimationSet.setDuration(ANIMATION_DURATION);
inAnimationSet.addAnimation(inScaleAnimation);
inAnimationSet.addAnimation(inAlphaAnimation);
mInAnimation = inAnimationSet;
Animation outScaleAnimation = new ScaleAnimation(1, 1.4f, 1, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation outAlphaAnimation = new AlphaAnimation(1f, 0f);
AnimationSet outAnimationSet = new AnimationSet(true);
outAnimationSet.setDuration(ANIMATION_DURATION);
outAnimationSet.addAnimation(outScaleAnimation);
outAnimationSet.addAnimation(outAlphaAnimation);
mOutAnimation = outAnimationSet;
}
@MainThread
public void append(int value) {
if (index >= codes.size()) return;
setInactive(containers);
setActive(containers.get(index));
TextView codeView = codes.get(index++);
Animation translateIn = new TranslateAnimation(0, 0, codeView.getHeight(), 0);
translateIn.setInterpolator(new OvershootInterpolator());
translateIn.setDuration(500);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(200);
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(fadeIn);
animationSet.addAnimation(translateIn);
animationSet.reset();
animationSet.setStartTime(0);
codeView.setText(String.valueOf(value));
codeView.clearAnimation();
codeView.startAnimation(animationSet);
if (index == codes.size() && listener != null) {
listener.onCodeComplete(Stream.of(codes).map(TextView::getText).collect(Collectors.joining()));
}
}
private void animateFromLeftPaneToRightPane(View view) {
Animation translate = new LeftMarginAnimation(view, 0, mLeftPaneWidth);
Animation width = new WidthAnimation(view, mLeftPaneWidth, mRightPaneWidth);
AnimationSet set = new AnimationSet(true);
set.addAnimation(translate);
set.addAnimation(width);
view.startAnimation(set);
}
/**
* 从顶部进入
*
* @return
*/
public static AnimationSet getAnimationSetFromTop() {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateX1 = new TranslateAnimation(RELATIVE_TO_SELF, 0, RELATIVE_TO_SELF, 0,
RELATIVE_TO_SELF, -2.5f, RELATIVE_TO_SELF, 0);
translateX1.setDuration(400);
translateX1.setInterpolator(new DecelerateInterpolator());
translateX1.setStartOffset(0);
animationSet.addAnimation(translateX1);
animationSet.setDuration(400);
return animationSet;
}
static Animation makeOpenCloseAnimation(Context context, float startScale,
float endScale, float startAlpha, float endAlpha) {
AnimationSet set = new AnimationSet(false);
ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale,
Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
scale.setInterpolator(DECELERATE_QUINT);
scale.setDuration(ANIM_DUR);
set.addAnimation(scale);
AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha);
alpha.setInterpolator(DECELERATE_CUBIC);
alpha.setDuration(ANIM_DUR);
set.addAnimation(alpha);
return set;
}
private void addTextRotateAnimations(AnimationSet set) {
RotateAnimation mRotateUpAnim = new RotateAnimation(0.0f, ROTATION_ANGLE, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
mRotateUpAnim.setDuration(CHARACTER_ANIM_DURATION);
set.addAnimation(mRotateUpAnim);
RotateAnimation mRotateDownAnim = new RotateAnimation(ROTATION_ANGLE, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
mRotateDownAnim.setDuration(CHARACTER_ANIM_DURATION);
mRotateDownAnim.setStartOffset(CHARACTER_ANIM_DURATION + 20);
mRotateDownAnim.setFillAfter(true);
set.addAnimation(mRotateDownAnim);
set.setInterpolator(interpolator);
}
protected synchronized void animateNow() {
View view = this.view.get();
if (view != null) {
AnimationSet set = new AnimationSet(true);
for (Animation animation : animations)
if (animation != null)
set.addAnimation(animation);
set.setDuration(0);
set.setFillAfter(true);
view.setAnimation(set);
set.start();
animations.clear();
}
}
private void showFloatImage() {
isShowFloatImage = true;
TranslateAnimation ta = new TranslateAnimation(moveDistance,0,0,0);
ta.setDuration(300);
AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
aa.setDuration(300);
AnimationSet set = new AnimationSet(true);
//动画完成后不回到原位 true:persist 默认值为false
set.setFillAfter(true);
set.addAnimation(ta);
set.addAnimation(aa);
mIvCartView.startAnimation(set);
}
private static Animation createItemDisapperAnimation(final long duration, final boolean isClicked) {
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(new ScaleAnimation(1.0f, isClicked ? 2.0f : 0.0f, 1.0f, isClicked ? 2.0f : 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
animationSet.setDuration(duration);
animationSet.setInterpolator(new DecelerateInterpolator());
animationSet.setFillAfter(true);
return animationSet;
}
public void scaleRotateIn(View view, long durationMillis, long delayMillis)
{
ScaleAnimation animation1 = new ScaleAnimation(0, 1, 0, 1, rela1, 0.5f, rela1, 0.5f);
RotateAnimation animation2 = new RotateAnimation(0, 360, rela1, 0.5f, rela1, 0.5f);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(animation1);
animation.addAnimation(animation2);
baseIn(view, animation, durationMillis, delayMillis);
}
public static AnimationSet startAlphaHideAnimation(final int DELAY, final View VIEW, boolean includeTransition) {
final int ANIMATION_DURATION = 300;
if(VIEW == null)
return null;
final Animation mAlphaAnimation = new AlphaAnimation(1f, 0f);
mAlphaAnimation.setDuration(ANIMATION_DURATION);
mAlphaAnimation.setFillAfter(true);
final AnimationSet mHideAnimations = new AnimationSet(true);
mHideAnimations.setInterpolator(new AccelerateDecelerateInterpolator());
mHideAnimations.setFillAfter(true);
mHideAnimations.addAnimation(mAlphaAnimation);
if (includeTransition) {
final Animation mTransitionAnimation = new TranslateAnimation(0, 0, 0, VIEW.getHeight()/2);
mTransitionAnimation.setDuration(ANIMATION_DURATION);
mTransitionAnimation.setFillAfter(false);
mHideAnimations.addAnimation(mTransitionAnimation);
}
new Handler().postDelayed(() -> {
VIEW.startAnimation(mHideAnimations);
}, DELAY);
return mHideAnimations;
}
public static Animation clickAnimation(float scaleXY, long durationMillis) {
AnimationSet set = new AnimationSet(true);
set.addAnimation(getScaleAnimation(scaleXY, durationMillis));
set.setDuration(durationMillis);
return set;
}