Android,画布:我如何清除(删除)画布(=位图),生活在一个表面视图中? [英] Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

查看:45
本文介绍了Android,画布:我如何清除(删除)画布(=位图),生活在一个表面视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了制作一个简单的游戏,我使用了一个模板来绘制一个带有位图的画布,如下所示:

In order to make a simple game, I used a template that draws a canvas with bitmaps like this:

private void doDraw(Canvas canvas) {
    for (int i=0;i<8;i++)
        for (int j=0;j<9;j++)
            for (int k=0;k<7;k++)   {
    canvas.drawBitmap(mBits[allBits[i][j][k]], i*50 -k*7, j*50 -k*7, null); } }

(画布在run()"中定义/SurfaceView 存在于 GameThread 中.)

(The canvas is defined in "run()" / the SurfaceView lives in a GameThread.)

我的第一个问题是如何清除(或重绘)新布局的整个画布?
其次,如何只更新屏幕的一部分?

My first question is how do I clear (or redraw) the whole canvas for a new layout?
Second, how can I update just a part of the screen?

// This is the routine that calls "doDraw":
public void run() {
    while (mRun) {
        Canvas c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if (mMode == STATE_RUNNING) 
                    updateGame();
                doDraw(c);          }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);  }   }   }       }

推荐答案

我如何清除(或重绘)整个画布以获得新的布局(= 尝试游戏)?

How do I clear (or redraw) the WHOLE canvas for a new layout (= try at the game) ?

只需调用 Canvas.drawColor(Color.BLACK),或者任何你想要清除Canvas 的颜色.

以及:如何只更新屏幕的一部分?

And: how can I update just a part of the screen ?

没有这样的方法可以只更新屏幕的一部分",因为 Android 操作系统在更新屏幕时会重新绘制每个像素.但是,当您没有清除 Canvas 上的旧绘图时,旧绘图仍然在表面上,这可能是仅更新一部分"屏幕的一种方法.

There is no such method that just update a "part of the screen" since Android OS is redrawing every pixel when updating the screen. But, when you're not clearing old drawings on your Canvas, the old drawings are still on the surface and that is probably one way to "update just a part" of the screen.

因此,如果您想更新屏幕的一部分",请避免调用 Canvas.drawColor() 方法.

So, if you want to "update a part of the screen", just avoid calling Canvas.drawColor() method.

这篇关于Android,画布:我如何清除(删除)画布(=位图),生活在一个表面视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆