如何编程此UI以使其更流畅?

时间:2014-08-20 17:33:48

标签: java android multithreading ui-thread

我从循环中收集数据:

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();
    }

currentAIVoltage0currentAIVoltage5都是公共浮点变量。 我的updateUI方法是FragmentAIF中的一种方法。

然而,

AIF使用这种方法往往会很慢;人工智能不定期更新。

以下是我的对象updateUI的类文件中的AIF方法:

    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));
                }
             }
           );  
        }

完整片段:

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));
                }
             }
           );  
        }
}

0 个答案:

没有答案