1

我想做这个:

    • 当我触摸屏幕时,我想为触摸的 x,y 坐标创建一个新的椭圆
    • 当我在屏幕上拖动手指时,我想为拖动过程中手指所在的每个 x,y 坐标创建新的椭圆。
    • 当我触摸并将手指放在屏幕上(长按)时,我想删除我创建的每个椭圆

我可以做第一步,但我不知道如何做 2. 和 3.

这是我尝试过的代码:

public class BouncingBallView extends View implements OnGestureListener{

private List<Oval> ovals = new ArrayList<Oval>();
private GestureDetector gestureScanner;

public BouncingBallView(Context context) {
    super(context);
    setBackgroundColor(Color.WHITE);
    gestureScanner = new GestureDetector(this);
}

@Override
protected void onDraw(Canvas canvas) {
    for(Oval o: ovals) { //prints every oval
        o.graph(canvas);
    } 
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    //1. this works:
    float x = event.getX();
    float y = event.getY();
    Oval oval = new Oval(x, y, this);
    ovals.add(oval);
    invalidate();
    return super.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent arg0) {
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float x,
        float y) {
    //2. this doesn't work:
    Oval oval = new Oval(x, y, this);
    ovals.add(oval);
    invalidate();
    return true;
}

@Override
public void onLongPress(MotionEvent arg0) {
    //3. this doesn't work:
    ovals.clear();
    invalidate();
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float x,
        float y) {
    //2. this doesn't work:
    Oval oval = new Oval(x, y, this);
    ovals.add(oval);
    invalidate();
    return true;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

}

您可以看到,我不使用“gestureScanner”参数,也许我应该使用,但我不知道在哪里以及如何使用!

请帮我!谢谢!

4

0 回答 0