Animate visibility of a view from gone to visible with animation

前端 未结 5 747
傲寒
傲寒 2021-02-01 17:53

I have a view that is invisible by default(Just for the first time).

Now I need to switch the visibility to VISIBLE with this animation

5条回答
  •  一个人的身影
    2021-02-01 18:26

    Based on this answer:

    with this methods, I can set the visibility of my view to VISIBLE with a slideUp animation(Like snackbar animation):

    int getScreenHeight() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }
    
    public void animateOnScreen(View view) {
        final int screenHeight = getScreenHeight();
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", screenHeight, (screenHeight * 0.8F));
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }
    

    Then I can use it like this:

    if (myView.getVisibility() == View.INVISIBLE) {
        myView.setVisibility(View.VISIBLE);
        animateOnScreen(myView);
    }
    

提交回复
热议问题