Android幀動(dòng)畫(huà)animation-list在ProgressBar上失效的解決辦法

做Loading的時(shí)候,幀動(dòng)畫(huà)我們經(jīng)常用到,主要是一些比較復(fù)雜的動(dòng)畫(huà),比如小人跑動(dòng),人物翻轉(zhuǎn)等等;
常規(guī)的做法參考:

drawable下放一個(gè)如下的文件loading_a,圖片是連續(xù)的幾張圖切分

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:variablePadding="true">
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_1"
            android:gravity="left"></clip>
    </item>
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_2"
            android:gravity="left"></clip>
    </item>
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_3"
            android:gravity="left"></clip>
    </item>
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_4"
            android:gravity="left"></clip>
    </item>
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_5"
            android:gravity="left"></clip>
    </item>
    <item android:duration="100">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:drawable="@drawable/ic_popup_sync_6"
            android:gravity="left"></clip>
    </item>

</animation-list>


然后在自定義一個(gè)Dialog,ProgressA

public class ProgressA extends Dialog {
    private View mView;
    private ProgressBar mProgressBar;

    public ProgressA(Context context) {
        super(context,android.support.v7.appcompat.R.style.Base_Theme_AppCompat_Dialog);
        mView = LayoutInflater.from(context).inflate(R.layout.progress_dialog_a, null);
        mProgressBar=(ProgressBar)mView.findViewById(R.id.progressbar);
        setContentView(mView);
    }
}

progress_dialog_a的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:indeterminateBehavior="repeat"
        android:indeterminateDrawable="@drawable/loading_a"
        android:indeterminateDuration="600" />
</LinearLayout>

然后Activity如下:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_loadingA).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showA();
            }
        });
    }


    private void showA(){
        final ProgressA progressA = new ProgressA(this);
        progressA.setCanceledOnTouchOutside(false);
        progressA.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                progressA.cancel();
            }
        },2000);
    }
}

可以發(fā)現(xiàn)這些寫(xiě)了之后可以基本上滿(mǎn)足常用手機(jī)的需求,但是最近我發(fā)現(xiàn)在一些手機(jī)上會(huì)出現(xiàn)如下2個(gè)問(wèn)題:

1,在Android6.0上此動(dòng)畫(huà)和6.0以下在顯示上有區(qū)別(已解決);
2,在加載動(dòng)畫(huà)的時(shí)候中興V5以及其他某些機(jī)型會(huì)產(chǎn)生只加載第一張圖余下的幾張都不加載的情況;

如下圖在6.0上無(wú)法顯示loading圖:


彈出

當(dāng)然這個(gè)問(wèn)題很容易解決:
我們只需要在ProgressA中加上判斷就可以了

public class ProgressA extends Dialog {
    private View mView;
    private ProgressBar mProgressBar;

    public ProgressA(Context context) {
        super(context,android.support.v7.appcompat.R.style.Base_Theme_AppCompat_Dialog);
        mView = LayoutInflater.from(context).inflate(R.layout.progress_dialog_a, null);
        mProgressBar=(ProgressBar)mView.findViewById(R.id.progressbar);
        if (android.os.Build.VERSION.SDK_INT > 22) {//android 6.0替換clip的加載動(dòng)畫(huà)
            final Drawable drawable =  context.getApplicationContext().getResources().getDrawable(R.drawable.loading_a_6);
            mProgressBar.setIndeterminateDrawable(drawable);
        }
        setContentView(mView);
    }
}

其中l(wèi)oading_a_6代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:variablePadding="true">
    <item
        android:drawable="@drawable/ic_popup_sync_1"
        android:duration="100"
        android:gravity="center"></item>
    <item
        android:drawable="@drawable/ic_popup_sync_2"
        android:duration="100"
        android:gravity="center"></item>
    <item
        android:drawable="@drawable/ic_popup_sync_3"
        android:duration="100"
        android:gravity="center"></item>
    <item
        android:drawable="@drawable/ic_popup_sync_4"
        android:duration="100"
        android:gravity="center"></item>
    <item
        android:drawable="@drawable/ic_popup_sync_5"
        android:duration="100"
        android:gravity="center"></item>
    <item
        android:drawable="@drawable/ic_popup_sync_6"
        android:duration="100"
        android:gravity="center"></item>
</animation-list>

可是第二個(gè)問(wèn)題我找遍所有方法都解決不了,后來(lái)我跟蹤該機(jī)型發(fā)現(xiàn)只要是xml布局中的幀動(dòng)畫(huà)都無(wú)法用,廠(chǎng)商修改源碼的時(shí)候不知道改了啥,很是蛋疼!
為了解決這個(gè)問(wèn)題,我只能棄用ProgressBar了,改用ImageView,結(jié)果完美解決以上2個(gè)問(wèn)題
解決方法:

直接在自定義的Dialog中用ImageView替代ProgressBar來(lái)做,換這種方法可以一次解決上面2個(gè)問(wèn)題

public class ProgressB extends Dialog {
    private View mView;
    private ImageView iv;
    private AnimationDrawable mAnimation;

    @SuppressLint("NewApi")
    public ProgressB(Context context) {
        super(context, android.support.v7.appcompat.R.style.Base_Theme_AppCompat_Dialog);
        mView = LayoutInflater.from(context).inflate(R.layout.progress_dialog_b, null);
        iv = (ImageView) mView.findViewById(R.id.iv_loading);
        mAnimation = new AnimationDrawable();
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_1),100);
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_2),100);
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_3),100);
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_4),100);
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_5),100);
        mAnimation.addFrame(getContext().getResources().getDrawable(R.drawable.ic_popup_sync_6),100);
        mAnimation.setOneShot(false);
        iv.setBackground(mAnimation);
        if (mAnimation != null && !mAnimation.isRunning()) {
            mAnimation.start();
        }
        setContentView(mView);
    }
}

這里寫(xiě)圖片描述

代碼地址:
https://github.com/hloong/progressbar

總結(jié):因?yàn)闄C(jī)型繁多,很多系統(tǒng)方法在一些手機(jī)上無(wú)法使用或者用起來(lái)不是很爽,最后只能自定義的情況還是挺多的,
以前寫(xiě)過(guò)一篇頭像的也是系統(tǒng)方法不好用只能自定義:
http://www.hloong.com/?p=328

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

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

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