A Ripple Effect for Android

The code in this article shows you how to apply a wave effect over a bitmap. Unlike many apps in the Google Play Store, this is a genuine wave effect that implements the 2d Wave Equation:

2ut2=c22ux2+2uy2

2d Wave Equation

Here is a sample animation:

The code is presented below with comments.  A full physical model that models specific fluid depths, refractive indices, etc is not required. Instead, practical end-effect constants are defined. E.g. a simple refractive index ratio, a scale factor on the refraction displacement, etc. For speed and battery conservation, the waves are computed on a grid 1/32th the size of the bitmap. The choice of 32 allows quick bit-shift conversion between the wave grid and the pixel grid. 2d linear interpolation is then used to convert the calculated refraction deflections to the bitmap’s pixel grid.

The code includes a ‘Poke‘ function which simulates a finger touching the air/water boundary. This would typically be hooked up to a touch or mouse event. This simulation raises a circular area of the wavefield to the maximum amplitude. Multiple closely-spaced calls to Poke can be used to simulate a dragged finger.

Here is the code:

 

Leave a Reply