如何在Android中制作线性展示动画而不是圆形展示动画?

时间:2019-02-06 06:15:26

标签: android android-studio animation android-animation circularreveal

我想为我的按钮设置动画,该按钮从按钮的左偏移量显示到该按钮的右边。我想要的是线性显示动画,而不是圆形显示动画。 请帮我。谢谢。

更新

我想要两种基于线性揭示动画显示和隐藏视图的方法。

1 个答案:

答案 0 :(得分:1)

尝试以下代码以显示线性揭示效果-

public class ViewAnimationUtils {

public static AnimatorSet createLinearReveal(final View viewToReveal, final int offsetWidth, final int offsetHeight, final int duration) {
    viewToReveal.clearAnimation();

    final int targetWidth = viewToReveal.getMeasuredWidth();
    final int targetHeight = viewToReveal.getMeasuredHeight();

    viewToReveal.getLayoutParams().height = offsetHeight;
    viewToReveal.requestLayout();

    final ValueAnimator heightAnimator = ValueAnimator
            .ofInt(offsetHeight, targetHeight)
            .setDuration(duration);
    heightAnimator.addUpdateListener(animation -> {
        viewToReveal.getLayoutParams().height = (int) animation.getAnimatedValue();
        viewToReveal.requestLayout();
    });

    final ValueAnimator widthAnimator = ValueAnimator
            .ofInt(offsetWidth, targetWidth)
            .setDuration(duration);
    widthAnimator.addUpdateListener(animation -> {
        viewToReveal.getLayoutParams().width = (int) animation.getAnimatedValue();
        viewToReveal.requestLayout();
    });

    final AnimatorSet set = new AnimatorSet();
    set.playSequentially(widthAnimator, heightAnimator);
    set.setInterpolator(new AccelerateInterpolator());
    return set;
}

}

添加此类后,只需将您的视图传递动画值和偏移量到createLinearReveal方法即可。