美文网首页Android自定义控件
Android自定义进度条

Android自定义进度条

作者: 蒙伟 | 来源:发表于2018-05-21 19:48 被阅读32次

    核心代码:

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                //动画进度条
                case R.id.anim:
                    //显示动画进度条
                    showRoundProcessDialog(this, R.layout.loading_process_dialog_anim);
                    break;
                //颜色进度条
                case R.id.color:
                    //显示颜色进度条
                    showRoundProcessDialog(this, R.layout.loading_process_dialog_color);
                    break;
                //图片进度条
                case R.id.icon:
                    //显示图片进度条
                    showRoundProcessDialog(this, R.layout.loading_process_dialog_icon);
                    break;
                default:
                    break;
            }
        }
    
        public void showRoundProcessDialog(Context mContext, int layout) {
            //初始化进度条
            mDialog = new AlertDialog.Builder(mContext).create();
            //显示进度条
            mDialog.show();
            // 注意此处要放在show之后 否则会报异常
            mDialog.setContentView(layout);
            // mDialog.setCancelable(false); //false设置点击其他地方不能取消进度条
        }
    

    动画进度条: loading_process_dialog_anim.xml

    <?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="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <ProgressBar
            android:id="@+id/loading_process_dialog_progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:indeterminateDrawable="@drawable/loading" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading" />
    
    </LinearLayout>
    

    loading.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <animation-list  xmlns:android="http://schemas.android.com/apk/res/android">
        <!--逐帧动画-->
        <item android:duration="150" android:drawable="@drawable/loading_01" />
        <item android:duration="150" android:drawable="@drawable/loading_02" />
        <item android:duration="150" android:drawable="@drawable/loading_03" />
        <item android:duration="150" android:drawable="@drawable/loading_04" />
        <item android:duration="150" android:drawable="@drawable/loading_05" />
        <item android:duration="150" android:drawable="@drawable/loading_06" />
        <item android:duration="150" android:drawable="@drawable/loading_07" />
    </animation-list>
    

    颜色进度条: loading_process_dialog_color.xml

    <?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="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <ProgressBar
            android:id="@+id/loading_process_dialog_progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:indeterminateDrawable="@drawable/dialog_style_xml_color" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading" />
    
    </LinearLayout>
    

    dialog_style_xml_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
        android:toDegrees="360">
        
        <shape android:shape="ring" android:innerRadiusRatio="3"
            android:thicknessRatio="8" android:useLevel="false">
            <gradient android:type="sweep" android:useLevel="false"
                android:startColor="#FFFFFF" android:centerColor="#FFDC35"
                android:centerY="0.50" android:endColor="#CE0000" />
        </shape>
    </rotate>
    

    图片进度条: loading_process_dialog_icon.xml

    <?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="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <ProgressBar
            android:id="@+id/loading_process_dialog_progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:indeterminateDrawable="@drawable/dialog_style_xml_icon" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading" />
    
    </LinearLayout>
    

    dialog_style_xml_icon.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <rotate
                android:drawable="@drawable/dialog_progress_round"
                android:fromDegrees="0.0"
                android:pivotX="50.0%"
                android:pivotY="50.0%"
                android:toDegrees="1080.0"/>
            <!--设置动画时间无效,将toDegrees增大可以加快旋转速度-->
        </item>
    </layer-list>
    

    效果图:


    2018-05-21 19_40_18.gif

    源码地址:https://github.com/280357392/ProgressBar

    相关文章

      网友评论

        本文标题:Android自定义进度条

        本文链接:https://www.haomeiwen.com/subject/tkbqjftx.html