Water ripple effect --- 3 ring ripple, water ripple --- 3 ripple
Import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. bitmap. config; import android. OS. handler; import android. OS. message; import android. util. displayMetrics; import android. util. log; import android. view. view; import android. view. viewGroup; import android. widget. imageView;/*** water ripple View * @ author alan **/public class WaterView extends View {/*** opacity (completely transparent: 0) */private int radius1; private int radius2; private int radius3; private int alpha1; private int alpha2; private int alpha3; private Paint paint1; private Paint paint2; private Paint paint3; private int widthScreen; private int heightScreen; private Handler mHandler;/*** image diameter */private static final int MIN = 50; public WaterView (Context context, Bitmap source) {super (context); init (); // obtain the screen width and height DisplayMetrics dm = getResources (). getDisplayMetrics (); widthScreen = dm. widthPixels; heightScreen = dm. heightPixels; mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case 0: flushState (); WaterView. this. invalidate (); if (alpha1 = 0 | alpha2 = 0 | alpha3 = 0) {Log. I ("TAG", "alpha1 -->" + alpha1 + "alpha2 -->" + alpha2 + "alpha3 -->" + alpha3); mHandler. sendEmptyMessage (1);} else {mHandler. sendEmptyMessageDelayed (0, 50);} break; case 1: initSimple (); WaterView. this. invalidate (); mHandler. sendEmptyMessage (0); break; default: break ;}}; mHandler. sendEmptyMessage (0) ;}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper. onMeasure (widthMeasureSpec, heightMeasureSpec);} @ Overrideprotected void onDraw (Canvas canvas) {Log. I ("TAG", "onDraw ...... "); super. onDraw (canvas); // circle // c1 (smallest circle) canvas. drawCircle (widthScreen/2, this. getHeight ()/2, radius1, paint1); // c2canvas. drawCircle (widthScreen/2, this. getHeight ()/2, radius2, paint2); // c3canvas. drawCircle (widthScreen/2, this. getHeight ()/2, radius3, paint3);} private void init () {alpha1 = 255; radius1 = MIN/2; alpha2 = 175; radius2 = MIN/2 + 80; alpha3 = 95; radius3 = MIN/2 + 160; initPaint ();} private void initSimple () {if (alpha1 = 0) {alpha1 = 255; radius1 = MIN/2; paint1.setColor (Color. BLUE);} if (alpha2 = 0) {alpha2 = 255; radius2 = MIN/2; paint2.setColor (Color. BLUE);} if (alpha3 = 0) {alpha3 = 255; radius3 = MIN/2; paint3.setColor (Color. BLUE);} paint1.setAlpha (alpha1); paint2.setAlpha (alpha2); paint3.setAlpha (alpha3);} private void flushState () {radius1 + = 5; alpha1-= 5; // control the speed. The greater the value, the faster the ring expands. radius2 + = 5; alpha2-= 5; radius3 + = 5; alpha3-= 5; if (alpha1 <= 0) {alpha1 = 0;} if (alpha2 <= 0) {alpha2 = 0;} if (alpha3 <= 0) {alpha3 = 0;} paint1.setAlpha (alpha1 ); paint2.setAlpha (alpha2); paint3.setAlpha (alpha3);} private void initPaint () {// create a Paint brush paint1 = new Paint (); paint1.setAntiAlias (true ); // set the paint1.setStrokeWidth (float) 1); // set the width of the 1paint1. setStyle (Paint. style. STROKE); // you can specify a hollow paint brush to draw paint1.setAlpha (alpha1) and paint1.setColor (Color. BLUE); // defines the Paint brush 2paint2 = new Paint (); paint2.setAntiAlias (true); // sets the anti-sawtooth paint2.setStrokeWidth (float) 1); // sets the linewidth 1paint2. setStyle (Paint. style. STROKE); // set the hollow Paint brush and draw paint2.setAlpha (alpha2) in a circular manner; // define the Paint brush 3paint3 = new Paint (); paint3.setAntiAlias (true ); // set the paint3.setStrokeWidth (float) 1); // set the width of the 1paint3. setStyle (Paint. style. STROKE); // set the hollow paint brush and draw paint3.setAlpha (alpha3) in a circular manner );}}