用户界面滞后于使用倒数计时器更新textview和在同一活动上使用textview字幕

时间:2019-06-20 07:48:23

标签: java android

我有一个textView显示倒计时计时器的进度,并且我还有另一个textView,当用户输入的长度太长时,我将其设置为选取框。当出现字幕动画并且计时器处于活动状态时,UI会滞后。如何防止滞后并分别进行UI更新

计时器代码:

  countDownTimer = new CountDownTimer(milliSeconds+100, 1000)
    {
            @Override
            public void onTick(long millisUntilFinished) {
                milliSecTime = (int) millisUntilFinished;
                long millis = millisUntilFinished;
                //Convert milliseconds into hour,minute and seconds


                String hms = String.format(Locale.getDefault(), 
                          "%02d:%02d:%02d",
                        TimeUnit.MILLISECONDS.toHours(millis),
                        TimeUnit.MILLISECONDS.toMinutes(millis) -
                                TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                        TimeUnit.MILLISECONDS.toSeconds(millis) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

                countDownTimerTextView.setText(hms);

            }

            @Override
            public void onFinish() {

                beepSoundStop.start();
                countDownTimerTextView.setText(R.string.time);

                countNumberOfSets = countNumberOfSets + 1;
                setsTextView.setText(countNumberOfSets + "/" + tNumberOfSets);
                Log.i("CountNumberOfSets", String.valueOf(countNumberOfSets));
                restTimerActive = false;
                timerIsActive = false;
                getNewWorkout();
                Log.i("REST TIMER FINISHED", "TRUE");

            }
        }.start();

以下是字幕的Java代码:

       workoutNameTextView =  findViewById(R.id.workoutNameTextView);
       workoutNameTextView.setSelected(true);

这是textView的xml:

                       <TextView
                        android:id="@+id/workoutNameTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toStartOf="@+id/stopFab"
                        android:layout_toEndOf="@id/startAndPauseFAB"
                        android:singleLine="true"
                        android:ellipsize="marquee"
                        android:layout_centerHorizontal="true"
                        android:text="@string/workout"
                        android:textAlignment="center"

                        android:textColor="?attr/colorAccent"
                        android:textSize="@dimen/workoutNameTextSize"
                        android:textStyle="bold" />

我知道这些都是在UI的主线程上运行的进程,但是如何使它们同时发生以防止滞后

0 个答案:

没有答案