私の UI にはImageView
、リアルタイムで更新する必要がある (矢印の画像) があります。
矢じりには 2 つの可能な動きがあります。
- 常に北を指すように回転する
- ユーザーの場所が変わったときの移動
これら2つの方法が正しく機能するようにすでに取得しています。
唯一の問題は、UI が非常に遅く、時々動かなくなることです。一方、アプリを実行している間、私の電話は常に非常に熱くなります。Logcat
も時々教えてくれます
*フレームをスキップしました。アプリケーションがメイン スレッドで処理しすぎている可能性があります。
AsyncTask
UIにストレスを与えないように使うように言われました。だから私は矢じりの更新作業を で行いAsyncTask
ます。ただし、問題は残ります。私の UI はまだ非常に遅いです。
私のAsyncTask
実装には何か問題があるはずです。次のようにここに貼り付けます。
public class ArrowheadUpdater extends AsyncTask<Float, Integer, Float> { // Float: azimuth, Integer: state
private ImageView arrowheadToRotate;
private float rotationAngle; // also in radians
// constructor
public ArrowheadUpdater(ImageView _arrowheadToRotate) {
arrowheadToRotate = _arrowheadToRotate;
rotationAngle = -1;
}
protected void onPreExecute(Float _azimuth) {
super.onPreExecute();
}
@Override
// executed first to get the angle to rotate
protected Float doInBackground(Float... arg0) {
rotationAngle = (float) (Constant.MAP_ORIENTATION_OFFSET + arg0[0]);
return rotationAngle;
}
protected void onProgressUpdated(Integer... progress) {
super.onProgressUpdate(progress);
}
protected void onPostExecute(Float result) {
super.onPostExecute(result);
\\ rotation happens here
rotateImageView(ShowPathActivity.this, arrowheadToRotate, R.drawable.marker, result);
\\ moving happens here
movaImageView(arrowhead, MapView.historyXSeries, MapView.historyYSeries);
}
AsyncTask を次のように呼び出します。
// called when sensor values change
public void onSensorChanged(SensorEvent event) { // is roughly called 350 times in 1s
//...
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
compassChangedTimes++;
magneticField[0] = event.values[0];
magneticField[1] = event.values[1];
magneticField[2] = event.values[2];
SensorManager.getRotationMatrix(RotationM, I, gravity, magneticField);
SensorManager.getOrientation(RotationM, direction);
if (compassChangedTimes % 50 == 0) {
// HERE!!!!!!!!!!
new ArrowheadUpdater(arrowhead).execute(direction[0]);
}
}
if (startFlag)
dataCollector.saveDataShowPath(acceleration, magneticField, startTime, currentTime);
}
doInBackground()
の代わりに2 つの矢印更新メソッドを入れる必要がありonPostExecute()
ますか?
しかし、doInBackground() の行は UI を更新できますか? 私はわかりません。
私の中に何か問題がありAsyncTask
ますか?
他の推測やコメントは大歓迎です!
より多くの手がかり:
このアクティビティに入ると、UI が非常に遅くなり、多くのスタックが発生することに気付きました。しかし、しばらくすると、たとえば 10 秒後に、許容できるほど滑らかになります。