简体   繁体   English

为什么触摸屏幕时性能会提高?

[英]Why does my performance increase when touching the screen?

For some reason my FPS jumps up considerably when I move my mouse around on the screen (on the emulator) while holding the left mouse button. 由于某些原因,当我按住鼠标左键在屏幕上(模拟器上)四处移动时,FPS会大幅跳升。 Normally my game is very laggy, but if I touch the screen (and as long as I am moving the mouse around while touching) it goes perfectly smooth. 通常我的游戏很慢,但是如果我触摸屏幕(并且只要我在触摸时四处移动鼠标),它就会非常流畅。

I have tried sleeping for 20ms in the onTouchEvent, but it doesn't appear to make any difference. 我曾尝试在onTouchEvent中睡眠20毫秒,但似乎没有任何区别。

Here is the code I use in my onTouchEvent: 这是我在onTouchEvent中使用的代码:

 // events when touching the screen
    public boolean onTouchEvent(MotionEvent event) 
    {
        int eventaction = event.getAction(); 

        touchX=event.getX();
        touchY=event.getY();

        switch (eventaction) 
        { 
         case MotionEvent.ACTION_DOWN: 
         {
          touch=true;
         }
         break; 

         case MotionEvent.ACTION_MOVE:
         {

         }
         break; 

         case MotionEvent.ACTION_UP: 

         {
          touch=false;
         }
              break; 
        }

     /*try 
     {
      AscentThread.sleep(20);
     } 
     catch (InterruptedException e) 
     {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }*/

        return true; 
    }

In the logcat log, FPS is the current fps (average of the last 20 frames), touch is whether or not the screen is being touched (from onTouchEvent). 在logcat日志中,FPS是当前fps(最近20帧的平均值),touch是是否正在触摸屏幕(来自onTouchEvent)。

What on earth is going on? 到底是怎么回事? Has anyone else had this odd behaviour before? 以前有没有其他人有这种奇怪的行为?

Logcat log: Logcat日志:

12-21 19:43:26.154: INFO/myActivity(786): FPS: 31.686569159606414 Touch: false
12-21 19:43:27.624: INFO/myActivity(786): FPS: 19.46310293212206 Touch: false
12-21 19:43:29.104: INFO/myActivity(786): FPS: 18.801202175690467 Touch: false
12-21 19:43:30.514: INFO/myActivity(786): FPS: 21.118295877408478 Touch: false
12-21 19:43:31.985: INFO/myActivity(786): FPS: 19.117397812958878 Touch: false
12-21 19:43:33.534: INFO/myActivity(786): FPS: 15.572571858239263 Touch: false
12-21 19:43:34.934: INFO/myActivity(786): FPS: 20.584119901503506 Touch: false
12-21 19:43:36.404: INFO/myActivity(786): FPS: 18.888025905454207 Touch: false
12-21 19:43:37.814: INFO/myActivity(786): FPS: 22.35722329083629 Touch: false
12-21 19:43:39.353: INFO/myActivity(786): FPS: 15.73604859775362 Touch: false
12-21 19:43:40.763: INFO/myActivity(786): FPS: 20.912449882754633 Touch: false
12-21 19:43:42.233: INFO/myActivity(786): FPS: 18.785278388997718 Touch: false
12-21 19:43:43.634: INFO/myActivity(786): FPS: 20.1357397209596 Touch: false
12-21 19:43:45.043: INFO/myActivity(786): FPS: 21.961138432007957 Touch: false
12-21 19:43:46.453: INFO/myActivity(786): FPS: 22.167196852834273 Touch: false
12-21 19:43:47.854: INFO/myActivity(786): FPS: 22.207318228024274 Touch: false
12-21 19:43:49.264: INFO/myActivity(786): FPS: 22.36980559230175 Touch: false
12-21 19:43:50.604: INFO/myActivity(786): FPS: 23.587638823252547 Touch: false
12-21 19:43:52.073: INFO/myActivity(786): FPS: 19.233902040593076 Touch: false
12-21 19:43:53.624: INFO/myActivity(786): FPS: 15.542190150440987 Touch: false
12-21 19:43:55.034: INFO/myActivity(786): FPS: 20.82290063974675 Touch: false
12-21 19:43:56.436: INFO/myActivity(786): FPS: 21.975282007207717 Touch: false
12-21 19:43:57.914: INFO/myActivity(786): FPS: 18.786927284103687 Touch: false
12-21 19:43:59.393: INFO/myActivity(786): FPS: 18.96879004217992 Touch: false
12-21 19:44:00.625: INFO/myActivity(786): FPS: 28.367566618064878 Touch: false
12-21 19:44:02.113: INFO/myActivity(786): FPS: 19.04441528684418 Touch: false
12-21 19:44:03.585: INFO/myActivity(786): FPS: 18.807837511809065 Touch: false
12-21 19:44:04.993: INFO/myActivity(786): FPS: 21.134330284993418 Touch: false
12-21 19:44:06.275: INFO/myActivity(786): FPS: 27.209688764079907 Touch: false
12-21 19:44:07.753: INFO/myActivity(786): FPS: 19.055894653261653 Touch: false
12-21 19:44:09.163: INFO/myActivity(786): FPS: 22.05422794901088 Touch: false
12-21 19:44:10.644: INFO/myActivity(786): FPS: 18.6956805300596 Touch: false
12-21 19:44:12.124: INFO/myActivity(786): FPS: 17.434180581311054 Touch: false
12-21 19:44:13.594: INFO/myActivity(786): FPS: 18.71932038510891 Touch: false
12-21 19:44:14.504: INFO/myActivity(786): FPS: 40.94571503868066 Touch: true
12-21 19:44:14.924: INFO/myActivity(786): FPS: 57.061200121138576 Touch: true
12-21 19:44:15.364: INFO/myActivity(786): FPS: 62.54377946377936 Touch: true
12-21 19:44:15.764: INFO/myActivity(786): FPS: 64.05005071818726 Touch: true
12-21 19:44:16.384: INFO/myActivity(786): FPS: 50.912951172948155 Touch: true
12-21 19:44:16.874: INFO/myActivity(786): FPS: 55.31242053078078 Touch: true
12-21 19:44:17.364: INFO/myActivity(786): FPS: 59.31625410615102 Touch: true
12-21 19:44:18.413: INFO/myActivity(786): FPS: 36.63504170925923 Touch: false
12-21 19:44:19.885: INFO/myActivity(786): FPS: 18.099130467755923 Touch: false
12-21 19:44:21.363: INFO/myActivity(786): FPS: 18.458978222946566 Touch: false
12-21 19:44:22.683: INFO/myActivity(786): FPS: 25.582179409330823 Touch: true
12-21 19:44:23.044: INFO/myActivity(786): FPS: 60.99865521942455 Touch: true
12-21 19:44:23.403: INFO/myActivity(786): FPS: 74.17873975470984 Touch: true
12-21 19:44:23.763: INFO/myActivity(786): FPS: 64.25663040460714 Touch: true
12-21 19:44:24.113: INFO/myActivity(786): FPS: 62.47483457826921 Touch: true
12-21 19:44:24.473: INFO/myActivity(786): FPS: 65.27969529547072 Touch: true
12-21 19:44:24.825: INFO/myActivity(786): FPS: 67.84743115273311 Touch: true
12-21 19:44:25.173: INFO/myActivity(786): FPS: 73.50854551357706 Touch: true
12-21 19:44:25.523: INFO/myActivity(786): FPS: 70.46432534585368 Touch: true
12-21 19:44:25.873: INFO/myActivity(786): FPS: 69.04076953445896 Touch: true

If you're running the emulator on Windows, then I think this explains it: 如果您在Windows上运行模拟器,那么我认为这可以解释一下:

Description of Performance Options in Windows Windows中的性能选项说明

... Also, remember that Microsoft Windows NT also uses additional priority adjustments (usually 2) based on other events, such as momentarily boosting a thread's priority when it returns from an I/O call, when returning from a user-mode subsystem call, or when receiving keyboard input. ...另外,请记住,Microsoft Windows NT还基于其他事件使用其他优先级调整(通常为2),例如从I / O调用返回时,从用户模式子系统调用返回时,暂时提高线程的优先级。 ,或接收键盘输入时。

I've seen this effect many times. 我已经多次看到这种效果。 And I just took a peek at the emulator using ProcessExplorer: while I can't say for sure, it looks like the emulator runs under a single Windows thread. 我只是使用ProcessExplorer窥视了模拟器:虽然我不能肯定地说,但是看起来模拟器运行在单个Windows线程下。 Unless you're seeing a similar performance boost on a real Android device, I'd assume that it's an effect of the Windows OS, not AndroidOS. 除非您在真正的Android设备上看到类似的性能提升,否则我认为这是Windows操作系统而不是AndroidOS的影响。

I had the same problem and it also occurs on the phone even though it's much less notable. 我遇到了同样的问题,即使不太明显,它也出现在电话上。 I think I know what happend. 我想我知道发生了什么。 I use a Handler with postDelayed(...) for my main loop. 我在主循环中使用了带有postDelayed(...)的处理程序。 I guess the drawing first somehow blocks the message queue and then the touchevent causes the app to check the queue before next scheduled check. 我想绘图首先会以某种方式阻塞消息队列,然后touchevent会使应用在下一次计划检查之前检查队列。 Why the problem happens somewhat randomly is a mystery. 为什么问题在某种程度上随机发生是一个谜。

I solved it by putting the postDelayed-line before the drawing in the Runnable. 我通过将postDelayed-line放置在Runnable中的图形之前来解决它。

It has for sure nothing to do with Windows. 当然,它与Windows没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM