Multi-color water ripple diffusion effect

Source: Internet
Author: User

Layout

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" Match_parent "    android:layout_height = "Match_parent"    tools:context= ". Mainactivity ">    <com.ee.MyWave        android:layout_width=" Match_parent "        android: Layout_height= "Match_parent" >    </com.ee.MyWave></RelativeLayout>

Class

 Packagecom.ee;Importjava.util.ArrayList;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Paint.Style;ImportAndroid.os.Handler;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;/*** Water Ripple effect*/ Public classMywaveextendsView {/*** Define a water wave class (a circle includes the center, RADIUS, brush (required to draw))*/    Private classWave {//Center        intPointx; intPointy; //radius        intradius; //BrushesPaint paint; }    //the minimum distance between the two and the center point of a wave    Private Static Final intDIS_SOLP = 13; //whether there is water wave    protected BooleanIsRunning =false; /*** First Step builder*/    //define the set of water ripples    PrivateArraylist<wave>wlist;  PublicMywave (Context context, AttributeSet attrs) {Super(context, attrs); //Initialize Create water ripple setWlist =NewArraylist<mywave.wave>(); }    /*** Touch Event*/@Override Public Booleanontouchevent (Motionevent event) {Super. Ontouchevent (event); Switch(Event.getaction ()) {//Press the screen, swipe to create water waves         CaseMotionevent.action_down: CaseMotionevent.action_move://get the center of the down and move            intx = (int) Event.getx (); inty = (int) event.gety (); //If the water ripple set is empty, draw a water ripple            if(wlist.size () = = 0) {                //Draw Water Rippleaddpoint2list (x, y); //modify the state to indicate the formation of water wavesIsRunning =true; //now send a message stating that a water wave was drawnHandler.sendemptymessage (0); } Else {                //If the wave set is not empty, go to the last drawn circular water waveWave 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 Waves*/    Private voidAddpoint2list (intXinty) {//Create a water wave classWave W =NewWave (); //Assign values to the water wave class by sliding or pressing the coordinatesW.pointx =x; W.pointy=y; //Create a brushPaint PA =NewPaint (); //Draw a water wave (ring) set color, antialiasing, ShapePa.setcolor (colors[(int) (Math.random () * 4)]); Pa.setantialias (true);        Pa.setstyle (Style.stroke); //Assign ValueW.paint =PA; //add a circular wave to the collectionWlist.add (W); }    /*** Draw Water Ripple*/@Overrideprotected voidOnDraw (canvas canvas) { for(inti = 0; I < wlist.size (); i++) {Wave wave=Wlist.get (i);        Canvas.drawcircle (Wave.pointx, Wave.pointy, Wave.radius, Wave.paint); }    }    /** Using message mechanism*/    PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {//accept the message sent to the appropriate action//Refresh DataFlushData (); //Refresh Pageinvalidate (); //Looping animations            if(isrunning) {handler.sendemptymessagedelayed (0, 50);    }        };    }; /*** Refresh the data (the transparency of the operation ring water and the change in radius)*/    Private voidFlushData () { for(inti = 0; I < wlist.size (); i++) {Wave W=Wlist.get (i); //If the transparency is 0 removed from the collection            intAlpha =W.paint.getalpha (); if(Alpha = = 0) {wlist.remove (i); Continue; }            //minus 5 per transparency valueAlpha-= 5; if(Alpha < 5) {Alpha= 0; }            //Reduce TransparencyW.paint.setalpha (Alpha); //Enlarge radiusW.radius = W.radius + 3; //Set radius thicknessW.paint.setstrokewidth (W.RADIUS/3); }        /** Stop refreshing animations If the collection is emptied*/        if(wlist.size () = = 0) {isrunning=false; }    }}

Multi-color water ripple diffusion effect

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.