2 slaks SLaks 于 2013.09.09 17:44 提问

Android 中 TextView 的 lag

下面是我的代码,TextView 被分成两组,要求其中一组要比另一组好。

   public void onLocationChanged(Location location) 
    {
        // TODO Auto-generated method stub
        if(predictionFlag)
        {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            predictionFlag = false;
        }
        int x = 0;
        do
        {
            if(x != 0)
            {
                try 
                {
                    Thread.sleep(100);
                } catch (InterruptedException e) 
                  {
                    e.printStackTrace();
                  }
            }
            Location predicationPoint = DOTGpsAppUtils.predictionAlgorithm(latitude, longitude, 1, 100);
            double predictionLongitude = (longitude + predicationPoint.getLongitude())/2;
            double predictionLatitude = (latitude + predicationPoint.getLatitude())/2;
            TextView textView = (TextView) findViewById(R.id.oneHundredMillisecondLatitude);
            textView.setText(Double.valueOf(predictionLatitude).toString());
            textView = (TextView) findViewById(R.id.oneSecondCalculatedPointLongitude);
            textView.setText(Double.valueOf(predictionLongitude).toString());
            if(x == 9)
            {
                longitude = predictionLongitude;
                latitude = predictionLatitude;
            }
            System.out.println(x);
            ++x;
        }while(x < 10);

        TextView textView = (TextView) findViewById(R.id.startingPointLongitude);
        textView.setText(Double.valueOf(location.getLongitude()).toString());
        textView = (TextView) findViewById(R.id.startingPointLatitude);
        textView.setText(Double.valueOf(location.getLatitude()).toString());
        textView =  (TextView) findViewById(R.id.oneSecondCalculatedPointLongitude);
        textView.setText(Double.valueOf(longitude).toString());
        textView = (TextView) findViewById(R.id.oneSecondCalculatedLatitude);
        textView.setText(Double.valueOf(latitude).toString());
        ++y;
        System.out.println("Thread Count:" + y);
    }

在 android 屏幕上为什么不能快速更新 R.id.oneHundredmilliesecand 和其它的 textviews?

1个回答

sasuke38
sasuke38   2013.09.10 12:26
已采纳
public void onLocationChanged(final Location location) 
    {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(predictionFlag)
        {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            predictionFlag = false;
        }
        int x = 0;
        do
        {
            if(x != 0)
            {
                try 
                {
                    Thread.sleep(100);
                } catch (InterruptedException e) 
                  {
                    e.printStackTrace();
                  }
            }
            Location predicationPoint = DOTGpsAppUtils.predictionAlgorithm(latitude, longitude, 1, 100);
            double predictionLongitude = (longitude + predicationPoint.getLongitude())/2;
            double predictionLatitude = (latitude + predicationPoint.getLatitude())/2;
            TextView textView = (TextView) findViewById(R.id.oneHundredMillisecondLatitude);
            textView.setText(Double.valueOf(predictionLatitude).toString());
            textView = (TextView) findViewById(R.id.oneSecondCalculatedPointLongitude);
            textView.setText(Double.valueOf(predictionLongitude).toString());
            if(x == 9)
            {
                longitude = predictionLongitude;
                latitude = predictionLatitude;
            }
            System.out.println(x);
            ++x;
        }while(x < 10);

        TextView textView = (TextView) findViewById(R.id.startingPointLongitude);
        textView.setText(Double.valueOf(location.getLongitude()).toString());
        textView = (TextView) findViewById(R.id.startingPointLatitude);
        textView.setText(Double.valueOf(location.getLatitude()).toString());
        textView =  (TextView) findViewById(R.id.oneSecondCalculatedPointLongitude);
        textView.setText(Double.valueOf(longitude).toString());
        textView = (TextView) findViewById(R.id.oneSecondCalculatedLatitude);
        textView.setText(Double.valueOf(latitude).toString());
        ++y;
        System.out.println("Thread Count:" + y);
                }
            });
    }
sasuke38
sasuke38 不过我的代码中 Thread.sleep(100);这句代码可能会造成ui卡顿 你想办法把它提取到runOnUiThread(new Runnable() { @Override public void run() { } });外面
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!