How to scroll off screen memory bitmap

跟風遠走 提交于 2019-12-06 08:54:28

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.

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.

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

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.

There is a drawBitmap function within canvas with the following signature:

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

You can make your dst the size of the canvas, and your src can be set up to be new

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

You can then increment offset every frame and get a smoothly scrolling bitmap in your view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!