admin管理员组

文章数量:1122846

I can't get anything to show up on screen. I have created a Empty Views Action then written the following code. (Copied from youtube where it works)

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
    private Paint redpaint = new Paint();
    public GamePanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        redpaint.setColor(Color.RED);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        Canvas c = holder.lockCanvas();
        c.drawRect(50,50,100,100,redpaint);
        holder.unlockCanvasAndPost(c);
    }

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

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GamePanel(this));
    }
}

I have checked all kinds of things:

  • I have changed the background color to make sure I can see the view
  • I have checked that c (the canvas from lockCanvas()) is not null
  • I have checked that visibility for this and parent is 0

And all other kinds of checks that ChatGPT said I should do

I have now narrowed the problem down to the emulator. I tried connecting my phone which seemed to fix the problem. However, since I would like to use the emulator this still poses a problem.

The emulator seems to be working fine but whatever I draw to the screen never shows up.

本文标签: javaCant draw anything on canvas using surfaceViewStack Overflow