【问题标题】:Canvas doesn't draw circles画布不画圆圈
【发布时间】:2018-07-07 16:52:54
【问题描述】:

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

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

}

}

并不是所有的游戏都实现了,我现在只是想让画布正确地绘制对象。 提前致谢

【问题讨论】:

  • 您是否尝试将球涂成与周围环境不同的颜色?
  • 我做了,但无济于事:(

标签: android canvas draw


【解决方案1】:

你不需要重写onDraw 方法并在你的draw 方法中调用draw(canvas),所有使用画布的工作都应该在那里完成。删除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),否则您的视图将过于频繁地重新绘制。

【讨论】:

  • 感谢答案,方法的改变并没有帮助,但它确实让我的代码不那么麻烦。您对重新绘制视图和 y 边界是正确的。当我记录球的 x y 坐标时,y 坐标在 2 秒后达到十万,当我禁用更新方法时,球被正确绘制。我不敢相信在在这里发布我的问题之前我没有想过检查更新方法..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多