Android Water Ripple

Source: Internet
Author: User

 Public classMywaveviewextendsview{PrivateArraylist<wave>list; Private int[] Colors ={color.red,color.blue, color.dkgray,color.green,color.yellow};  Public BooleanIsRunning =false;  PublicMywaveview (Context context) { This(Context,NULL); }     PublicMywaveview (Context context, AttributeSet attrs) { This(Context, attrs,0); }         PublicMywaveview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); List=NewArraylist<mywaveview.wave>(); }            //triggered when the user touches the hand@Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down: CaseMotionevent.action_move:intx = (int) Event.getx (); inty = (int) event.gety (); //create ripples and put them in the set;addwave (x, y);  Break; default:             Break; }        return true; }             Public voidAddwave (intXinty) {Wave Wave=NewWave (); //To determine the centerWave.rx =x; Wave.ry=y; //Determine radius radius:0--->Paint Paint=NewPaint (); Paint.setcolor (colors[(int) (Math.random ())]); Paint.setantialias (true);        Paint.setstyle (Paint.Style.STROKE); Wave.paint=paint;    Addwavetolist (Wave,x,y); }        Private voidAddwavetolist (Wave wave,intXinty) {if(list.size () = = 0) {List.add (wave); IsRunning=true; Handler.sendemptymessage (0); }Else{            if(Math.Abs (X-(List.get (List.size ()-1)). RX) > 50 | | Math.Abs (Y-(List.get (List.size ()-1)). RY) > 50) {List.add (wave); }}} Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {refreshpaint ();                        Invalidate (); if(isrunning) {handler.sendemptymessagedelayed (0, 18);    }        };            }; Private voidRefreshpaint () { for(inti = 0;i< list.size (); i++) {Wave wave=List.get (i); Wave.radius+ = 4; intAlpha =Wave.paint.getAlpha (); Alpha-= 5; if(Alpha < 5) {Alpha= 0;            } wave.paint.setAlpha (Alpha); if(Alpha = = 0) {list.remove (i); } Wave.radius= Wave.radius + 3; Wave.paint.setStrokeWidth (Wave.radius/3); if(list.size () = = 0) {isrunning=false; }        }    }    //Water Ripple Object     Public classwave{ Public intradius;  Public intRX;  Public intRY;  PublicPaint paint; } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas);  for(inti = 0; I < list.size (); i++) {Wave wave=List.get (i);        Canvas.drawcircle (Wave.rx, Wave.ry, Wave.radius, Wave.paint); }    }}

Android Water Ripple

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.