程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-求教Android多點觸控屏幕顯示觸摸點的問題

android-求教Android多點觸控屏幕顯示觸摸點的問題

編輯:編程綜合問答
求教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);
}

}


最佳回答:


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

}你這個是單點觸摸的程序,所以只會相應第一個手指按下,你要改為多點相應的.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved