水波纹效果
``
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.myandroid_test.WaterWave
android:layout_width="match_parent"
android:layout_height="match_parent"
</RelativeLayout>
- 自定义Vi package com.example.myandroid_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class WaterWave extends View {
private List<Wave> waveList;
private static final int MAX_ALPHA = 255;
protected static final int FLUSH_ALL = -1;
private boolean isStart = true;
private int xDown;
private int yDown;
private float radius;
private int alpha;
private int[] colors = { Color.BLUE, Color.RED, Color.YELLOW, Color.DKGRAY,
Color.GREEN, Color.LTGRAY, Color.MAGENTA };
public WaterWave(Context context, AttributeSet attrs) {
super(context, attrs);
waveList = Collections.synchronizedList(new ArrayList<Wave>());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
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);
}
}
private Paint initPaint(int alpha, float width) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setStyle(Paint.Style.STROKE);
paint.setAlpha(alpha);
paint.setColor(colors[(int) (Math.random() * (colors.length - 1))]);
return paint;
}
private Handler hd = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
flushState();
invalidate();
if (waveList != null && waveList.size() > 0) {
hd.sendEmptyMessageDelayed(0, 100);
}
}
};
};
private void flushState() {
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
if (isStart == false && wave.alpha == 0) {
waveList.remove(i);
wave.paint = null;
wave = null;
continue;
} else if (isStart == true) {
isStart = false;
}
wave.radius += 10;
wave.alpha -= 10;
if (wave.alpha < 0) {
wave.alpha = 0;
}
wave.width = wave.radius / 4;
wave.paint.setAlpha(wave.alpha);
wave.paint.setStrokeWidth(wave.width);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Wave wave = new Wave();
wave.radius = 0;
wave.alpha = MAX_ALPHA;
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) {
isStart = true;
}
waveList.add(wave);
invalidate();
if (isStart) {
hd.sendEmptyMessage(0);
}
case MotionEvent.ACTION_MOVE:
Wave wave1 = new Wave();
wave1.radius = 0;
wave1.alpha = MAX_ALPHA;
wave1.width = wave1.radius / 4;
wave1.xDown = (int) event.getX();
wave1.yDown = (int) event.getY();
wave1.paint = initPaint(wave1.alpha, wave1.width);
if (waveList.size() == 0) {
isStart = true;
}
waveList.add(wave1);
invalidate();
if (isStart) {
hd.sendEmptyMessage(0);
}
break;
default:
break;
}
return true;
}
private class Wave {
int waveX;
int waveY;
float radius;
Paint paint;
int xDown;
int yDown;
float width;
int alpha;
}
}