CircularReveal动画在首次尝试时不起作用

arc*_*tjn 15 animation android android-5.0-lollipop

在android 5.0中,我正在尝试使用圆形揭示动画

问题

当我点击按钮开始显示动画时,第一次点击动画无法启动

第二次点击它可以正常工作

我的守则

public class MainActivity extends ActionBarActivity {

Animator a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final View cardType = findViewById(R.id.cardtype);
    cardType.setVisibility(View.GONE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        a = ViewAnimationUtils.createCircularReveal(cardType,
                cardType.getWidth(),
                cardType.getHeight(),
                0,
                cardType.getHeight() * 2)
                .setDuration(2500);
        a.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                cardType.setVisibility(View.VISIBLE);
            }
        });
        a.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                cardType.setVisibility(View.GONE);
            }
        });
        findViewById(R.id.icon_first_activity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a.start();
            }
        });
    }
}}
Run Code Online (Sandbox Code Playgroud)

G. *_*ike 30

我没有尝试过您的代码,但我认为您的订购问题很少.我想你只需要开始动画之前设置cardType可见性.

编辑添加:

...并且您应该设置按钮View.INVISIBLE,而不是View.GONE.

这里: 此代码有效.

再次编辑添加:

是.您的问题是您最初设置了GONE视图.这意味着它有0个大小.然后你使用cardType.getHeightcardType.getWidth作为显示坐标.它们是0.您最初要将视图设置为INVISIBLE,然后使用width/2和height/2作为显示的中心.


Dav*_*eas 6

基本上其他人的回答说,这是正确的,但问题是如果你想要可见性GONE(因为你的布局需要它GONE!)你必须在xml中设置可见性INVISIBLE高度为0dp(和/或宽度为0dp)并以编程方式即使在它将起作用的click事件中设置正确的LayoutParams.例如我的代码:

    ...
    expandButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //To not have empty scroll, the container is INVISIBLE with 0dp height.
            //Otherwise the Reveal effect will not work at the first click.
            //Here I set the parameters programmatically.
            viewContainer.setLayoutParams(new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            if (viewContainer.getVisibility() == View.VISIBLE) {
                expandButton.animate().rotation(0f).setDuration(duration).start();
                Utils.unReveal(viewContainer, 0, 0);
            } else {
                expandButton.animate().rotation(180f).setDuration(duration).start();
                Utils.reveal(viewContainer, viewContainer.getWidth(), 0);
            }
        }
    });
    ...

@TargetApi(VERSION_CODES.LOLLIPOP)
public static void reveal(final View view, int cx, int cy) {
    if (!hasLollipop()) {
        view.setVisibility(View.VISIBLE);
        return;
    }

    //Get the final radius for the clipping circle
    int finalRadius = Math.max(view.getWidth(), view.getHeight());

    //Create the animator for this view (the start radius is zero)
    Animator animator =
            ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);

    //Make the view VISIBLE and start the animation
    view.setVisibility(View.VISIBLE);
    animator.start();
}

@TargetApi(VERSION_CODES.LOLLIPOP)
public static void unReveal(final View view, int cx, int cy) {
    if (!hasLollipop()) {
        view.setVisibility(View.GONE);
        return;
    }

    //Get the initial radius for the clipping circle
    int initialRadius = view.getWidth();

    //Create the animation (the final radius is zero)
    Animator animator =
        ViewAnimationUtils.createCircularReveal(view, cx, cy, initialRadius, 0);

    //Make the view GONE when the animation is done
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            view.setVisibility(View.GONE);
        }
    });

    //Start the animation
    animator.start();
}
Run Code Online (Sandbox Code Playgroud)

如果你只在xml中设置了GONE,那么第一次将永远不会工作,因为高度/宽度/ x/y /等..是0.另外,如果你只是在调用动画之前设置了INVISIBLE它也不会工作,但是如果你从可见性INVISIBLE开始,它将初始化布局参数.