简体   繁体   English

Android:在画布上的另一个矩形上绘制矩形

[英]Android: Draw Rectangle Over Another Rectangle on Canvas

I'm trying to draw one rectangle that's light gray over another rectangle that's white on a canvas, but it doesn't seem to do anything. 我正在尝试在画布上的另一个白色矩形上绘制一个浅灰色的矩形,但是它似乎无能为力。 Here's what I've got: 这是我得到的:

public void onDraw(Canvas c) {
    Paint paint = new Paint();
    paint.setColor(Color.LTGRAY);
    c.drawRect(0, 0, width, height, paint);
    paint.setColor(Color.WHITE);
    c.drawRect(10, 10, width - 10, height - 10, paint); //This is slightly smaller than the gray rectangle, so it looks kinda like a border.
}

To display this, I use setContentView() in the main activity to set the view to a new class extending SurfaceView. 为了显示这一点,我在主要活动中使用setContentView()将视图设置为扩展SurfaceView的新类。 When the surface is created in the custom SurfaceView, it starts a thread that executes onDraw() every 100 milliseconds. 在自定义SurfaceView中创建表面时,它将启动一个线程,该线程每100毫秒执行一次onDraw()。 I got the Canvas by using holder.lockCanvas(), then onDraw()ing and holder.unlockCanvasAndPost(c). 我通过使用holder.lockCanvas(),然后onDraw()ing和holder.unlockCanvasAndPost(c)得到了Canvas。

Where are you initializing your width and height ? 您在哪里初始化widthheight Are they set to 0? 它们设置为0吗? Try calling canvas.getWidth() or canvas.getHeight() . 尝试调用canvas.getWidth()canvas.getHeight()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM