Chaining Rotate, Merge, and Ripple Reveal: A Custom Android SplashView
When developing a Splash screen or transition page, to enhance the visual experience, we often need to implement a series of coherent combined animations. This article records how to implement a custom SplashView that sequentially executes three animation effects: ball rotation, diffusion and aggregation, and water ripple reveal, and finally transitions to the actual page layout (represented here by a bottom image).
Effect:
Core Member Variable Definitions
Before writing the drawing logic, let's first sort out the core properties and state variables that need to be maintained in SplashView:
Paint and Colors
paint: Used to draw the six small balls.holePaint: Used to draw the hollow circle (water ripple) in the third phase.circleColors: An array of colors, storing the color for each of the six balls.Coordinates and Size Parameters
centerX,centerY: The center coordinates of the rotating circle.circleRadius: The radius of each small ball.rotateRadius: The radius of the rotating circle (the distance from a ball's center to the central point).mDistance: The maximum radius when the water ripple fully spreads, i.e., half the screen diagonal length.Animation and Dynamic State Variables
animator: A property animation object (ValueAnimator) that drives the changes across phases.currentRotateAngle: The current rotation angle in the first phase, default value is0.mRotateRadius: The dynamically changing radius of the rotating circle during the second phase's diffusion and aggregation.holeRadius: The radius of the water ripple as it dynamically expands in the third phase.
Initialization and Drawing Framework Setup
In the custom View's initialization, the paint and color array are prepared, and after the view size is determined, key coordinates are calculated:
- Size Calculation (
onSizeChanged): Get the control's center pointcenterX,centerY, and use the Pythagorean theorem to calculate half of the view's diagonal length, assign it tomDistance. - State Dispatch (
onDraw): Do not pile up complexif-elsedrawing logic inonDraw; instead, directly call the current state instance'sdrawState(canvas)method.
Phase One: Rotation Animation Implementation (RotateState)
The first phase makes six balls of different colors rotate around the center point. Create RotateState which inherits from SplashState and implement the following logic:
1. Ball Position Calculation and Drawing
- Draw a solid background: call
canvas.drawColor(backgroundColor)to clear and fill the white background. - Calculate the angle between adjacent balls:
rotatorAngel = Math.PI * 2 / circleColors.length. - Iterate through the
circleColorsarray, calculate the center coordinates of the ith ball and draw it: - Current angle:
angle = i * rotatorAngel + currentRotateAngle(when static,currentRotateAngleis 0) - X coordinate:
cx = centerX + rotateRadius * Math.cos(angle) - Y coordinate:
cy = centerY + rotateRadius * Math.sin(angle) - Set paint color
paint.setColor(circleColors[i])and callcanvas.drawCircle(cx, cy, circleRadius, paint)to draw the ball.
2. Rotation Property Animation
- Use
ValueAnimator.ofFloat(0, (float) (Math.PI * 2))to create an animation for one full rotation. - Configure animation parameters: Set repeat count
setRepeatCount(2), single durationsetDuration(1200), and useLinearInterpolatorto ensure uniform rotation. - In
addUpdateListener, get the current animation value in real time, updatecurrentRotateAngle, and callinvalidate()to trigger a redraw. - Add an
addListenerlistener, and in theonAnimationEndcallback, switch to the second phase state.
Phase Two: Diffusion and Aggregation Animation Implementation (MergeState)
After the rotation ends, the six balls first spread outward, then aggregate toward the center, to prepare for the subsequent water ripple reveal. Create MergeState which inherits from SplashState:
- Drawing Logic: Basically the same as the first phase; first draw the white background, then draw the six balls based on the angle. The difference is that the circle radius is no longer fixed; instead, it uses the dynamically changing
mRotateRadius. - Diffusion and Aggregation Control:
- Use
ValueAnimator.ofFloat(circleRadius, maxRadius)to define the radius change range, and chooseOvershootInterpolatoras the interpolator to create a rebound tension. - In
addUpdateListener, assign the current animation value tomRotateRadiusand callinvalidate()to redraw. - Combine with
animator.reverse()to achieve spreading outward first, then reversing back to the original position. - After the animation completely ends, switch to the third phase state in the end listener.
Phase Three: Water Ripple Expansion Animation Implementation (ExpandState)
After aggregation, a hollow circle (water ripple) expands outward from the center point, gradually revealing the underlying real layout. Create ExpandState which inherits from SplashState:
1. Hollow Circle Drawing Logic
- In
drawState, check: whenholeRadius > 0, draw the hollow circle water ripple; otherwise draw the white background. - Calculate dynamic parameters and draw:
- Hollow circle border width:
stroke = mDistance - holeRadius - Hollow circle drawing radius:
realRadius = mDistance / 2 + holeRadius - Set paint stroke width
holePaint.setStrokeWidth(stroke)and callcanvas.drawCircle(centerX, centerY, realRadius, holePaint)to draw the expanding transparent center area.
2. Water Ripple Property Animation
- Use
ValueAnimator.ofFloat(circleRadius, mDistance)so the expansion radius extends from a single ball radius all the way to half of the screen diagonal. - Set the interpolator to
LinearInterpolator. - In
addUpdateListener, updateholeRadiuswith the current value and callinvalidate()to trigger a redraw, then start the animation.
State Flow and Summary
The entire SplashView lifecycle achieves seamless transitions through alternating state objects:
- Initialize and enter Phase One (
RotateState) by default, execute the rotation animation. - After the rotation animation ends, instantiate Phase Two (
MergeState) and trigger its drawing and diffusion-aggregation animation. - After the diffusion-aggregation animation ends, instantiate Phase Three (
ExpandState) and execute the water ripple expansion, finally fully revealing the underlying view.
Example code: Canvas-splash