2种方式实现带进度的圆形进度条

progressbar默认为水平和圆形进度条,但圆形的进度条是没有进度的。下面提供2中方式实现带进度的圆形进度条。

1、修改progressbar的默认样式。

<ProgressBar
        android:id="@+id/circularProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:clickable="false"
        android:indeterminate="false"
        android:max="100"
        android:progress="50"
        android:rotation="270"
        android:progressDrawable="@drawable/circular_drawable" />

drawable/circular_progress_bar.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:innerRadiusRatio="@dimen/circular_progress_bar_inner_radius_ratio"
    android:thicknessRatio="@dimen/circular_progress_bar_thickness_ratio"
    android:shape="ring"
    android:useLevel="true">
    <solid android:color="@color/circular_progress_bar" />
</shape>

dimems.xml

<resources>

    <!-- Circular Progress Bar Sizes -->
    <item format="float" type="dimen" name="circular_progress_bar_inner_radius_ratio">3</item>
    <item format="float" type="dimen" name="circular_progress_bar_thickness_ratio">10</item>

</resources>

第二种方式:继承progressbarbar,然后自己画圆弧的方式。。

<com.test.maria.widget.RoundProgressBar
            android:id="@+id/download_pb"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:max="100"
            android:padding="1dp"
            android:progress="50"
            roundprogress:progress_reached_bar_height="2dp"
            roundprogress:progress_reached_color="@color/application_main_color"
            roundprogress:radius="11.5dp" />

attrs.xml

 <!-- roundProgressbar -->
    <declare-styleable name="RoundProgressBarWidthNumber">
        <attr name="progress_reached_color" format="color" />
        <attr name="progress_reached_bar_height" format="dimension" />
        <attr name="radius" format="dimension" />
    </declare-styleable>
package com.test.maria.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

public class RoundProgressBar extends ProgressBar {
    private static final String TAG = "RoundProgressBar";

    private static final int DEFAULT_TEXT_COLOR = Color.RED;
    private static final float DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2f;
    private static final float DEFAULT_HEIGHT_RADIUS = 22 / 2f;

    private float mRadius = dp2px(DEFAULT_HEIGHT_RADIUS);
    private float mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
    private int mReachedBarColor;

    private Paint mPaint;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setHorizontalScrollBarEnabled(true);
        this.setIndeterminate(false);

        initAttrs(attrs);
        initPaint();
    }

    private void initAttrs(AttributeSet attrs) {
        final TypedArray attributes = getContext().obtainStyledAttributes(attrs,
                R.styleable.RoundProgressBarWidthNumber);
        mReachedBarColor = attributes.getColor(R.styleable.RoundProgressBarWidthNumber_progress_reached_color,
                DEFAULT_TEXT_COLOR);
        mReachedProgressBarHeight = (int) attributes.getDimension(
                R.styleable.RoundProgressBarWidthNumber_progress_reached_bar_height, mReachedProgressBarHeight);
        mRadius = (int) attributes.getDimension(R.styleable.RoundProgressBarWidthNumber_radius, mRadius);
        attributes.recycle();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(mReachedBarColor);
        mPaint.setStrokeCap(Cap.BUTT);
        mPaint.setStrokeWidth(mReachedProgressBarHeight);
    }

    protected float dp2px(float dpVal) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        float paintWidth = mReachedProgressBarHeight;
        // 如果不是 指定的长宽/fillparent
        if (heightMode != MeasureSpec.EXACTLY) {
            int exceptHeight = (int) (getPaddingTop() + getPaddingBottom() + mRadius * 2 + paintWidth);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight, MeasureSpec.EXACTLY);
        }
        if (widthMode != MeasureSpec.EXACTLY) {
            int exceptWidth = (int) (getPaddingLeft() + getPaddingRight() + mRadius * 2 + paintWidth);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(exceptWidth, MeasureSpec.EXACTLY);
        }
        super.onMeasure(heightMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;
        canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 270, sweepAngle, false, mPaint);
        canvas.restore();
    }

}

附上效果图

 

fu

fun

funs

funsh

funshi

funshio

funshion

时间: 2024-08-28 19:43:17

2种方式实现带进度的圆形进度条的相关文章

Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

转载请注明地址:http://blog.csdn.net/xiaanming/article/details/10298163 很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为了我们自定义的View在一个项目中能够重用,有时候我们需要自定义其属性,举个很简单的例子,我在项目中的多个界面使用我自定义的View,每个界面该自定义View

Android 带进度的圆形进度条

extends:http://blog.csdn.net/xiaanming/article/details/10298163 转载请注明地址:http://blog.csdn.net/xiaanming/article/details/10298163 很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为了我们自定义的View在一个项

HTML5 canvas带渐变色的圆形进度条动画

query-circle-progress是一款带渐变色的圆形进度条动画特效jQuery插件.该圆形进度条使用的是HTML5 canvas来绘制圆形进度条及其动画效果,进度条使用渐变色来填充,效果非常的酷. 效果演示:http://www.htmleaf.com/Demo/201505271919.html 下载地址:http://www.htmleaf.com/html5/html5-canvas/201505271918.html

Android 高手进阶,自己定义圆形进度条

背景介绍 在Android 开发中,我们常常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,许多时候须要我们自定义控件,在开发的过程中.我们公司遇到了一种须要自己写的一个自定义带进度的圆形进度条,看起来很的绚丽,当然另一些其它的.比方:水纹形的圆形进度条等效果都是很nice的.假设哪位朋友有实现,希望分享出来,我也好学习学习. 好了多的不说.接下来,我们就来看看来怎样实现圆形进度条. 原文地址:http://blog.csdn.net/xiaanming/arti

Android 高手进阶,自定义圆形进度条

背景介绍 在Android 开发中,我们经常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,很多时候需要我们自己定义控件,在开发的过程中,我们公司遇到了一种需要自己写的一个自定义带进度的圆形进度条,看起来非常的绚丽,当然还有一些其他的,比如:水纹形的圆形进度条等效果都是非常nice的.如果哪位朋友有实现,希望分享出来,我也好学习学习.好了多的不说,接下来,我们就来看看来如何实现圆形进度条. 原文地址:http://blog.csdn.net/xiaanming/a

音频播放的三种方式:

第一种方式:不带面板: /** * */ package com.niit.hitmouse; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.Unsuppor

Spring 实例化Bean的两种方式

使用Spring管理Bean也称依赖注入( Dependency Injection, DI ),通过这种方式将Bean的控制权交给Spring 在使用Spring实例化一个对象时,无论类是否有参数都会默认调用对象类的无参构造,对于有参数的情况,Spring有两种方式可以带参实例化 示例类 Shape public class Shape { private Integer width; private Integer height; public Shape() { System.out.pr

创建一对多表结构实例 /操作的三种方式

例 1.注册App01  完成各项配置 2. 写完后自动生成一个id自增列(主键) 如果不想生成 自己写 创建两张表 3.执行创建语句 (其中还进行了一个小修改) 4.按照之前的方法 打开数据库 并输入数据 5.修改表结构 法一: 在更新时 遇到选择 因为已经存入数据 新建列默认不能为Null 默认为sa 注意输入的是字符串 刷新 法二: 法三: ====================== 接下来进行view 应该先看到业务线  再看到主机 1.urls 注意:如果同时有 bussiness

【Android进度条】三种方式实现自定义圆形进度条ProgressBar

一.通过动画实现 定义res/anim/loading.xml如下: [html] view plaincopyprint? <?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> &