Circle.java
public class Circle {
private float cx;
private float cy;
private float radius;
private Paint paint;
private int alpha = 255;
public Circle() {
}
public Circle(float cx, float cy, float radius, Paint paint) {
this.cx = cx;
this.cy = cy;
this.radius = radius;
this.paint = paint;
}
public float getCx() {
return cx;
}
public void setCx(float cx) {
this.cx = cx;
}
public float getCy() {
return cy;
}
public void setCy(float cy) {
this.cy = cy;
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public int getAlpha() {
return alpha;
}
public void setAlpha(int alpha) {
this.alpha = alpha;
}
@Override
public String toString() {
return "Circle{" +
"cx=" + cx +
", cy=" + cy +
", radius=" + radius +
", paint=" + paint +
'}';
}
}
public class WaveView extends View {
private Random random = new Random();
private List<Circle> list;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
invalidate();
if (list != null && list.size() != 0) {
mHandler.sendEmptyMessageDelayed(0, 100);
}else{
mHandler.removeCallbacksAndMessages(null);
}
}
};
public WaveView1(Context context) {
super(context);
}
public WaveView1(Context context, AttributeSet attrs) {
super(context, attrs);
list = new ArrayList<>();
}
public WaveView1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
if(list!=null&&list.size()!=0){
for (int i = 0; i < list.size(); i++) {
Circle circle = list.get(i);
if(circle.getAlpha()==0){
list.remove(i);
i--;
}else{
canvas.drawCircle(circle.getCx(),circle.getCy(),circle.getRadius(),circle.getPaint());
circle.getPaint().setAlpha(circle.getAlpha() - 5);
circle.setAlpha(circle.getAlpha() - 5);
circle.setRadius(circle.getRadius()+5);
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
list.add(new Circle(x, y, 10, getPaint()));
invalidate();
mHandler.sendEmptyMessageDelayed(0, 100);
return true;
case MotionEvent.ACTION_MOVE:
Circle circle = new Circle(x, y, 10, getPaint());
list.add(circle);
invalidate();
mHandler.sendEmptyMessageDelayed(0, 100);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private Paint getPaint() {
Paint paint = new Paint();
paint = new Paint();
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setColor(random.nextInt(Integer.MAX_VALUE&0x00ffffff) | 0xff000000);
return paint;
}
}