自定义Android扇形的加载ProgressBar

扇形的加载ProgressBar 之前在其他大神的文章中用看过自定义的空间,现在有时间自己可以尝试一下了,这是自定义的圆形的加载view
在valus中创建,自定义yuan_attrs.xml


下面的是自定义控件
public class YuanProgressbar extends AppCompatTextView {private Paint paint_text; private Paint paint_yuan; private Paint paint_arc; private RectF rectfArc; private int text_color; //文字的颜色 private int yuan_color; //背景的颜色 private String text; //文字 private int mTitleTextSize; private Rect mBound; float ratio = 0.00f; //比例初始值 /** * 当前进度 */ private int progress; /** * 最大进度 */ float maxValue; /** * 当前进度 */ float currentValue; private Context context; public YuanProgressbar(Context context) { this(context, null); this.context = context; }public YuanProgressbar(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }public YuanProgressbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; init(context, attrs, defStyleAttr); }private void init(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.YuanView, defStyleAttr, 0); text_color = ta.getColor(R.styleable.YuanView_text_color, Color.RED); yuan_color = ta.getColor(R.styleable.YuanView_yuan_color, Color.BLACK); text = ta.getString(R.styleable.YuanView_text); for (int n = 0; n < ta.getIndexCount(); n++) { int attr = ta.getIndex(n); if (attr == R.styleable.YuanView_text_size) { mTitleTextSize = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); }}ta.recycle(); //初始化画笔 paint_yuan = new Paint(); paint_yuan.setColor(yuan_color); paint_yuan.setStyle(Paint.Style.FILL); //设为实心paint_text = new Paint(); paint_text.setColor(text_color); paint_text.setTextSize(mTitleTextSize); paint_text.setStyle(Paint.Style.FILL); paint_arc = new Paint(); paint_arc.setColor(Color.RED); paint_arc.setStrokeWidth(2); paint_arc.setStyle(Paint.Style.STROKE); //设为空心}@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); printYuan(canvas); //画圆准备1/4扇形 printArc(canvas); //圆弧 printText(canvas); //写字}private void printArc(Canvas canvas) { int center = getWidth() / 2; int ringWidth = dip2px(context, 5); //设置圆环宽度 int radius = (center - ringWidth / 2); //圆环的半径 this.paint_arc.setStrokeWidth(ringWidth); paint_arc.setAntiAlias(true); //消除锯齿 canvas.drawCircle(center, center, radius, this.paint_arc); }@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); }private void printText(Canvas canvas) { canvas.save(); paint_text.getTextBounds(text, 0, text.length(), mBound); canvas.drawText(text, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, paint_text); canvas.restore(); }private void printYuan(Canvas canvas) { RectF rectF = new RectF(getLeft(), getTop(), getRight(), getBottom()); //canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2, paint_yuan); //中心圆点 canvas.drawArc(rectF, 180, ratio * 360, true, paint_yuan); //设置为180是为了从左边开始画图 }public void setText(String text) { this.text = text; invalidate(); }/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public float getMaxValue() { return maxValue; }public float getCurrentValue() { return currentValue; }public synchronized void setMaxValue(float maxValue) { if (maxValue < 0) { throw new IllegalArgumentException("max not less than 0"); } this.maxValue = https://www.it610.com/article/maxValue; }/** * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 * 刷新界面调用postInvalidate()能在非UI线程刷新 * * @param currentValue */ public synchronized void setProgress(float currentValue) { if (currentValue < 0) { throw new IllegalArgumentException("progress not less than 0"); } if (currentValue > maxValue) { currentValue = https://www.it610.com/article/maxValue; } if (currentValue <= maxValue) { this.currentValue = currentValue; DecimalFormat df = new DecimalFormat("#0.00"); ratio = Float.valueOf(df.format(currentValue / maxValue)); postInvalidate(); }}/** * 获取进度.需要同步 * * @return */ public synchronized float getProgress() { return currentValue; }}

【自定义Android扇形的加载ProgressBar】以下是在activity.xml中搭建布局,因为有自定义的属性需要在xml中添加
xmlns:yuan=”http://schemas.android.com/apk/res-auto”

接下来是在MainActivity中实现了
public class MainActivity extends AppCompatActivity {private YuanProgressbar yuanView; /* public static void launch(Context ctx) { Intent it = new Intent(ctx, PanOneActivity.class); ctx.startActivity(it); }*/@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); initViews(); }private void initViews() {yuanView = (YuanProgressbar) findViewById(R.id.yuan); yuanView.setText("0000"); yuanView.setMaxValue(300); yuanView.setProgress(100); } }

    推荐阅读