Android: Ripple Background Programmatically

后端 未结 3 1934
忘掉有多难
忘掉有多难 2020-12-14 10:37

I am currently creating an Android app where someone can input their name, press a button, and then it just outputs their name back to them.

One effect that I would

3条回答
  •  死守一世寂寞
    2020-12-14 11:11

    What you are describing is a reveal effect on the background.

    From the official doc you can find ready to use examples:

    1) Here is how to reveal a previously invisible view using reveal effect:

     // previously invisible view
     View myView = findViewById(R.id.my_view);
    
     // get the center for the clipping circle
     int cx = myView.getWidth() / 2;
     int cy = myView.getHeight() / 2;
    
     // get the final radius for the clipping circle
     int finalRadius = Math.max(myView.getWidth(), myView.getHeight());
    
     // create the animator for this view (the start radius is zero)
     Animator anim =
         ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
    
     // make the view visible and start the animation
     myView.setVisibility(View.VISIBLE);
     anim.start();
    

    2) Here is how to hide a previously visible view using the reveal effect:

     // previously visible view
     final View myView = findViewById(R.id.my_view);
    
     // get the center for the clipping circle
     int cx = myView.getWidth() / 2;
     int cy = myView.getHeight() / 2;
    
     // get the initial radius for the clipping circle
     int initialRadius = myView.getWidth();
    
     // create the animation (the final radius is zero)
     Animator anim =
         ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);
    
     // make the view invisible when the animation is done
     anim.addListener(new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
             super.onAnimationEnd(animation);
             myView.setVisibility(View.INVISIBLE);
         }
     });
    
     // start the animation
     anim.start();
    

    In you app, you can use a colored background layer (invisible at the beginning) and then use the reveal effect on it.

提交回复
热议问题