我有一个在 Service 中运行的 MediaPlayer。该服务每 250 毫秒向 Activity 发送一个包含当前歌曲持续时间的广播,以更新 Activity 中的 SeekBar。
seekIntent = new Intent("com.someaction");
private Runnable sendUpdateToUI = new Runnable() {
public void run()
{
LogMediaPosition();
handler.postDelayed(this, 250);
}
};
private void LogMediaPosition()
{
mediaPosition = mMediaPlayer.getCurrentPosition();
mediaMax = mMediaPlayer.getDuration();
seekIntent.putExtra("counter", mediaPosition);
seekIntent.putExtra("mediamax", mediaMax);
sendBroadcast(seekIntent);
}
并且在活动中
registerReceiver(broadcastReceiver, new IntentFilter("com.someaction"));
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
updateUI(intent);
}
};
private void updateUI(Intent intent)
{
int seekProgress = intent.getIntExtra("counter", 0);
int seekMax = intent.getIntExtra("mediamax", 0);
songCurrentDurationLabel.setText(utils.millisecondsToTimer(seekProgress));
songProgressBar.setMax(seekMax);
songProgressBar.setProgress(seekProgress);
}
该活动还有一个简单的TranslateAnimation。
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(500);
translate.setFillAfter(true);
translate.setFillEnabled(true);
btnName.startAnimation(translate);
注册接收器时,TranslateAnimation 中有一个非常轻微但明显的滞后。但是,如果我在活动中注释掉 registerReceiver() 行,即不让接收者注册因此不更新 SeekBar,则 TranslateAnimation 运行非常顺利。
任何建议,即使在接收器注册后如何解决这个问题以获得流畅的动画?