Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a circular reveal animation

I'm trying to create a circular reveal animation that starts at the center of a View (API 21+). I tried using the averages of View.getLeft() and View.getRight() for x and View.getTop() and View.getBottom() for y (which is what the sample does, but if my View is offset by CoordinatorLayout, the values are wrong. I'm grabbing the values in an OnPreDrawListener, so the View has already been measured.

like image 558
keyboardr Avatar asked Nov 23 '25 11:11

keyboardr


1 Answers

I've found that using View.getDrawingRect() does the trick.

private Animator createCenteredReveal(View view) {
  // Could optimize by reusing a temporary Rect instead of allocating a new one
  Rect bounds = new Rect();
  view.getDrawingRect(bounds);
  int centerX = bounds.centerX();
  int centerY = bounds.centerY();
  int finalRadius = Math.max(bounds.width(), bounds.height());
  return ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0f, finalRadius);
}
like image 62
keyboardr Avatar answered Nov 25 '25 02:11

keyboardr