简体   繁体   English

使用SeekBar更新Android布局中的计算值

[英]Updating calculated values in Android layouts using SeekBar

So, I've been working on an app that has a Plan model with a number of different inputs and outputs, and the layout of the app has slider controls for the inputs and labels for the outputs. 因此,我一直在开发一个具有Plan模型并带有许多不同输入和输出的应用程序,并且该应用程序的布局具有用于输入的滑块控件和用于输出的标签。 When an input changes, it updates the model, which then runs a calculation, and then updates the views. 输入发生更改时,它将更新模型,然后运行模型,然后更新视图。 I didn't think there was anything wrong with this architecture at first, but even simple calculations seem to run really slowly, blocking the UI thread. 我最初并不认为此体系结构有什么问题,但是即使是简单的计算也似乎运行缓慢,从而阻塞了UI线程。 Granted, I do have a somewhat complicated way of updating things: 当然,我确实有一些复杂的更新方式:

  1. Slider (in a viewgroup subclass) updates its value and sends a message to a delegate (which implements an interface specific to that viewgroup subclass). Slider(在视图组子类中)更新其值,并将消息发送到委托(该代理实现特定于该视图组子类的接口)。
  2. Delegate (which holds the model and the control subviews) tells the Plan instance to set a new value, which triggers the plan to recalculate its outputs. 委托(保存模型和控制子视图)告诉Plan实例设置一个新值,这将触发计划重新计算其输出。
  3. Once the plan finishes its calculations, it sends another message to the delegate, which then tells its output views to update with the new values. 计划完成计算后,它将向代理发送一条消息,然后代理将其输出视图更新为新值。

I've modeled this architecture off of an iOS app that I developed which didn't seem to have as big of a problem running the calculations. 我已经根据我开发的iOS应用程序对这种体系结构进行了建模,该应用程序似乎在运行计算时没有太大的问题。

Now, I know that Android is significantly different than iOS, so I'm wondering if I'm going about this completely wrong. 现在,我知道Android与iOS显着不同,所以我想知道我是否完全错误。 Is there a way to just tell these views to watch the Plan model for changes and then grab the value it's supposed to display? 有没有一种方法可以告诉这些视图监视计划模型的更改,然后获取应该显示的值?

Another major issue that I'm seeing here is with the slider input. 我在这里看到的另一个主要问题是滑块输入。 If I put the model update calculations into a thread, every time the slider changes, a new thread will be created. 如果我将模型更新计算放入线程中,则每次滑块更改时,都会创建一个新线程。 These threads (as I've seen) will more or less finish in random order, updating the view in such a way as too make very little sense when you should see incremental changes. 这些线程(如我所见)将以随机顺序或多或少地完成操作,以这种方式更新视图,以至于当您应该看到增量更改时,它们就变得太没有意义了。 Is there a good way of threading calculations that are supposed to be changeable with a seekbar? 有没有一种好的方法,可以通过搜索栏更改线程计算?

Have you looked at Observer and Observable ? 您是否看过观察者 观察者 Maybe your observed model can perform the update using Runnable and then notify the observer. 也许您观察到的模型可以使用Runnable执行更新,然后通知观察者。

This is just an idea of the top of my head: 这只是我脑海中的一个想法:

Instead of just starting a new thread for each update from the slider, you could implement some kind of Queue . 您可以实现某种Queue ,而不仅仅是为滑块上的每次更新启动一个新线程。

You would need a to have a Thread running, that holds the Queue . 您需要运行一个包含QueueThread

public class QueueThread extends Thread {
  private boolean running;
  private ArrayDeque<Runnable> queue;
  private Thread current;

  public QueueThread() {
    running = true;
    queue = new ArrayDeque<Runnable>();
    current = new Thread();
  }

  @Override
  public void run() {
    while( running ) {
      if( !queue.isEmpty() && !current.isAlive() ) { //We only want to start a new thread if there is one or more in the queue AND the old task is not runnning.
        current = new Thread( queue.pollFirst() );
        current.start();
      }
      else
        try {
          Thread.sleep( 200 ); //We need a sleep in order to not hammer the CPU.
        }
        catch( InterruptedException e ) {
          e.printStackTrace();
        }
    }
  }

  public void stopThread() {
    running = false;
  }

  public void add( Runnable task ) {
    queue.addLast( task ); //Here is where we add a task to the queue. The slider (or whoever posts the updates) must have a reference to this thread object.
  }
}

Doing this would allow each update to finish before the next is started. 这样做将允许每个更新在开始下一个更新之前完成。 I am not sure how it will do in performance. 我不确定它的性能如何。 I haven't tested it or anything. 我还没有测试过。 It was just an idea. 这只是一个主意。

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

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