音樂播放器動(dòng)畫的相關(guān)優(yōu)化修改。

問題點(diǎn):原始版本在onCreate時(shí)就立刻播放動(dòng)畫,無法靈活的停止音樂的動(dòng)畫效果。

更改方式:動(dòng)畫的更新需要獨(dú)立封裝,點(diǎn)擊播放就進(jìn)行動(dòng)畫展示,點(diǎn)擊暫停就進(jìn)行暫停。


舊代碼:StoryViewController.class

mContext.runOnUiThread(new Runnable() {

@Override

public void run() {

Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimation.setDuration(10000);

rotateAnimation.setFillAfter(true);

rotateAnimation.setRepeatCount(Animation.INFINITE);

rotateAnimation.setInterpolator(mContext, android.R.anim.linear_interpolator);//設(shè)置動(dòng)畫插入器

mRotate.startAnimation(rotateAnimation);//立即執(zhí)行動(dòng)畫

}

});


新代碼: StoryViewController.class




上面問題涉及到的知識(shí)點(diǎn):自定義接口,當(dāng)音樂狀態(tài)更新時(shí)進(jìn)行回調(diào),更新UI. (接口的定義方式)

1: StoryViewController.class

注冊:

playerManager.setmOnPlayerStatusListener(mStatusListener);

回調(diào):

private IMediaPlayerStatusListener mStatusListener = new IMediaPlayerStatusListener() {

@Override

public void onPlay() {

mPlayState.setBackgroundResource(R.drawable.music_start_selector);

rotateAnimation.startNow();

}

@Override

public void onPause() {

mPlayState.setBackgroundResource(R.drawable.music_pause_selector);

rotateAnimation.cancel();

}

};

2: MediaPlayerManager.class

public void setmOnPlayerStatusListener(IMediaPlayerStatusListener statusListener) {

this.mOnPlayerStatusListener = statusListener;

}

//當(dāng)音樂狀態(tài)更新時(shí)進(jìn)行回調(diào),更新UI.

public void pause() {

if (isPlaying()) {

try {

mMediaPlayer.pause();

} catch (IllegalStateException e) {

LogUtils.d(TAG, "pause() an Error has occur..." + e.getMessage());

}

}

if (mOnPlayerStatusListener != null) {

mOnPlayerStatusListener.onPause();

}

}

3:接口定義

public interface IMediaPlayerStatusListener {

voidonPlay();

voidonPause();

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容