0

我需要像一本书一样编写一个android应用程序。我有大约 100 张图像,我需要用后退、前进和另一个按钮来显示它们。我尝试为每个图像创建一个 xml 布局,并将图像制作为布局的背景。

在运行应用程序时,如果我快速按下按钮,程序在切换 xml 布局期间崩溃。如果我减小图像大小,我的问题也会减少。不幸的是,我需要另一种解决方案来解决它,因为我不能使用较小的图像尺寸,但我仍然有崩溃问题。

4

2 回答 2

1

有一个布局,其中有一个ImageView。然后在需要循环到下一个或上一个图像时不断更改图像视图的源图像。

于 2012-08-08T17:52:29.590 回答
0

部分问题是单击 UI 按钮会立即返回/排队单击,即使与该单击关联的操作尚未完成。由于超出此响应范围的原因,值得注意的是,在“工作”时简单地停用按钮是无效的。这类问题有几个解决方案:一个是使用一个布尔标志,该标志仅在底层“工作”完成后才被设置。然后在按钮操作处理程序中,忽略在重置标志之前发生的按钮单击:

   /**
    * Button presses are ignored unless idle.
    */
   private void onMyButtonClicked() {
      if(idle) {
         doWork();
      }
   }

   /**
    * Does some work and then restores idle state when finished.
    */
   private void doWork() {
      idle = false;
      // maybe you spin off a worker thread or something else.
      // the important thing is that either in that thread's run() or maybe just in the body of
      // this doWork() method, you:
      idle = true;
   } 

另一个通用选项是使用时间进行过滤;IE。您设置了按钮按下的最大频率为 1hz 的限制:

/**
    * Determines whether or not a button press should be acted upon.  Note that this method
    * can be used within any interactive widget's onAction method, not just buttons.  This kind of
    * filtering is necessary due to the way that Android caches button clicks before processing them.
    * See http://code.google.com/p/android/issues/detail?id=20073
    * @param timestamp timestamp of the button press in question
    * @return True if the timing of this button press falls within the specified threshold
    */
   public static synchronized boolean validateButtonPress(long timestamp) {
      long delta = timestamp - lastButtonPress;
      lastButtonPress = timestamp;
      return delta > BUTTON_PRESS_THRESHOLD_MS;
   }

然后你会做这样的事情:

private void onMyButtonClicked() {      
      if(validateButtonPress(System.currentTimeMillis())) {
         doWork();
      }
   }

最后一个解决方案无疑是不确定的,但如果您考虑到用户几乎从不故意在移动设备上每秒点击超过 1-2 次按钮,那么它还不错。

于 2012-08-08T18:37:45.163 回答