zoukankan      html  css  js  c++  java
  • 自己定义圆形进度条

    在工作中遇到一个问题,在跳转到一个有多页Fragment数据的Activity的时候,Activity的打开速度非常慢。

    于是就想到了加入一个loading进度条。让用户知道正在载入数据。而不至于觉得应用无响应。

    自己定义进度条能够用三种方法实现。

    能够自己定义动画,像帧动画那样实现loading旋转载入的效果。

    能够使用一张图片。实现自己定义。

    也能够通过颜色实现自己定义。

    1.自己定义动画实现

    在res/anim/loading.xml下定义的xml文件

    <?xml version="1.0" encoding="UTF-8"?>  

    <animation-list android:oneshot="false"  

    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>

    2.使用图片进行自己定义。在res/drawable/loading.xml文件

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"  

        android:drawable="@drawable/spinner_black_16"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:fromDegrees="0"  
        android:toDegrees="360" />

    3.通过颜色进行自己定义,在res/drawable/loading.xml文件

    <?xml version="1.0" encoding="utf-8"?

    >  

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
        android:fromDegrees="0"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:toDegrees="360" >  
        <shape  
            android:innerRadiusRatio="3"  
            android:shape="ring"  
            android:thicknessRatio="8"  
            android:useLevel="false" > 
            <gradient  
                android:centerColor="#FFFFFF"  
                android:centerY="0.50"  
                android:endColor="#1E90FF"  
                android:startColor="#000000"  
                android:type="sweep"  
                android:useLevel="false" />  
        </shape>  
    </rotate>  

    在使用的时候

    <ProgressBar
            android:id="@+id/loading"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:indeterminateDrawable="@drawable/loadingl" 
            />

    注意,使用动画方式的和使用图片颜色方式的。要注意差别是在哪个资源目录下的,drawable和anim目录是不一样的。

    对于使用颜色方式自己定义的loading做一下说明:

    fromDegrees:旋转动画 開始的角度

    toDegrees:旋转动画结束的角度
    pivotX:动画相对于控件的 x 坐标的位置
    pivotY:动画相对于控件的 y 坐标的位置
    innerRadiusRatio:以环的宽度比率来表示内环的半径,能够被innerRadiu覆盖
    innerRadiu:指定内环半径
    thickness:指定环的厚度
    thicknessRatio:以环的宽度比率来表示环的厚度。能够被thickness覆盖
    shape:控件形状:rectangle(矩形),oval(椭圆),line(线条), ring(环)。默认矩形
    gradient设置填充的渐变色  
            startColor:起始颜色
            centerColor:中间渐变的颜色
            endColor:结束颜色
            angle设置渐变的角度,仅当渐变类型为线性渐变时有效。
                            默认0值为水平向右,其它角度以逆时针旋转为准,比如设置为90则是自下而上。
                            设置为270自上而下,以此类推。

    注意:必须是45的倍数。

            centerX、centerY两个属性用于设置渐变的中心点位置。
                            仅当渐变类型为放射渐变时有效。类型为分数或小数,不接受Dimension。
                            默认值是0.5,有效值是0.0~1.0。超出该范围后会看不出渐变效果。
         useLevel属性通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用。默认值为false。


  • 相关阅读:
    多线程《三》进程与线程的区别
    多线程《二》开启线程的两种方式
    多线程《一》线程理论
    多进程《七》生产者消费者模型
    多进程《六》队列
    互斥锁与join
    多进程《五》互斥锁
    多进程《四》守护进程
    再度认识未来——2.11
    开始——2.10
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8429997.html
Copyright © 2011-2022 走看看