2 fbyfbyfby fbyfbyfby 于 2015.07.02 01:49 提问

求教Android多点触控屏幕显示触摸点的问题

想试试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);
}

}


4个回答

Trent1985
Trent1985   2015.07.02 11:39
已采纳

@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;

}你这个是单点触摸的程序,所以只会相应第一个手指按下,你要改为多点相应的.

bdmh
bdmh   Ds   Rxr 2015.07.02 08:44

addPoint方法中,只有第一次才会sendEmptyMessageDelayed,也就是通知了一次而已

Trent1985
Trent1985   2015.07.02 11:42
gggfddhj
gggfddhj   2015.07.02 16:24

AddPoint方法中,只有第一次才会sendEmptyMessageDelayed

Csdn user default icon
上传中...
上传图片
插入图片