記錄一個(gè)遇到的問題
使用Lottie優(yōu)雅的實(shí)現(xiàn)矢量動(dòng)畫,用起來妙啊,能夠做到普通幀動(dòng)畫無法做到的功能,模仿的騰訊視頻底部切換的動(dòng)畫.但是使用中發(fā)現(xiàn)了下圖的問題.
我在這里點(diǎn)了快速運(yùn)行,導(dǎo)致本身四個(gè)不同的圖片變成和最后一個(gè)圖片一樣的了.,其實(shí)旋轉(zhuǎn)屏也可以復(fù)現(xiàn)!
然鵝,如果出現(xiàn)這個(gè)問題的時(shí)候是旋轉(zhuǎn)屏,我會(huì)直接曉得是場景恢復(fù)導(dǎo)致的,但是我發(fā)現(xiàn)的時(shí)候是快速運(yùn)行和崩潰的時(shí)候復(fù)現(xiàn)的,我沒有聯(lián)想到,所以我找了好久.
那么問題已經(jīng)找到了就是場景恢復(fù)導(dǎo)致的,那就去定位問題吧.
lottie 源碼版本
implementation 'com.airbnb.android:lottie:2.7.0'
既然大致知道是場景恢復(fù)引發(fā)的這個(gè)事件,那么就直接查LottieAnimationView的這兩個(gè)方法
- onSaveInstanceState
- onRestoreInstanceState
看了一下LottieAnimationView果然重寫了這兩個(gè)方法
onSaveInstanceState,果然保存了動(dòng)畫的屬性,o(╥﹏╥)o
@Override protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.animationName = animationName;
ss.animationResId = animationResId;
ss.progress = lottieDrawable.getProgress();
ss.isAnimating = lottieDrawable.isAnimating();
ss.imageAssetsFolder = lottieDrawable.getImageAssetsFolder();
ss.repeatMode = lottieDrawable.getRepeatMode();
ss.repeatCount = lottieDrawable.getRepeatCount();
return ss;
}
onRestoreInstanceState 恢復(fù)的時(shí)候把對應(yīng)的動(dòng)畫屬性恢復(fù)過來了
@Override protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
animationName = ss.animationName;
if (!TextUtils.isEmpty(animationName)) {
setAnimation(animationName);
}
animationResId = ss.animationResId;
if (animationResId != 0) {
setAnimation(animationResId);
}
setProgress(ss.progress);
if (ss.isAnimating) {
playAnimation();
}
lottieDrawable.setImagesAssetsFolder(ss.imageAssetsFolder);
setRepeatMode(ss.repeatMode);
setRepeatCount(ss.repeatCount);
}
你會(huì)說沒毛病呀,一個(gè)對象一個(gè)資源,不會(huì)導(dǎo)致4個(gè)對象使用一個(gè)資源呀!這里先把原因說出來.
這四個(gè)LottieAnimationView有相同的id.
給你看個(gè)布局 R.layout.view_maintab,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/view_maintab_img"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
//...省略
public class MainTab extends FrameLayout {
public MainTab(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.view_maintab,
this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MainTab);
String titleContent = typedArray.getString(R.styleable.MainTab_text);
Drawable drawable = typedArray.getDrawable(R.styleable.MainTab_img_res);
typedArray.recycle();
自定義了一個(gè)FrameLayout用來裝布局,然后寫一些共同的屬性,來實(shí)現(xiàn)切換的tab!
問題原因:
這里因?yàn)閳鼍盎謴?fù)的數(shù)據(jù)通過onSaveInstanceState()先存到一個(gè)SparseArray<Parcelable> container集合里,通過dispatchRestoreInstanceState()通過id來取出對應(yīng)的數(shù)據(jù).
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID) {
//注意看這里
Parcelable state = container.get(mID);
if (state != null) {
mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
onRestoreInstanceState(state);
//....
}
}
}
在執(zhí)行onSaveInstanceState()的時(shí)候最后一個(gè)tab因?yàn)閕d和前面的三個(gè)一致,直接覆蓋了前面的數(shù)據(jù).這就是問題的原因,那怎么辦呢?
解決方式:
不寫id了就好了唄!那你怎么獲取對象呢?可以使用getChild()....等方式拿到對象.