自定义旋转加载进度

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

相关文章
相关标签/搜索