想試試Android的多點觸控,三根手指依次瞬間按下屏幕(按下的手指不抬起,最後同時抬起),需求是在按下的地方同時顯示水波紋,現在這個代碼執行起來只是在第一根手指按下的地方顯示水波紋,請各位大神指教。
```package com.waterwave;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MultWave extends View {
private ArrayList<myWave> list;
private boolean isRuning;
public MultWave(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initView();
}
private void initView() {
// TODO Auto-generated method stub
list = new ArrayList<MultWave.myWave>();
}
public MultWave(Context context) {
super(context);
// TODO Auto-generated constructor stub
initView();
}
private class myWave {
int cx;
int cy;
Paint paint;
int r;
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
flushData();
invalidate();
if (isRuning) {
handler.sendEmptyMessageDelayed(0, 50);
}
}
};
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
protected void flushData() {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
myWave wave = list.get(i);
int alpha = wave.paint.getAlpha();
if (alpha == 0) {
list.remove(i);
continue;
}
alpha -= 5;
if (alpha < 5) {
alpha = 0;
}
wave.paint.setAlpha(alpha);
wave.r += 3;
wave.paint.setStrokeWidth(wave.r / 3);
}
if (list.size() == 0) {
isRuning = false;
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
myWave wave = list.get(i);
canvas.drawCircle(wave.cx, wave.cy, wave.r, wave.paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
addPoint(x, y);
break;
case MotionEvent.ACTION_DOWN:
int x1 = (int) event.getX();
int y1 = (int) event.getY();
addPoint(x1, y1);
break;
}
return true;
}
private void addPoint(int x, int y) {
// TODO Auto-generated method stub
if (list.size() == 0) {
add2list(x, y);
isRuning = true;
handler.sendEmptyMessageDelayed(0, 200);
} else {
add2list(x, y);
}
}
private void add2list(int x, int y) {
// TODO Auto-generated method stub
myWave wave = new myWave();
wave.cx = x;
wave.cy = y;
wave.paint = new Paint();
wave.paint.setAntiAlias(true);
wave.paint.setAlpha(255);
wave.paint.setStyle(Style.STROKE);
wave.paint.setColor(Color.GREEN);
list.add(wave);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
addPoint(x, y);
break;
case MotionEvent.ACTION_DOWN:
int x1 = (int) event.getX();
int y1 = (int) event.getY();
addPoint(x1, y1);
break;
}
return true;
}你這個是單點觸摸的程序,所以只會相應第一個手指按下,你要改為多點相應的.