【发布时间】:2012-01-08 03:13:31
【问题描述】:
使用下面的代码滚动 我有一个 Surfaceview 线程和一个正在生成(更改)的画布外纹理位图,第一行(行),每一帧,然后在常规 Surfaceview 位图上复制一个位置(行)以产生滚动效果,然后我继续在此之上绘制其他东西。这就是我真正想要的,但是即使我正在为屏幕外位图创建单独的画布,我也无法让它工作。它根本不滚动。
换句话说,我有一个内存位图,大小与 Surfaceview 画布相同,我需要每帧向下滚动(移动)一行,然后用新的随机纹理替换顶行,然后在常规 Surfaceview 画布上绘制它.
这是我认为可行的方法;
我的 surfaceChanged 我指定位图和画布并启动线程:
@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();
}
我的线程,只显示必要的中间运行部分:
@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 帧,但我认为通过滚动位图可以做得更好。
【问题讨论】:
-
完全跑题了,但是你在市场上的应用描述是全文,没有段落,什么都没有……你应该结构化它!第一个想法是:tldr
-
好建议,添加段落谢谢,但不确定您所说的“全文......没有什么”是什么意思。这是一个纯文本字段,我无法向其中添加图像。
-
没错,但你的文本格式很丑。我不想在描述中提供完整的“如何使用/播放”指南。把它想象成一个广告文字。在列表中总结一些很酷的功能,让读者对你的游戏感兴趣。
标签: java android bitmap scroll surfaceview