Android Water Ripple Effect realized

Source: Internet
Author: User

Customize a class, inherit view, directly on the code, relatively simple

public class Myring extends View {
Private list<wave> listwave;//Ripple List
Private int[] Colors = {color.blue, color.green, Color.yellow, color.red, Color.cyan};
Private final int midx = 13;
Private final int midy = 13;
Private Boolean isrunning = false;//false has completed true to begin painting

Public myring (context context, AttributeSet Attrs) {
Super (context, attrs);
Initview ();
}

/**
* Initialize Data
*/
private void Initview () {
Listwave = new arraylist<myring.wave> ();
}

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
for (int i = 0; i < listwave.size (); i++) {
Canvas.drawcircle (Listwave.get (i). CX, Listwave.get (i). CY, Listwave.get (i). Radius, Listwave.get (i). Paint);
}

}

@Override
public boolean ontouchevent (Motionevent event) {
Super.ontouchevent (event);
Switch (event.getaction ()) {
Case Motionevent.action_down:
Case Motionevent.action_move:
int cx = (int) event.getx ();
int cy = (int) event.gety ();
Chexkwave (CX, CY);
Break

Default
Break
}
return true;
}

/**
* Check Water droplets
*
* @param CX
* @param cy
*/
private void Chexkwave (int cx, int cy) {
if (listwave.size () = = 0) {
IsRunning = true;
Addwave (CX, CY);//Add water droplets
Handler.sendemptymessage (0);//Send Update message
} else {
Wave wave = Listwave.get (Listwave.size ()-1);
if (Math.Abs (WAVE.CX-CX) > Midx | | Math.Abs (Wave.cy-cy) > Midy) {
exceeds the interval
Addwave (CX, CY);
}
}
}

/**
* Add water droplets. Initializing droplet information
*
* @param CX
* @param cy
*/
private void Addwave (int cx, int cy) {
Wave wave = new Wave ();
WAVE.CX = CX;
wave.cy = cy;
Wave.radius = 5;
Paint paint = new paint ();
Paint.setantialias (TRUE);//Set anti-aliasing
Random random = new random ();
int coloritem = Random.nextint (5);
Paint.setcolor (Colors[coloritem]);//Set color
Paint.setstyle (Style.stroke);//Set the ring
Paint.setstrokewidth (10);//Set the ring width
Paint.setalpha (255);//Set Transparency 0-255 0 is fully transparent
Wave.paint = paint;
Listwave.add (Wave);
}

/**
* Refresh Status
*/
private void Flushstats () {

for (int i = 0; i < listwave.size (); i++) {
Wave wave = Listwave.get (i);
int alpha2 = Wave.paint.getAlpha ();

if (alpha2 = = 0) {
Listwave.remove (i);
Continue
}
ALPHA2-= 10;
ALPHA2 = Math.max (0, ALPHA2);
Wave.paint.setAlpha (ALPHA2);
Wave.radius + = 8;
Wave.paint.setStrokeWidth (WAVE.RADIUS/3);

}
if (listwave.size () = = 0) {
IsRunning = false;
}

}

Private Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Flushstats ();
Invalidate ();//Perform refresh action
if (Paint.getalpha () > 0) {
handler.sendemptymessagedelayed (0, 100);
// }
if (isrunning) {
handler.sendemptymessagedelayed (0, 100);
}

}

};

/**
* Use the default size
*/
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
TODO auto-generated Method Stub
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}

/**
* Define Ripple
*
* @author
*
*/
Private class Wave {
X-axis coordinates
private int CX;
Y-Axis coordinates
private int cy;
Radius of the ring
private float radius;
Brush
private paint paint;
}

Water Ripple effect-zip

Android Water Ripple Effect realized

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.