2

我正在尝试为我的 android 游戏制作一个浮动操纵杆,但我无法这样做。这是我的 onTouch() 函数:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    c = holder.lockCanvas();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        float x = event.getX();
        float y = event.getY();
        Log.d("joystick", " " + x + " " + y);
        ondraw(c, x, y);
        break;

    case MotionEvent.ACTION_MOVE:
        d = holder.lockCanvas();
        float xdrag = event.getX();
        float ydrag = event.getY();
        Log.d("joystick move", " " + xdrag + " " + ydrag);
        ondraw(d, xdrag, ydrag);
        break;
    }

    return false;
}

由于某种原因,图像没有拖动。有人可以在这里告诉问题并帮助我吗?

我的抽签是:

protected void ondraw(Canvas c, float x, float y) {

    c.drawColor(Color.BLACK);
    c.drawBitmap(bmpback, x - (bmpback.getWidth() / 2),
            y - (bmpback.getHeight() / 2), null);
    c.drawBitmap(bmpfront, x - (bmpfront.getWidth() / 2),
            y - (bmpfront.getHeight() / 2), null);
    holder.unlockCanvasAndPost(c);
}
4

1 回答 1

1

像这样简单地拖动图像

@Override
public boolean onTouchEvent(MotionEvent event) {

    x = event.getX();
    y = event.getY();
    //Log.d("joy", "x = " + x + "y =" + y);

    invalidate();
    return true;
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawBitmap(joyback, x - joyback.getWidth() / 2,
            y - joyback.getHeight() / 2, null);



    super.onDraw(canvas);
}
于 2012-07-06T08:07:24.757 回答