Water ripple effect ---- single ring ripple

Source: Internet
Author: User

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 ;}}

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.