我如何获得球的连续运动? [英] How do I get continuous motion of ball?

查看:114
本文介绍了我如何获得球的连续运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的执行动作现在:

This is my implementation of movement right now:

 public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        move_ball=true;
        check_collisions();
        x+=speed_x;
        y+=speed_y;
        invalidate();

        Log.d("down", String.valueOf(x)+" "+String.valueOf(y));

        break;
    }



    case MotionEvent.ACTION_UP:
        move_ball=false;
        Log.d("up", String.valueOf(x)+" "+String.valueOf(y));


        break;
    }
return true;



};

但我想,只要用户有他的手指把球不断,而现在我需要不断地移动我的手指,以获得运动如上在屏幕上移动。

But what I want is as long as the user has his finger on the screen the ball keeps moving as above while right now I need to keep moving my finger to obtain the movement.

推荐答案

这是一个surfaceView一个完全工作的例子。看我是如何做到的,你必须抓住触摸坐标一次,然后将你的球在出现更新的方法。更改更新方法来改变球的运动。

This is a fully working example with a surfaceView. See how I did it, you have to grab the touch coordinates once and then move your ball there in an update method. Change the update method to change the balls movement.

public class BallView extends SurfaceView implements Runnable, SurfaceHolder.Callback {

private float ballX;
private float ballY;

private float touchX;
private float touchY;

private Paint ballPaint,backgroundPaint;
private ExecutorService service;

public BallView(Context context) {
    super(context);
    getHolder().addCallback(this);
    ballPaint = new Paint();
    ballPaint.setColor(Color.RED);
    backgroundPaint = new Paint();
    backgroundPaint.setColor(Color.BLACK);
}

public void update() {
    float deltaX = touchX - ballX;
    float deltaY = touchY - ballY;

    ballX+=deltaX/100;
    ballY+=deltaY/100;

}

public void doDraw(Canvas canvas) {
    canvas.drawPaint(backgroundPaint);
    canvas.drawCircle(ballX, ballY, 100, ballPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.i("Touch","Event");
    if(event.getAction()==MotionEvent.ACTION_UP) {
        touchX = event.getX();
        touchY = event.getY();
    }       
    return true;
}

private boolean running;
@Override
public void run() {
    running = true;
    while(running) {

        update();

        Canvas canvas = getHolder().lockCanvas();
        if(canvas!=null) {

            doDraw(canvas);

            getHolder().unlockCanvasAndPost(canvas);

        }

    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if(service == null) service = Executors.newSingleThreadExecutor();
    service.execute(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    running = false;
}

}

MainActivity

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(new BallView(this));
}

这篇关于我如何获得球的连续运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆