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.PorterDuffXfermode; 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; /** * 水波纹View * @author alan * */ public class WaterView extends View{ /** * 不透明度(完全透明: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; /** * 图片直径 */ private static final int MIN = 50; public WaterView(Context context,Bitmap source) { super(context); init(); //获取屏幕的宽高 DisplayMetrics dm = getResources().getDisplayMetrics(); widthScreen = dm.widthPixels; heightScreen = dm.heightPixels; mHandler = new Handler(){ @Override public 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); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { Log.i("TAG", "onDraw......"); super.onDraw(canvas); //画圆 //c1(最小圆) canvas.drawCircle(widthScreen/2, this.getHeight()/2, radius1, paint1); //c2 canvas.drawCircle(widthScreen/2, this.getHeight()/2, radius2, paint2); //c3 canvas.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;//控制速度,值越大,圆环扩大越快 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() { //新建一个画笔 paint1 = new Paint(); paint1.setAntiAlias(true);//设置抗锯齿 paint1.setStrokeWidth((float) 1);//设置线宽1 paint1.setStyle(Paint.Style.STROKE);//设置空心画笔,环形方式绘制 paint1.setAlpha(alpha1); paint1.setColor(Color.BLUE); //定义画笔2 paint2 = new Paint(); paint2.setAntiAlias(true);//设置抗锯齿 paint2.setStrokeWidth((float) 1);//设置线宽1 paint2.setStyle(Paint.Style.STROKE);//设置空心画笔,环形方式绘制 paint2.setAlpha(alpha2); //定义画笔3 paint3 = new Paint(); paint3.setAntiAlias(true);//设置抗锯齿 paint3.setStrokeWidth((float) 1);//设置线宽1 paint3.setStyle(Paint.Style.STROKE);//设置空心画笔,环形方式绘制 paint3.setAlpha(alpha3); } }
原文地址:http://blog.csdn.net/u013372185/article/details/41806059