一段时间以来,我经历了一段间歇性的"口吃",即在我的Android游戏中运行的精灵.这是一款非常简单的2D OpenGL ES 2.0游戏.(这是一个持续存在的问题,我多次重访过).
在我的游戏循环中,我有2个'定时器' - 一个用于记录前一秒的帧数,另一个用于计算从当前onDrawFrame迭代结束到下一个开始的时间(以毫秒为单位).
这就是我发现的:
当不渲染任何东西时,我得到60fps(大部分),并且每次调用onDrawFrame时,它报告自己需要更长的16.667ms.现在,如果我渲染一些东西(无论是1个四边形还是100个四边形,结果是相同的),我得到60fps(大部分)但是现在,只有大约20%的onDrawFrame调用报告自己需要更长的时间比上次通话时的16.667毫秒.
我真的不明白为什么会发生这种情况,首先,为什么当onDrawFrame没有做任何事情时,它被称为"慢慢" - 更重要的是,为什么任何GL调用(一个简单的四边形),仍然需要时间onDrawFrame调用超过16.667ms(尽管频率低得多).
我应该说当onDrawFrame报告从最后一次迭代开始超过16.667ms时,它几乎总是伴随着FPS下降(到58或59),但并非所有时间,有时候,FPS保持不变.相反,有时当FPS下降时,onDrawFrame在最后一次迭代完成的16.667ms内被调用.
所以......
我正在尝试修复我的游戏循环并根除这些"口吃" - 其他一些注意事项:
我显然是在误解某些东西,所以我们将不胜感激.
OnDrawFrame
@Override
public void onDrawFrame(GL10 gl) {
startTime = System.nanoTime();
fps++;
totalTime = System.nanoTime() - timeSinceLastCalled;
if (totalTime > 16667000) {
Log.v("Logging","Time between onDrawFrame calls: " + (totalTime /(double)1000000));
}
//Grab time
newTime = System.currentTimeMillis() * 0.001;
frameTime = newTime - currentTime; //Time the last frame took
if (frameTime > 0.25)
frameTime = 0.25;
currentTime = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在从外部源接收到字节后Bitmap
使用 Android创建一个。Bitmap.Config.ARGB_8888
据我了解,在 a 中设置原始字节Bitmap
(不使用JNI
)的最快方法是使用该copyPixelsFromBuffer()
方法,但是出现了关于该缓冲区中字节的正确顺序的问题。
经过一番尝试和错误后,尽管事实上Config.ARGB_8888
表明了正确的顺序,ARGB
但似乎使用的内部格式Bitmap
是RGBA
. Activity
您可以在ie中使用以下方法来测试此行为onCreate()
(我已经在 Android 4.4.4 中测试了它,该方法确实进行了测试,copyPixelsToBuffer()
但根据我的测试,copyPixelsFromBuffer()
其行为是相同的):
private void testBitmap() {
// one pixel bitmap
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
// as per javadoc, the int value is in ARGB format, so A=0xFF, R=0x11, G=0x22, B=0x33
bitmap.setPixel(0, 0, 0xFF112233);
ByteBuffer buffer = ByteBuffer.allocateDirect(4); // 4 bytes for the single pixel
bitmap.copyPixelsToBuffer(buffer);
buffer.position(0); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的Java程序获取一个随机数,但没有任何反应,当我运行程序时,我得到一个java.lang.NullPointerException ...这是我得到random int的代码部分,我将提供之后的完整代码.在此先感谢任何帮助!
try {
PrintStream oFile = new PrintStream("Cipher.txt");
//i get the random number right below here...
rot = random.nextInt(5) + 1;
scan = new Scanner(message);
while (scan.hasNext()) {
cipherWord = scan.next();
l = cipherWord.length();
charScan = new Scanner(cipherWord);
for(int i = 0; i < cipherWord.length(); i++){
cipherChar = cipherWord.charAt(i);
if (cipherChar == 'a') {
cipherChar = 'b';
} else if (cipherChar == 'b') {
cipherChar = 'c';
} else if (cipherChar == 'c') {
cipherChar = 'd';
} …
Run Code Online (Sandbox Code Playgroud)