绘制多个矩形而不调用 invalidate()

发布于 2024-11-14 02:38:27 字数 1044 浏览 4 评论 0原文

我正在尝试绘制多个矩形。我希望能够手工绘制每个矩形。我可以绘制一个,但是一旦我调用 invalidate(),当然,画布就会被清除。 是否有另一种方法来调用 onDraw() 以便画布不会被清除? 这就是我所拥有的:

我只是有一个扩展 SurfaceView 的类,然后 覆盖 onDraw

@Override
protected void onDraw(Canvas canvas) 
{
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    canvas.drawRect(xCoor, yCoor, rectW, rectH, paint);
}

然后覆盖 OnTouchEvent

@Override
public boolean onTouchEvent (MotionEvent event) 
{

    downX = Math.round(event.getX());
    downY = Math.round(event.getY());

    invalidate();    //clears canvas which I don't want
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            xCoor = downX;
            yCoor = downY;
            rectH = 0;
            rectW = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            rectH = downY;
            rectW = downX;
            break;
        case MotionEvent.ACTION_UP:

            break;
    }

    return true;
}

我这样做完全错误吗? :)

任何帮助将不胜感激。

谢谢!

I'm trying to draw multiple rectangles. I want to be able to draw each rectangle by hand though. I can draw one but then once I call invalidate(), of course, the canvas gets cleared.
Is there another way to call the onDraw() so that the canvas doesn't get cleared?
Here's what I have:

I simply have a class which extends a SurfaceView and then
Override the onDraw

@Override
protected void onDraw(Canvas canvas) 
{
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    canvas.drawRect(xCoor, yCoor, rectW, rectH, paint);
}

And then I Override an OnTouchEvent

@Override
public boolean onTouchEvent (MotionEvent event) 
{

    downX = Math.round(event.getX());
    downY = Math.round(event.getY());

    invalidate();    //clears canvas which I don't want
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            xCoor = downX;
            yCoor = downY;
            rectH = 0;
            rectW = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            rectH = downY;
            rectW = downX;
            break;
        case MotionEvent.ACTION_UP:

            break;
    }

    return true;
}

Am I doing this completely wrong? :)

Any help would be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-11-21 02:38:27

您可以将每个矩形添加到列表中并迭代它的onDraw,如下所示:

import android.graphics.Rect;

private ArrayList<Rect> rectangles = new ArrayList<Rect>();

@Override
public boolean onTouchEvent (MotionEvent event) {
    // ...
    case MotionEvent.ACTION_UP:
        rectangles.add(new Rect(xCoor, yCoor, rectW, rectH));
        break;
    // ...
}

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    for (Rect rect : rectangles) {
        canvas.drawRect(rect, paint);
    }
}

You could add each rectangle to a list and iterate it onDraw like this:

import android.graphics.Rect;

private ArrayList<Rect> rectangles = new ArrayList<Rect>();

@Override
public boolean onTouchEvent (MotionEvent event) {
    // ...
    case MotionEvent.ACTION_UP:
        rectangles.add(new Rect(xCoor, yCoor, rectW, rectH));
        break;
    // ...
}

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    for (Rect rect : rectangles) {
        canvas.drawRect(rect, paint);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文