画布不会画圆

时间:2018-07-07 16:52:54

标签: android canvas draw

我正在尝试使用自定义视图制作一个小型的乒乓球游戏,该视图在每侧绘制平台并将球绘制为一个圆圈。但是,当我运行该应用程序以查看绘图是否正确完成时,尽管平台绘制正确,但圆圈不会出现。所以我的问题是,为什么根本不拉球? 这是自定义视图:

public class GameView extends SurfaceView implements Runnable {
private Context mContext; // a private instance of the context
Thread gameThread=null; //the thread on which the game will run
Ball ball; //a ball
Platform lPlat,rPlat; // a platform for the left side and the right
//tools that will draw the view and the objects
Canvas canvas;
Paint paint;
Color color;
SurfaceHolder mHolder;
//variable for frame management and objects movement
long lastFrame;
long fps;

volatile boolean playing;

public GameView(Context context){
    super(context);
    this.mContext=context;
    mHolder=getHolder();
    color=new Color();
    paint=new Paint();
    gameThread=new Thread(this);
    lPlat=new Platform(2592,620,50,200);
    rPlat=new Platform(120,620,50,200);
    ball=new Ball(2400,620,20);
    setWillNotDraw(false);
    init();
}
public void init() {
    render();
    playing=true;
    gameThread.start();
}
//the game Thread
@Override
public void run(){
    while (playing){
        long currentTime=System.currentTimeMillis();
        render();
        lastFrame=System.currentTimeMillis()-currentTime;
        //dividing the time difference between the start of the count and the end of the render (which is a frame) by 1000 to get an approximation of the fps;
        if (lastFrame>=1)
            fps=1000/lastFrame;
    }
}
//renders the view, calling update method, and draw method afterwards
public void render(){
    update();
    draw();
}
public void update(){
    ball.updateBallPosition();
    rPlat.updatePlatformPosition();
    lPlat.updatePlatformPosition();
}
@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    Log.i("@CanvasStatus","Canvas onDraw");
    canvas.drawColor(Color.argb(255, 100, 120, 70));

    //the drawRect methods work properly
    canvas.drawRect(lPlat.shape,paint);
    canvas.drawRect(rPlat.shape,paint);
    canvas.drawCircle(ball.x,ball.y,ball.radius,paint); //this is where I try to draw the circle 
}


public void draw(){
    if (mHolder.getSurface().isValid()) {
        Log.i("@CanvasStatus","valid");
        canvas=mHolder.lockCanvas();
        paint.setColor(Color.argb(255,10,200,157));
        paint.setStyle(Paint.Style.FILL);
        this.draw(canvas);

        mHolder.unlockCanvasAndPost(canvas);
    }
    else  Log.i("@CanvasStatus","invalid");
}
}

这是球类:

public class Ball {
float x;
float y;
float radius;
double yspeed, xspeed;

public Ball(float x, float y, float radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    yspeed = 200;
    xspeed = 200;
}

public void updateBallPosition() {
    x+=xspeed;
    y+=yspeed;
    //checks for wall collisions
    if(x>=GameActivity.screenCoordinates.x) {
        x = GameActivity.screenCoordinates.x;
        xspeed=-xspeed;
    }
    else if (x<=0){
        x=0;
        xspeed=-xspeed;
    }

}

}

并不是关于游戏的所有事情都实现了,我只是想让画布此时正确地绘制对象。 预先感谢

1 个答案:

答案 0 :(得分:0)

您不需要重写onDraw方法并在draw(canvas)方法中调用draw,画布的所有工作都应在此完成。删除onDraw方法并更改draw方法,如下所示:

public void draw(){
    if (mHolder.getSurface().isValid()) {
        Log.i("@CanvasStatus","valid");
        canvas=mHolder.lockCanvas();
        paint.setColor(Color.argb(255,10,200,157));
        paint.setStyle(Paint.Style.FILL);

        canvas.drawColor(Color.argb(255, 100, 120, 70));

        //the drawRect methods work properly
        canvas.drawRect(lPlat.shape,paint);
        canvas.drawRect(rPlat.shape,paint);
        canvas.drawCircle(ball.x,ball.y,ball.radius,paint);

        mHolder.unlockCanvasAndPost(canvas);
    }
    else  Log.i("@CanvasStatus","invalid");
}

顺便说一句,您仅为x坐标设置了边界,还应该为y添加坐标。除此之外,您可能需要为画布绘制添加一些延迟(在最简单的情况下使用Thread.sleep),否则您的视图将过于频繁地重新绘制。