Water ripple effect ---- single ring 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 lixiao **/public class WaterView extends View {/*** opacity (completely transparent: 0) */private int alpha; private int radius; private Paint mPaint; private int widthScreen; private int heightScreen; private Handler mHandler;/*** image diameter */private static final int MIN = 50; public WaterView (Context contex T, 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 (alpha! = 0) {mHandler. sendEmptyMessageDelayed (0, 50);} else {mHandler. sendEmptyMessage (1);} break; case 1: init (); 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); // draw a circle canvas. drawCircle (widthScreen/2, this. getHeight ()/2, radius, mPaint);} private void init () {alpha = 255; radius = MIN/2; initPaint ();} private void flushState () {radius + = 10; alpha-= 10; // control speed. The greater the value, the faster the ring is. if (alpha <= 0) {alpha = 0;} mPaint. setAlpha (alpha);} private void initPaint () {// create a Paint brush mPaint = new Paint (); mPaint. setAntiAlias (true); // sets the anti-sawtooth mPaint. setStrokeWidth (float) 1); // you can specify the 10mPaint. setStyle (Paint. style. STROKE); // set the hollow brush and draw the mPaint in a ring mode. setAlpha (alpha); mPaint. setColor (Color. BLUE);}/*** draw a circular image (temporarily unavailable) * @ param source image resource * @ param min diameter * @ return */private Bitmap createCircleBitmap (Bitmap source, int min) {Paint paint = new Paint (); paint. setColor (Color. parseColor ("# FFFFFF"); paint. setAntiAlias (true); // Bitmap target = Bitmap. createBitmap (min, min, Config. ARGB_8888);/*** generate a Canvas of the same size */canvas Canvas = new canvas (target);/*** draw a circle first */Canvas. drawCircle (widthScreen/2, this. getHeight ()/2, min/2, paint);/*** use SRC_IN (key code) */paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_IN);/*** scale the image * // obtain the width and height of the image. int width = source. getWidth (); int height = source. getHeight (); // calculate the scaling rate. The new size includes the original size float scaleWidth = (float) min)/width; float scaleHeight = (float) min)/height; // create a matrix object for image operations. Matrix matrix = new Matrix (); // scale the image action matrix. postScale (scaleWidth, scaleHeight); // rotate the image // matrix. postRotate (45); // create a new image Bitmap newBitmap = Bitmap. createBitmap (source, 0, 0, width, height, matrix, true);/*** draw an image */canvas. drawBitmap (newBitmap, 0, 0, paint); return target ;}}