How to scroll a bitmap from a screen

Using below code for scrolling I have a Surfaceview stream and a canvas bitmap that is generated (changed), the first line (line), each frame, and then one position (line) is copied down to a regular bitmap to view the scroll, and then I continue to draw other things on top of this. Well, that’s what I really want, however I cannot get it to work, although I am creating a separate canvas for a bitmap without a screen. It just isn't scrolling.

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

Here is what I thought would work;

My surfaceChanged, where I point the bitmap and the canvas and start the stream:

@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 showing only the middle 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);
        }
    }
}       
}

Tunnel Run, https://market.android.com/details?id=com.zubima.TunnelRun, , , , , . 50 , , , .

+2
2

, Android AFAIK. , - Mole Miner.

: drawBitmap() - , , memcpy(), . , - L1 .., , , , () , (b) < dest.

( memcpy(), , . Canvas.drawBitmap() .)

, Mole Miner, , . 2009 , , , .

+1

drawBitmap :

public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint);

, src

 Rect(0,offset,width,height+offset)

.

0

Source: https://habr.com/ru/post/1780251/


All Articles