First look at the effect:
Implementation-----> Custom Controls
Core code:
Package Com.example.wavedemo1;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.path;import Android.os.Handler; Import Android.util.attributeset;import android.view.view;/** * @author rowandjj * * Imitation cheetah cleanup Master Wave effect */public class Waterwaveview extends View{private Handler mhandler;private long c = 0l;private Boolean mstarted = false;private Final Flo at f = 0.033f;private int malpha = 70;//transparency private int mcolor = color.blue;private float mamplitude = 6.0F; Amplitude private Final paint mpaint = new paint ();p rivate float mwatelevel = 0.5f;//water height (0~1) Private Path mpath;public Waterwave View (Context paramcontext) {super (Paramcontext); init (paramcontext);} Public Waterwaveview (Context paramcontext, AttributeSet paramattributeset) {super (Paramcontext, Paramattributeset); Init (paramcontext);} /** * start fluctuation */public void Startwave () {if (!mstarted) {this.c = 0l;mstarted = true;this.mhandler.sendemptymessage (0);}} private voidInit (Context context) {mpaint.setstrokewidth (1.0F); Mpaint.setcolor (Mcolor); Mpaint.setalpha (malpha); mPath = new Path (); mhandler = new Handler () {@Overridepublic void Handlemessage (Android.os.Message msg) {if (Msg.what = = 0) {invalidate (); if (mstarted) {//constantly send messages to yourself so that you are constantly redrawn mhandler.sendemptymessagedelayed (0, 60L);}}};} @Overrideprotected void OnDraw (canvas canvas) {canvas.save (); Mpaint.setalpha (Malpha); Mpaint.setcolor (mcolor);//Gets the control's wide height int width = getwidth (); int height = getheight ();// If not started (the Startwave method is not called), draw a rectangle if ((!mstarted) | | (width = = 0) | | (height = = 0)) {Canvas.drawrect (0.0F, HEIGHT/2, width, height, mpaint); return;} if (this.c >= 8388607L) {this.c = 0L;} C will increment this.c = (1L + this.c) at each OnDraw, float f1 = height * (1.0f-mwatelevel), int top = (int) (F1 + mamplitude); mpath.res ET ();//Draw a rectangle, that is, the height of the surface at standstill (0.0F, top, width, height, mpaint), int startX = 0;//wave effect while (StartX < width) {Canvas.drawrect T starty = (int) (f1-mamplitude* Math.sin (math.pi* (2.0F * (StartX + this).c * Width * this.f))/width); Canvas.drawline (StartX, Starty, StartX, Top, mpaint); startx++;} Canvas.restore ();} /** * Set amplitude * @param amplitued */public void setamplitude (float amplitued) {mamplitude = amplitued;} /** * Set Transparency * @param alpha */public void Setwateralpha (float alpha) {This.malpha = ((int) (255.0F * alpha)); Mpaint.setalpha (This.malpha);} /** * Set color * @param color */public void setcolor (int color) {this.mcolor = color;} /** * Set Water surface height * @param paramfloat */public void Setwaterlevel (float paramfloat) {mwatelevel = paramfloat;} @Overrideprotected void Onattachedtowindow () {Super.onattachedtowindow ();//Turn off hardware acceleration to prevent abnormal unsupported operation Exceptionthis.setlayertype (view.layer_type_software, NULL);}}
as with normal controls, configure in the layout file:
<com.example.wavedemo1.waterwaveview android:id= "@+id/wav" android:layout_width= "Match_parent " android:layout_height= "Match_parent" />
The water surface starts to fluctuate in code by calling the Startwave method, and the set method can be called to control its properties.