标签:
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(); } }
附上效果图
标签:
原文地址:http://blog.csdn.net/leehu1987/article/details/43985113