自定义圆形ProgressBar背景的三种方法

1.通过动画实现

anim文件夹下:custom_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/loading_0"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_3"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_4"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_5"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_6"
        android:duration="100"/>
    <item
        android:drawable="@drawable/loading_7"
        android:duration="100"/>

</animation-list>

在布局文件中引用方法:

<ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dip"
        android:layout_marginTop="10dip"
        android:indeterminate="false"
        android:indeterminateDrawable="@anim/custom_progress" />

2.通过一张图片作为背景

custom.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:drawable="@drawable/loading"
            android:fromDegrees="0.0"
            android:pivotX="50.0%"
            android:pivotY="50.0%"
            android:toDegrees="359.0" />
    </item>
</layer-list>
在布局文件中引用方法:
<ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:indeterminateDrawable="@drawable/custom"
        />

3.通过自定义形状

shape.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="359" >
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >
        <gradient
            android:centerColor="#FFDC35"
            android:centerY="0.50"
            android:endColor="#CE0000"
            android:startColor="#FFFFFF"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>
在布局文件中引用方法:

<ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false" 
        android:indeterminateDrawable="@drawable/shape" />

以下内容摘自百度经验:

Android中shape的使用

  1. 一、在res/drawable文件夹下创建一个名为gradient_box的xml文件:

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

    <!-- 

    shape drawable xml文件中定义的一个几何图形,定义在res/drawable/目录下,文件名filename称为访问的资源ID

    在代码中通过R.drawable.filename进行访问,在xml文件中通过@[package:]drawable/filename进行访问。

     -->

     <!-- 

      android:shape=["rectangle" | "oval" | "line" | "ring"]

      shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)

      下面的属性只有在android:shape="ring时可用:

      android:innerRadius 尺寸,内环的半径。

      android:innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径,

      例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.

      android:thickness 尺寸,环的厚度

      android:thicknessRatio 浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",

      那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.

      android:useLevel boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.

      -->

    <shape

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

        android:shape="rectangle">

        

        <!--

          圆角

          android:radius   整型 半径

          android:topLeftRadius   整型 左上角半径

          android:topRightRadius   整型 右上角半径

          android:bottomLeftRadius 整型 左下角半径

          android:bottomRightRadius 整型 右下角半径

         -->

         <corners  

            android:radius="8dp"

            android:topLeftRadius="5dp"

            android:topRightRadius="15dp"

            android:bottomLeftRadius="20dp"

            android:bottomRightRadius="25dp"  

            />

         

         <!--

            渐变色

            android:startColor  颜色值 起始颜色

            android:endColor    颜色值 结束颜色

            android:centerColor 整型   渐变中间颜色,即开始颜色与结束颜色之间的颜色

            android:angle       整型   渐变角度(PS:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍)

            android:type        ["linear" | "radial" | "sweep"] 渐变类型(取值:linear、radial、sweep)

                                linear 线性渐变,这是默认设置

                                radial 放射性渐变,以开始色为中心。

                                sweep 扫描线式的渐变。

           android:useLevel   ["true" | "false"] 如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。false有渐变色

           android:gradientRadius 整型 渐变色半径.当 android:type="radial" 时才使用。单独使用 android:type="radial"会报错。

           android:centerX     整型   渐变中心X点坐标的相对位置

           android:centerY   整型   渐变中心Y点坐标的相对位置

        -->

        <gradient

            android:startColor="#FFFF0000"

            android:endColor="#80FF00FF"

            android:angle="45"

            /> 

            

        <!--

              内边距,即内容与边的距离 

              android:left   整型 左内边距

              android:top   整型 上内边距

              android:right   整型 右内边距

              android:bottom 整型 下内边距

          -->

         <padding 

             android:left="10dp"

             android:top="10dp"

             android:right="10dp"

             android:bottom="10dp"

             />

         

        <!-- 

           size 大小

           android:width 整型 宽度

           android:height 整型 高度

        -->

        <size

            android:width="600dp"

            />

        

        <!--

            内部填充

            android:color 颜色值 填充颜色

        -->

        <solid 

            android:color="#ffff9d77"

            />

        

         <!--

             描边

             android:width 整型 描边的宽度

             android:color 颜色值 描边的颜色

             android:dashWidth 整型 表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线。

             android:dashGap   整型 表示描边为虚线时,虚线之间的间隔 即“ - - - - ”

         -->

         <stroke 

            android:width="2dp"

            android:color="#dcdcdc"  

            /> 

    </shape>

  2. 二、在窗口布局文件中将步骤一中创建的文件作为TextView的背景:

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

    <LinearLayout

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

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="ShapeTest"

            android:background="@drawable/gradient_box"

            android:textSize="24.0dp"

            android:textColor="@android:color/black"

            />

    </LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
总共分为三层:一层为圆形边线,一层为进度边线,一层用来显示标识进度节点。 public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 15; private int progressStrokeWidth = 2; private int marxArcStorkeWidth = 16; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); width = (width > height) ? height : width; height = (width > height) ? height : width; paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.WHITE); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = marxArcStorkeWidth / 2; // 左上角x oval.top = marxArcStorkeWidth / 2; // 左上角y oval.right = width - marxArcStorkeWidth / 2; // 左下角x oval.bottom = height - marxArcStorkeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setStrokeWidth(marxArcStorkeWidth); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "%"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } /** * 设置进度 * * @param progress * 进度百分比 * @param view * 标识进度的节点视图 */ public void setProgress(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int initDegress = 306;// 进度点起始位置(图片偏移约54度) RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(1);// 设置动画执行时间 animation.setRepeatCount(1);// 设置重复执行次数 animation.setFillAfter(true);// 设置动画结束后是否停留在结束位置 return animation; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值