自定義旋轉加載進度

Android自帶的加載旋轉框樣式太醜,或是咱們有特殊的需求須要自定義加載進度條。java

進度條核心類,本類繼承自Dialogandroid

public class MyProgressDialog extends Dialog {
   private static MyProgressDialog dialog;

   private MyProgressDialog(Context context, int style) {
      super(context, style);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.progress_bar_item);
      /**
      *此處經過幀動畫設置旋轉效果
      **/
      ImageView iv = (ImageView) findViewById(R.id.progressbar);
      iv.setBackgroundResource(R.drawable.progress_dialog);  //幀動畫
      AnimationDrawab le ad = (AnimationDrawable) iv.getBackground();
      ad.start();
   }

   public static MyProgressDialog createDialog(Context context, String msg) {
      dialog = new MyProgressDialog(context, R.style.add_dialog);
      Window dialogWindow = dialog.getWindow();
      WindowManager.LayoutParams lp = dialogWindow.getAttributes();
      lp.width = DensityUtil.dip2px(80);
      lp.height = DensityUtil.dip2px(80);
      return dialog;
   }

}

Window dialogWindow = dialog.getWindow();
      WindowManager.LayoutParams lp = dialogWindow.getAttributes();
      lp.width = DensityUtil.dip2px(80);
      lp.height = DensityUtil.dip2px(80);

能夠看到這幾行代碼,控制dialog對話框的大小,這裏是整個類的關鍵。若是去掉這幾行你會發現,在你的旋轉的動畫周邊會有一個默認的黑邊。有人說能夠在佈局文件中控制,這也是我一開始的想到的,可是結果是殘酷的,通過一番痛苦的掙扎後發現此處必須得從Dialog自己的屬性出發去設置,纔能有好的效果。佈局


佈局文件add_dialog動畫

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
  
        <ImageView android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/progressbar"/>
        

</LinearLayout>

Dialog樣式code

<style name="add_dialog" parent="@android:style/Theme.Dialog">  
    <item name="android:windowFrame">@null</item><!-- 邊框 -->  
    <item name="android:windowIsFloating">true</item><!-- 是否浮如今activity之上 -->  
    <item name="android:windowIsTranslucent">true</item><!-- 半透明 -->  
    <item name="android:windowNoTitle">true</item><!-- 無標題 -->  
    <item name="android:alpha">1</item>
    <item name="android:windowBackground">@color/lightBlack</item>
    <item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->  
</style>

這裏很少作解釋,這些樣式你們在官網上均可以查到。xml

用法以下:繼承

processBar = MyProgressDialog.createDialog(context, tips);
processBar.show();

如上是本人的效果截圖,哪位大俠有更好的方法能夠來討論。謝謝ip

相關文章
相關標籤/搜索