package com.lix.shuobowen.bo_4;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/5/11.
*/
public class MyWaterRound extends View {
//1,创建Wave类保存需要的数据
//2,创建List保存,并在有参构造方法里初始化
List<Wave> waveList;
//3,透明度的最的值和最小值-1,标记属性
int maxAlpha = 255;
int flushAll = -1;
boolean flag = true;
int[] colors = {Color.RED,Color.GREEN,Color.YELLOW,Color.BLUE,Color.BLACK,Color.GRAY};
public MyWaterRound(Context context, AttributeSet attrs) {
super(context, attrs);
waveList = new ArrayList<Wave>();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
//4,绘制--(显示List里保存的圆圈)
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
canvas.drawCircle(wave.xDown,wave.yDown,wave.radius,wave.paint);
}
}
//5,初始化画笔
private Paint initPaint(int alpha,float width){
Paint paint = new Paint();
paint.setAntiAlias(true);//抗距齿
paint.setStyle(Paint.Style.STROKE);//空心圆
paint.setStrokeWidth(width);//圆环的宽度
paint.setAlpha(alpha);//透明度
paint.setColor(colors[(int) (Math.random() * (colors.length - 1))]);
return paint;
}
//6.实现onTouchEvent方法,为Wave的属性赋值
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN://手指按下
//实例化Wave对象,为属性赋值
setWave(event);
break;
case MotionEvent.ACTION_MOVE://手指移动
setWave(event);
break;
case MotionEvent.ACTION_UP://手指抬起
break;
}
return true;
}
//7 实现hander方法
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0){
//8 刷新当前圆的状态,
flushState();
invalidate();
//判断集合和圆环的透明度,
if(waveList.size()>0 && waveList !=null ){
handler.sendEmptyMessageDelayed(0,100);
}
}
}
};
/**
* 刷新状态
*/
private void flushState() {
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
//当标记为false并且圆环完全透明的时候,移除当前对象
if(flag==false && wave.alpha==0){
waveList.remove(i);
}else{
flag = false;
}
//更新圆环的状态
wave.radius +=20;
wave.width = wave.radius/5;
wave.alpha -=10;
if(wave.alpha<=10){
wave.alpha=0;
}
wave.paint.setAlpha(wave.alpha);
wave.paint.setStrokeWidth(wave.width);
}
}
/**
* 触发onTouchEvent方法是,为Wave对象赋值
* @param event
*/
private void setWave(MotionEvent event) {
Wave wave = new Wave();
wave.radius=0;
wave.alpha=maxAlpha;
wave.width=wave.radius/4;
wave.xDown = (int) event.getX();
wave.yDown = (int) event.getY();
wave.paint = initPaint(wave.alpha,wave.width);
//判断手指是否是第一次点击
if(waveList.size()==0){
flag=true;
}
//把信息添加到集合
waveList.add(wave);
//刷新,重新调用onDraw()方法
invalidate();
//判断标记,向hander发信号
if(flag){
handler.sendEmptyMessage(0);
}
}
//创建一个内部类.保存画圆需要的属性
class Wave{
int cx; //x坐标
int cy; //y坐标
float radius;//圆半径
Paint paint;//画笔
int xDown;//按下的x坐标
int yDown;//按下的y坐标
float width;//圆环的宽
int alpha;//透明度
}
}
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/5/11.
*/
public class MyWaterRound extends View {
//1,创建Wave类保存需要的数据
//2,创建List保存,并在有参构造方法里初始化
List<Wave> waveList;
//3,透明度的最的值和最小值-1,标记属性
int maxAlpha = 255;
int flushAll = -1;
boolean flag = true;
int[] colors = {Color.RED,Color.GREEN,Color.YELLOW,Color.BLUE,Color.BLACK,Color.GRAY};
public MyWaterRound(Context context, AttributeSet attrs) {
super(context, attrs);
waveList = new ArrayList<Wave>();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
//4,绘制--(显示List里保存的圆圈)
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
canvas.drawCircle(wave.xDown,wave.yDown,wave.radius,wave.paint);
}
}
//5,初始化画笔
private Paint initPaint(int alpha,float width){
Paint paint = new Paint();
paint.setAntiAlias(true);//抗距齿
paint.setStyle(Paint.Style.STROKE);//空心圆
paint.setStrokeWidth(width);//圆环的宽度
paint.setAlpha(alpha);//透明度
paint.setColor(colors[(int) (Math.random() * (colors.length - 1))]);
return paint;
}
//6.实现onTouchEvent方法,为Wave的属性赋值
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN://手指按下
//实例化Wave对象,为属性赋值
setWave(event);
break;
case MotionEvent.ACTION_MOVE://手指移动
setWave(event);
break;
case MotionEvent.ACTION_UP://手指抬起
break;
}
return true;
}
//7 实现hander方法
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0){
//8 刷新当前圆的状态,
flushState();
invalidate();
//判断集合和圆环的透明度,
if(waveList.size()>0 && waveList !=null ){
handler.sendEmptyMessageDelayed(0,100);
}
}
}
};
/**
* 刷新状态
*/
private void flushState() {
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
//当标记为false并且圆环完全透明的时候,移除当前对象
if(flag==false && wave.alpha==0){
waveList.remove(i);
}else{
flag = false;
}
//更新圆环的状态
wave.radius +=20;
wave.width = wave.radius/5;
wave.alpha -=10;
if(wave.alpha<=10){
wave.alpha=0;
}
wave.paint.setAlpha(wave.alpha);
wave.paint.setStrokeWidth(wave.width);
}
}
/**
* 触发onTouchEvent方法是,为Wave对象赋值
* @param event
*/
private void setWave(MotionEvent event) {
Wave wave = new Wave();
wave.radius=0;
wave.alpha=maxAlpha;
wave.width=wave.radius/4;
wave.xDown = (int) event.getX();
wave.yDown = (int) event.getY();
wave.paint = initPaint(wave.alpha,wave.width);
//判断手指是否是第一次点击
if(waveList.size()==0){
flag=true;
}
//把信息添加到集合
waveList.add(wave);
//刷新,重新调用onDraw()方法
invalidate();
//判断标记,向hander发信号
if(flag){
handler.sendEmptyMessage(0);
}
}
//创建一个内部类.保存画圆需要的属性
class Wave{
int cx; //x坐标
int cy; //y坐标
float radius;//圆半径
Paint paint;//画笔
int xDown;//按下的x坐标
int yDown;//按下的y坐标
float width;//圆环的宽
int alpha;//透明度
}
}