如何滚动关闭屏幕内存位图 [英] How to scroll off screen memory bitmap

查看:151
本文介绍了如何滚动关闭屏幕内存位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面code滚动
我有一个Surfaceview线程和正在生成的(改变)一个关闭帆布质地的位图,第一行(行),每一帧再复制一个位置(线)向下定期surfaceview位图进行滚动效果,然后我继续画最重要的是其他东西。嗯,这是我真正想要的,但我不能让它即使我创造了关闭屏幕的位图单独帆布工作。这仅仅是根本滚动。

Using below code to scroll I have a Surfaceview thread and an off canvas texture bitmap that is being generated (changed), first row (line), every frame and then copied one position (line) down on regular surfaceview bitmap to make a scrolling effect, and I then continue to draw other things on top of that. Well that is what I really want, however I can't get it to work even though I am creating a separate canvas for off screen bitmap. It is just not scrolling at all.

我换句话说,我有一个内存位图,大小相同Surfaceview的画布,我需要滚动(移)下移一行每一帧,然后更换新的随机纹理顶线,然后画上经常Surfaceview帆布

I other words I have a memory bitmap, same size as Surfaceview canvas, which I need to scroll (shift) down one line every frame, and then replace top line with new random texture, and then draw that on regular Surfaceview canvas.

下面是我想会的工作;

Here is what I thought would work;

我surfaceChanged在那里我指定的位图和画布,并开始螺纹:

My surfaceChanged where I specify bitmap and canvasses and start thread:

@Override
    public void surfaceCreated(SurfaceHolder holder) {
    intSurfaceWidth = mSurfaceView.getWidth();
    intSurfaceHeight = mSurfaceView.getHeight();

    memBitmap = Bitmap.createBitmap(intSurfaceWidth, intSurfaceHeight,
            Bitmap.Config.ARGB_8888);
    memCanvas = new Canvas(memCanvas);

    myThread = new MyThread(holder, this);
    myThread.setRunning(true);
    blnPause = false;
    myThread.start();
}

我的线索,只显示必要的中间传动部分:

My thread, only showing essential middle running part:

@Override
public void run() {
  while (running) {     
    c = null;
try {
    // Lock canvas for drawing
    c = myHolder.lockCanvas(null);                  

    synchronized (mSurfaceHolder) {
        // Scroll down off screen canvas and hopefully underlying bitmap bitmap
        Rect src = new Rect(0, 0, intSurfaceWidth, intSurfaceHeight - 1);
        Rect dst = new Rect(0, 1, intSurfaceWidth, intSurfaceHeight);
        memCanvas.drawBitmap(memBitmap, src, dst, null);

        // Note scrolling up like this works fine
        // Rect src = new Rect(0, 1, intSurfaceWidth, intSurfaceHeight + 1);
        // Rect dst = new Rect(0, 0, intSurfaceWidth, intSurfaceHeight);
        // memCanvas.drawBitmap(memBitmap, src, dst, null);

        // Create random one line(row) texture bitmap
        textureBitmap = Bitmap.createBitmap(imgTexture, 0, rnd.nextInt(intTextureImageHeight), intSurfaceWidth, 1);

        // Now add this texture bitmap to top of screen canvas and hopefully underlying bitmap
        memCanvas.drawBitmap(textureBitmap,
                intSurfaceWidth, 0, null);

        // Draw above updated off screen bitmap to regular canvas, at least I thought it would update (save changes) shifting down and add the texture line to off screen bitmap the off screen canvas was pointing to.
        c.drawBitmap(memBitmap, 0, 0, null);

        // Other drawing to canvas comes here


    } finally {
        // do this in a finally so that if an exception is thrown
        // during the above, we don't leave the Surface in an
        // inconsistent state
        if (c != null) {
            myHolder.unlockCanvasAndPost(c);
        }
    }
}       
}

有关我的游戏隧道运行, https://market.android.com/详细信息?ID = com.zubima.TunnelRun ,现在我有一个有效的解决方案,我不是有位图,表面高度的大小的数组,我填充我的随机纹理,然后在一个循环下移每个帧。我得到每秒50帧,但我想我可以代替滚动位图做的更好。

For my game Tunnel Run, https://market.android.com/details?id=com.zubima.TunnelRun, right now I have a working solution where I instead have an array of bitmaps, size of surface height, that I populate with my random texture and then shift down in a loop for each frame. I get 50 frames per second, but I think I can do better by instead scrolling bitmap.

推荐答案

您正试图借鉴其本身上,这是从来没有在Android AFAIK工作的位图。我掉进了我的第一场比赛,一个横向卷轴叫鼹鼠矿工这个确切的洞。

You are trying to draw a bitmap on top of itself, something which has never worked on Android AFAIK. I fell into this exact hole with my first game, a side-scroller called Mole Miner.

问题很简单:drawBitmap(),在本质上,一种特殊的memcpy()的字节将始终解决的转发的。这是很好的,并且使用L1缓存优化等,但你显然打了一个问题,如果源和目标区域(A)的重叠,以及(b)源地址< DEST地址。

The problem is very simple: the drawBitmap() is, in essence, a special kind of memcpy() in which bytes are always addressed forwards. Which is fine, and uses L1 cache optimally, etc, but you obviously hit a problem if the source and destination areas (a) overlap, and (b) source address < dest address.

(的memcpy()的真正实现,顺便说一下,检查是否有这种可能性,如果需要向后做副本。Canvas.drawBitmap()不这样做。)

(Real implementations of memcpy(), by the way, check for this possibility and do the copy backwards if necessary. Canvas.drawBitmap() does not do this.)

我用鼹鼠矿工的解决办法是让的两个的屏幕外的位图,并在交替帧它们之间进行切换。但是,这是在2009年,这几天我可能不会使用在所有的滚动。

The workaround I used for Mole Miner was to have two off-screen bitmaps, and switch between them on alternate frames. But that was in 2009 and these days I'd probably not use scrolling at all.

这篇关于如何滚动关闭屏幕内存位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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