做Loading的時候,幀動畫我們經(jīng)常用到属百,主要是一些比較復(fù)雜的動畫茬末,比如小人跑動银亲,人物翻轉(zhuǎn)等等;
常規(guī)的做法參考:
drawable下放一個如下的文件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>
然后在自定義一個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)這些寫了之后可以基本上滿足常用手機(jī)的需求务蝠,但是最近我發(fā)現(xiàn)在一些手機(jī)上會出現(xiàn)如下2個問題:
1拍谐,在Android6.0上此動畫和6.0以下在顯示上有區(qū)別(已解決);
2请梢,在加載動畫的時候中興V5以及其他某些機(jī)型會產(chǎn)生只加載第一張圖余下的幾張都不加載的情況;
如下圖在6.0上無法顯示loading圖:
當(dāng)然這個問題很容易解決:
我們只需要在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);
}
}
其中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>
可是第二個問題我找遍所有方法都解決不了力穗,后來我跟蹤該機(jī)型發(fā)現(xiàn)只要是xml布局中的幀動畫都無法用毅弧,廠商修改源碼的時候不知道改了啥,很是蛋疼当窗!
為了解決這個問題够坐,我只能棄用ProgressBar了,改用ImageView崖面,結(jié)果完美解決以上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
總結(jié):因?yàn)闄C(jī)型繁多,很多系統(tǒng)方法在一些手機(jī)上無法使用或者用起來不是很爽巫员,最后只能自定義的情況還是挺多的庶香,
以前寫過一篇頭像的也是系統(tǒng)方法不好用只能自定義:
http://www.hloong.com/?p=328