EditTextを高速で更新して、数値がよりスムーズに流れるようにしようとしています。しかし、AsyncTask、Thread-> Runnable with handler、Timer-> TimerTask with handlerなど、テクニックとオブジェクトのさまざまな組み合わせを試しましたが、タイミングをどれだけ設定しても、うまくいかないようです。約1秒より良い。
メッセージポンプを通過しなければならないのも事実かもしれないと思いますが、よくわかりません。少なくとも250〜500ミリ秒の更新頻度を取得する方法を誰かが知っていますか?より高い周波数が望ましいですが、それでもかまいません。
更新2:カウント++以外のすべてをコメントアウトしました。編集テキストの1つをこの値で更新すると、実際に非常に高速に更新されます。したがって、updateUi()で発生していることが、速度を低下させています。何がわからない。
更新1:問題の一部である場合に備えて、プリミティブでテストするために使用していたSharedPreferencesのリアルタイムの使用法を切り替えました。パフォーマンスは同じように見えます。定期的に発生する唯一の考えは、タイマーで実行されるupdateUi()関数です。コードは以下のとおりです。これは私がすぐに更新することを期待しているものですが、そうではありません。
private void updateUi() {
lastDate = cal;
cal = Calendar.getInstance();
synchronized(cal) {
//if the current date is greater than the last stored date
if(compareDates(cal, lastDate) > 0 && count == 0) {
//disable timer
calorieTimer.cancel();
//dereference timer...probably not necessary
calorieTimer = null;
double bankBmr = currentBmr.getBMR() *
activityLevelMultipliers[activityLevelSpinner.getSelectedItemPosition()];
metrics.calorieBank += (int)bankBmr - metrics.caloriesEaten;
prefsEditor.putInt("CALORIE_BANK", metrics.calorieBank);
prefsEditor.putInt("CALORIES_EATEN", 0);
prefsEditor.commit();
metrics.caloriesEaten = 0;
//update lastDate to prevent multiple calorie banking
lastDate = cal;
count++;
Log.e("updateUi()", "count #" + count);
//set up timer again
setupCalorieTimer();
}
caloriesEatenEditText.setText("" + metrics.caloriesEaten);
caloriesRemainingEditText.setText(String.format("%d", (int)activeBmr - metrics.caloriesEaten));
bankEditText.setText("" + metrics.calorieBank);
estimatedWeightEditText.setText(String.format("%.2f", metrics.currentWeight - metrics.calorieBank / 3500.0)) ;
//update the time
time.setToNow();
//update calories available
caloriesAvailableEditText.setText(
String.format("%.2f", activeBmr * percentageOfDay(time) - metrics.caloriesEaten));
} //End synchronized(cal)
} //End updateUi()
private void setupCalorieTimer() {
//create handler to post update back to ui thread
final Handler handler = new Handler();
calorieTimer = new Timer();
calorieTimer.schedule(new TimerTask() {
@Override
public void run() {
//post to ui thread
handler.post(new Runnable() {
@Override
public void run() {
updateUi();
}
});
}
}, 0, 50);
}
最初にリストしなかった関数は、解像度が1秒しかないandroid.text.format.Timeオブジェクトを使用していることが判明しました。つまり、パフォーマンスが低下することはなく、オブジェクトが1秒に1回しか経過しなかったため、これ以上速く更新できなかった可能性があります。以下は誤警報を引き起こした最初の機能であり、2番目の機能は「修正」でした。うまくいけば、これは、パフォーマンスの低下が私のように原因であると誤って考える人に役立つでしょう。
//"caused" the problem
private double percentageOfDay(Time time) {
//factor the hour
double percentage = time.hour / 24f;
//factor the minute
percentage += time.minute / 1440f;
//factor the second
percentage += time.second / 86400f;
return percentage;
}
//This fixed the problem
private double percentageOfDay(Calendar cal) {
//factor the hour
double percentage = cal.get(Calendar.HOUR) / 24f;
//factor the minute
percentage += cal.get(Calendar.MINUTE) / 1440f;
//factor the second
percentage += cal.get(Calendar.SECOND) / 86400f;
//factor the millisecond
percentage += cal.get(Calendar.MILLISECOND) / 86400000f;
return percentage;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加