Java 如何编程使此UI更平滑?
我在这里从循环中收集数据:Java 如何编程使此UI更平滑?,java,android,multithreading,ui-thread,Java,Android,Multithreading,Ui Thread,我在这里从循环中收集数据: public void loop() throws ConnectionLostException, InterruptedException { led_.write(false); setCurrentAIVoltage0(AI0.getVoltage()); setCurrentAIVoltage1(AI1.getVoltage()); setCurrentAIVoltage2(AI2.getV
public void loop() throws ConnectionLostException, InterruptedException {
led_.write(false);
setCurrentAIVoltage0(AI0.getVoltage());
setCurrentAIVoltage1(AI1.getVoltage());
setCurrentAIVoltage2(AI2.getVoltage());
setCurrentAIVoltage3(AI3.getVoltage());
setCurrentAIVoltage4(AI4.getVoltage());
setCurrentAIVoltage5(AI5.getVoltage());
AIF.updateUI();
}
其中currentAIVoltage0到currentAIVoltage5都是公共浮点变量。
我的updateUI方法是我的片段AIF中的一个方法
然而,AIF使用这种方法的速度往往较慢;人工智能也不定期更新
下面是我的类文件中针对对象AIF的updateUI方法:
完整片段:
public static class AnalogInputFragment extends Fragment {
public TextView AITVVOLT0;
public TextView AITVVOLT1;
public TextView AITVVOLT2;
public TextView AITVVOLT3;
public TextView AITVVOLT4;
public TextView AITVVOLT5;
public ProgressBar AIVOLTBAR0;
public ProgressBar AIVOLTBAR1;
public ProgressBar AIVOLTBAR2;
public ProgressBar AIVOLTBAR3;
public ProgressBar AIVOLTBAR4;
public ProgressBar AIVOLTBAR5;
public static AnalogInputFragment newInstance() {
AnalogInputFragment fragment = new AnalogInputFragment();
return fragment;
}
public AnalogInputFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.analogin_fragment,
container, false);
AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
AITVVOLT0.setText("Voltage is: " + currentAIVoltage0);
AITVVOLT1.setText("Voltage is: " + currentAIVoltage1);
AITVVOLT2.setText("Voltage is: " + currentAIVoltage2);
AITVVOLT3.setText("Voltage is: " + currentAIVoltage3);
AITVVOLT4.setText("Voltage is: " + currentAIVoltage4);
AITVVOLT5.setText("Voltage is: " + currentAIVoltage5);
AIVOLTBAR0.setMax(330);
AIVOLTBAR1.setMax(330);
AIVOLTBAR2.setMax(330);
AIVOLTBAR3.setMax(330);
AIVOLTBAR4.setMax(330);
AIVOLTBAR5.setMax(330);
AIVOLTBAR0.setProgress(0);
AIVOLTBAR1.setProgress(0);
AIVOLTBAR2.setProgress(0);
AIVOLTBAR3.setProgress(0);
AIVOLTBAR4.setProgress(0);
AIVOLTBAR5.setProgress(0);
return rootView;
}
public void updateUI(){
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
AITVVOLT0.setText("Voltage is: " + getCurrentAIVoltage0());
AITVVOLT1.setText("Voltage is: " + getCurrentAIVoltage1());
AITVVOLT2.setText("Voltage is: " + getCurrentAIVoltage2());
AITVVOLT3.setText("Voltage is: " + getCurrentAIVoltage3());
AITVVOLT4.setText("Voltage is: " + getCurrentAIVoltage4());
AITVVOLT5.setText("Voltage is: " + getCurrentAIVoltage5());
AIVOLTBAR0.setProgress(Math.round(getCurrentAIVoltage0()*100));
AIVOLTBAR1.setProgress(Math.round(getCurrentAIVoltage1()*100));
AIVOLTBAR2.setProgress(Math.round(getCurrentAIVoltage2()*100));
AIVOLTBAR3.setProgress(Math.round(getCurrentAIVoltage3()*100));
AIVOLTBAR4.setProgress(Math.round(getCurrentAIVoltage4()*100));
AIVOLTBAR5.setProgress(Math.round(getCurrentAIVoltage5()*100));
}
}
);
}
}
你能更具体地描述一下你遇到的问题吗?请显示更多的代码。问题是,在运行程序时,显示的数据设置了一系列ProgressBar,并根据在单独线程上读取的电压水平对其进行更改,这是超慢/不稳定的。请您向我们展示片段的全部代码以及从您那里获取数据的代码正在显示?获取数据的代码如上所示,使用getVoltage方法。我将添加完整的片段代码。getCurrentAIVoltage0中的代码是最有可能导致问题的原因,我也需要看到这一点。片段是完全好的,尽管您似乎是从片段之外的某个地方调用updateUI?这有点不寻常。片段应该是自包含的单元,完全模块化,不依赖于任何东西。他们应该对自己的UI负责,片段之外的任何东西都不应该以任何方式与片段内部的UI有关。我认为这种误解可能是你错误的根源。
public static class AnalogInputFragment extends Fragment {
public TextView AITVVOLT0;
public TextView AITVVOLT1;
public TextView AITVVOLT2;
public TextView AITVVOLT3;
public TextView AITVVOLT4;
public TextView AITVVOLT5;
public ProgressBar AIVOLTBAR0;
public ProgressBar AIVOLTBAR1;
public ProgressBar AIVOLTBAR2;
public ProgressBar AIVOLTBAR3;
public ProgressBar AIVOLTBAR4;
public ProgressBar AIVOLTBAR5;
public static AnalogInputFragment newInstance() {
AnalogInputFragment fragment = new AnalogInputFragment();
return fragment;
}
public AnalogInputFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.analogin_fragment,
container, false);
AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
AITVVOLT0.setText("Voltage is: " + currentAIVoltage0);
AITVVOLT1.setText("Voltage is: " + currentAIVoltage1);
AITVVOLT2.setText("Voltage is: " + currentAIVoltage2);
AITVVOLT3.setText("Voltage is: " + currentAIVoltage3);
AITVVOLT4.setText("Voltage is: " + currentAIVoltage4);
AITVVOLT5.setText("Voltage is: " + currentAIVoltage5);
AIVOLTBAR0.setMax(330);
AIVOLTBAR1.setMax(330);
AIVOLTBAR2.setMax(330);
AIVOLTBAR3.setMax(330);
AIVOLTBAR4.setMax(330);
AIVOLTBAR5.setMax(330);
AIVOLTBAR0.setProgress(0);
AIVOLTBAR1.setProgress(0);
AIVOLTBAR2.setProgress(0);
AIVOLTBAR3.setProgress(0);
AIVOLTBAR4.setProgress(0);
AIVOLTBAR5.setProgress(0);
return rootView;
}
public void updateUI(){
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
AITVVOLT0.setText("Voltage is: " + getCurrentAIVoltage0());
AITVVOLT1.setText("Voltage is: " + getCurrentAIVoltage1());
AITVVOLT2.setText("Voltage is: " + getCurrentAIVoltage2());
AITVVOLT3.setText("Voltage is: " + getCurrentAIVoltage3());
AITVVOLT4.setText("Voltage is: " + getCurrentAIVoltage4());
AITVVOLT5.setText("Voltage is: " + getCurrentAIVoltage5());
AIVOLTBAR0.setProgress(Math.round(getCurrentAIVoltage0()*100));
AIVOLTBAR1.setProgress(Math.round(getCurrentAIVoltage1()*100));
AIVOLTBAR2.setProgress(Math.round(getCurrentAIVoltage2()*100));
AIVOLTBAR3.setProgress(Math.round(getCurrentAIVoltage3()*100));
AIVOLTBAR4.setProgress(Math.round(getCurrentAIVoltage4()*100));
AIVOLTBAR5.setProgress(Math.round(getCurrentAIVoltage5()*100));
}
}
);
}
}