码迷,mamicode.com
首页 > 其他好文 > 详细

水波纹效果----单环波纹

时间:2014-12-08 15:34:49      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:水波纹   单环波纹   

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 lixiao
 *
 */
public class WaterView extends View{
	
	/**
	 * 不透明度(完全透明:0)
	 */
	private int alpha;
	private int radius;
	
	private Paint mPaint;
	
	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(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);
	}
	
	@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);		
		//画圆
		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;//控制速度,值越大,圆环扩大越快
		
		if(alpha <= 0){
			
			alpha = 0 ;
		}
		mPaint.setAlpha(alpha);

	}
	
	
	private void initPaint() {
		//新建一个画笔
		mPaint = new Paint();
		mPaint.setAntiAlias(true);//设置抗锯齿
		mPaint.setStrokeWidth((float) 1);//设置线宽10
		mPaint.setStyle(Paint.Style.STROKE);//设置空心画笔,环形方式绘制
		
		mPaint.setAlpha(alpha);
		mPaint.setColor(Color.BLUE);
	}
	
	
	/**
	 * 绘制圆形图片(暂时没用到)
	 * @param source 图片资源
	 * @param min 直径
	 * @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);
		/**
		 * 产生同样大小的画布
		 */
		Canvas canvas = new Canvas(target);
		
		/**
		 * 首先绘制圆形
		 */
		canvas.drawCircle(widthScreen/2, this.getHeight()/2, min/2, paint);
		/**
		 * 使用SRC_IN(关键代码)
		 */
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		/**
		 * 缩放图片
		 */
		 //获取这个图片的宽和高
        int width = source.getWidth();
        int height = source.getHeight();
		//计算缩放率,新尺寸除原始尺寸
        float scaleWidth = ((float) min) / width;
        float scaleHeight = ((float) min) / height;
        
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);
        
        //旋转图片 动作
//        matrix.postRotate(45);
        
        // 创建新的图片
        Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0,
        width, height, matrix, true);
		/**
		 * 绘制图片
		 */		
		canvas.drawBitmap(newBitmap, 0, 0, paint);
		
		
		return target;
	}

	

}

水波纹效果----单环波纹

标签:水波纹   单环波纹   

原文地址:http://blog.csdn.net/u013372185/article/details/41804207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!