首页 > 解决方案 > 如何在Android中制作线性显示动画而不是圆形显示动画?

问题描述

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

更新

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

标签: androidandroid-studioanimationandroid-animationcircularreveal

解决方案


尝试下面的代码以获得线性显示效果 -

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 方法的动画和偏移值。


推荐阅读