Android幀動畫animation-list在ProgressBar上失效的解決辦法

做Loading的時候,幀動畫我們經常用到,主要是一些比較複雜的動畫,比如小人跑動,人物翻轉等等;
常規的做法參考:

drawable下放一個如下的文件loading_a,圖片是連續的幾張圖切分

<?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>

然後在自定義一個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);
    }
}

可以發現這些寫了之後可以基本上滿足常用手機的需求,但是最近我發現在一些手機上會出現如下2個問題:

1,在Android6.0上此動畫和6.0以下在顯示上有區別(已解決);
2,在加載動畫的時候中興V5以及其他某些機型會產生只加載第一張圖餘下的幾張都不加載的情況;

如下圖在6.0上無法顯示loading圖:
彈出

當然這個問題很容易解決:
我們只需要在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的加載動畫
            final Drawable drawable =  context.getApplicationContext().getResources().getDrawable(R.drawable.loading_a_6);
            mProgressBar.setIndeterminateDrawable(drawable);
        }
        setContentView(mView);
    }
}

其中loading_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>

可是第二個問題我找遍所有方法都解決不了,後來我跟蹤該機型發現只要是xml佈局中的幀動畫都無法用,廠商修改源碼的時候不知道改了啥,很是蛋疼!
爲了解決這個問題,我只能棄用ProgressBar了,改用ImageView,結果完美解決以上2個問題
解決方法:

直接在自定義的Dialog中用ImageView替代ProgressBar來做,換這種方法可以一次解決上面2個問題

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);
    }
}

這裏寫圖片描述

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

總結:因爲機型繁多,很多系統方法在一些手機上無法使用或者用起來不是很爽,最後只能自定義的情況還是挺多的,以前寫過一篇頭像的也是系統方法不好用只能自定義:http://blog.csdn.net/tmacsky/article/details/51179789

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章