效果
国际惯例,效果奉上
前言
上一篇写出了横向的进度条,这个自定义View
是继承自上一个自定义横向进度条,如果没看过上一篇,可能很多的数据都并不是看的很懂;当然希望大家去多去看看我写的文章,在此,谢过大家了;
正文
RoundnessProgressBar
public class RoundnessProgressBar extends HorizontlProgressar {
private int mMaxPaintWidth;
private int mRadius = dp2px(30);
public RoundnessProgressBar(Context context) {
this(context,null);
}
public RoundnessProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
mReachedProgressBarHeight = (int) (mUnReachedProgressBarHeight * 2.5f);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBarWidthNumber);
mRadius = (int) typedArray.getDimension(R.styleable.RoundProgressBarWidthNumber_radius,mRadius);
typedArray.recycle();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mMaxPaintWidth = Math.max(mReachedProgressBarHeight,mUnReachedProgressBarHeight);
int expect = mRadius * 2 + mMaxPaintWidth +getPaddingLeft()+ getPaddingRight();
int width = resolveSize(expect,widthMeasureSpec);
int height = resolveSize(expect,heightMeasureSpec);
int realWidth = Math.min(width,height);
mRadius = (realWidth - getPaddingRight() - getPaddingLeft() - mMaxPaintWidth)/2;
setMeasuredDimension(realWidth,realWidth);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
String text = getProgress() + "%";
float textWidth = mPaint.measureText(text);
float textHeight = (mPaint.descent() + mPaint.ascent())/2;
canvas.save();
canvas.translate(getPaddingLeft() + mMaxPaintWidth/2,getPaddingTop() +mMaxPaintWidth/2);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mUnReachedBarColor);
mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
canvas.drawCircle(mRadius,mRadius,mRadius,mPaint);
mPaint.setColor(mReachedBarColor);
mPaint.setStrokeWidth(mReachedProgressBarHeight);
float sweepAngle = getProgress() * 1.0f /getMax()* 360;
canvas.drawArc(new RectF(0,0,mRadius * 2,mRadius *2),0,sweepAngle,false,mPaint);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(text,mRadius -textWidth /2,mRadius - textHeight,mPaint);
canvas.restore();
}
}
- 继承了上一篇的HorizontlProgressar
- 重写onMeasure()
- 重写onDraw()
MainActivity
public class MainActivity extends AppCompatActivity {
private HorizontlProgressar progressbar;
private RoundnessProgressBar roundnessProgressBar;
private static final int MSG_PROGRESS_UPDATE = 0x110;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
int progress = progressbar.getProgress();
int roundProgress = roundnessProgressBar.getProgress();
progressbar.setProgress(progress);
roundnessProgressBar.setProgress(roundProgress);
progressbar.setProgress(++progress);
roundnessProgressBar.setProgress(++roundProgress);
if (progress >= 100) {
mHandler.removeMessages(MSG_PROGRESS_UPDATE);
}
mHandler.sendEmptyMessageDelayed(MSG_PROGRESS_UPDATE, 100);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar = findViewById(R.id.processbar);
roundnessProgressBar = findViewById(R.id.id_progress02);
mHandler.sendEmptyMessage(MSG_PROGRESS_UPDATE);
}
}
1.以上除了上一篇的内容,增加了圆形进度条;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.wangj.progressbar.RoundnessProgressBar
android:id="@+id/id_progress02"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@id/processbar"
android:layout_marginTop="50dip"
android:background="#44ff0000"
android:progress="30" />
</RelativeLayout>
<resources>
<declare-styleable name="RoundProgressBarWidthNumber">
<attr name="radius" format="dimension" />
</declare-styleable>
</resources>
- 上面的两个代码就是界面显示和属性设置
感谢
此方法依旧是鸿阳大神的自定义进度条;感谢大神;加油;