---------------------------------------look at the effect (and the animated effect OH)----------------------------------------------------
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent "> <com.atguigu.myware.mywave android:layout_ Width= "Match_parent" android:layout_height= "Match_parent"/></relativelayout>
Mainactivity.java
Package Com.atguigu.myware;import Android.os.bundle;import Android.app.activity;import android.view.Menu;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);}}
Mywave.java
Package Com.atguigu.myware;import Java.util.arraylist;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.os.handler;import Android.util.attributeset;import Android.view.motionevent;import android.view.view;/** * Water ripple Effect */public class Mywave extends View {/** * Defines a water wave class (a circle includes a center point , RADIUS, brush (required to draw)) */private class Wave {//center int pointx;int pointy;//RADIUS int radius;//brush paint paint;} Two the minimum distance from the center point of a wave to a private static final int DIS_SOLP = 13;//Whether there is water wave protected Boolean isrunning = false;/** * First step constructor *///define water The collection of ripples private arraylist<wave> wlist;public mywave (context context, AttributeSet Attrs) {Super (context, attrs);// Initialize create water ripple Set wlist = new arraylist<mywave.wave> ();} /** * Touch Event */@Overridepublic Boolean ontouchevent (Motionevent event) {super.ontouchevent (event); switch ( Event.getaction ()) {//press the screen, swipe to create a water wave case MotionEvent.ACTION_DOWN:case motionevent.action_move://get down, the center of the move int x = (int) event.getx (), int y = (int) event.gety ();//If the water ripple set is empty, draw a water ripple if (wlist.size () = = 0) {//Draw water Ripple Addpoint 2List (x, y);//modify state, indicating water wave generation isrunning = true;//now sends a message stating that a water wave was drawn handler.sendemptymessage (0);} else {//If the wave set is not empty, go to the last drawn ring wave w = Wlist.get (Wlist.size ()-1); if (Math.Abs (w.pointx-x) > dis_solp| | Math.Abs (w.pointy-y) > Dis_solp) {addpoint2list (x, y);}}; Break;default:break;} return true;} /* * Define 4 colors of water waves */private int[] colors = new int[] {color.blue, color.red, color.yellow,color.green};/** * Add Wave */private void addpoint2list (int x, int y) {//Create a wave class wave w = New Wave ();//Will slide or press the coordinates assigned to the water wave class W.pointx = X;w.pointy = y;//Create brush Paint PA = new Paint ();//Draw water waves (ring) set color, antialiasing, Shape Pa.setcolor (colors[(int) (Math.random () * 4)]);p A.setantialias (true); Pa.setstyle (Style.stroke);//Assignment W.paint = pa;//Add a circular wave to the collection Wlist.add (w);} /** * Draw Water ripple */@Overrideprotected void OnDraw (canvas canvas) {for (int i = 0; i < wlist.size (); i++) {Wave wave = wlist. Get (i); Canvas.drawcircle (Wave.poiNtX, Wave.pointy, Wave.radius, Wave.paint);}} /* * Use the message mechanism */private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {//Accept the message sent over the corresponding The operation//Refresh Data flushdata ();//Refresh page invalidate ();//Cycle animation if (isrunning) {handler.sendemptymessagedelayed (0, 50);}};};/ * * Refresh the data (the transparency of the operation ring waves and the change in radius) */private void FlushData () {for (int i = 0; i < wlist.size (); i++) {Wave w = wlist.get (i); /If transparency is 0 remove int alpha = W.paint.getalpha () from the collection, if (alpha = = 0) {wlist.remove (i); continue;} Each transparency value is reduced by 5alpha = 5;if (Alpha < 5) {alpha = 0;} Decrease Transparency W.paint.setalpha (alpha);//Enlarge radius W.radius = W.radius + 3;//set radius thickness w.paint.setstrokewidth (W.RADIUS/3);} /* * If the collection is emptied, stop refreshing the animation */if (wlist.size () = = 0) {isrunning = false;}}}
---------------------------look at a simple version----------------------the effect is gradually getting bigger until it disappears--------------------------------
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent "> <com.atguigu.myware.mywave android:layout_ Width= "Match_parent" android:layout_height= "Match_parent"/></relativelayout>
Mainactivity.java
Package Com.atguigu.myware;import Android.os.bundle;import Android.app.activity;import android.view.Menu;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);}}
Mywave.java
Package Com.atguigu.myware;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.os.handler;import android.util.attributeset;import Android.view.motionevent;import android.view.View;/** * View of water ripple effect * */public class Mywave extends View {private paint paint; @Overridepublic boolean ontouchevent (Motionevent even T) {super.ontouchevent (event); switch (Event.getaction ()) {/* * * * When pressed, start drawing the ring water ripple */case motionevent.action_down://1. Set Center StartX = Event.getx (); starty = Event.gety ();//2. Create a new Circle Initview () from the Center,//3. Draw invalidate (); break;default:break;} return true;} Private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {//1. Transparency Change-small int alpha = paint. Getalpha (); alpha-= 5;if (Alpha < 0) {alpha = 0;} Transparency: 0~255 between Paint.setalpha (Alpha);//2 radius becomes larger radius + = 5;paint.setstrokewidth (RADIUS/3);//3. Draw one again, Execute OnDraw method Invalidate ();};};/ * * First step, constructor */public MyWAve (Context context, AttributeSet Attrs) {Super (context, attrs); Initview ();} /** * Initialize View */private int radius;//radius private void Initview () {paint = new paint ();//Set Color Paint.setcolor (color.red);//Set Set anti-aliasing Paint.setantialias (true);//sets the Ring style Paint.setstyle (style.stroke);//Radius 5radius = 5;paint.setstrokewidth (RADIUS/3 );} Round coordinates private float startx;private float starty;/** * Draw annular water ripple */@Overrideprotected void OnDraw (canvas canvas) {if (paint. Getalpha () > 0) {if (StartX > 0 && starty > 0) {//Draw a ring canvas.drawcircle (StartX, starty, radius, paint) ;//Draw at the same time, send a delay message handler.sendemptymessagedelayed (0, 50);}}}
Custom Control---Inherit view class mode (multicolored water ripple case)