1.继承ImageView,重写onDraw()方法

2.在控件的中间位置画圆圈进度和进度文字

3.暴露设置进度值的方法,重绘

public class ProgressImageView extends AppCompatImageView {    private int progress = 0;//加载的进度    private int diameter = 100;//圆形进度条的直径    private Paint mPaint;    public ProgressImageView(Context context) {        super(context);        init();    }    public ProgressImageView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init();    }    public ProgressImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mPaint = new Paint();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (progress > 0 && progress < 360) {//如果图片加载完成,进度条不显示            /**             * 画进度圆圈,圆圈位置放在图片中间             */            mPaint.setAntiAlias(true);//取消锯齿            mPaint.setStrokeWidth(10);//画笔宽度            mPaint.setColor(Color.BLUE);//圆圈设置成蓝色            mPaint.setStyle(Paint.Style.STROKE);//空心            int ho = (getMeasuredWidth() - diameter) / 2;//水平            int ve = (getMeasuredHeight() - diameter) / 2;//垂直            RectF rectF = new RectF();            rectF.left = ho;            rectF.top = ve;            rectF.right = ho + diameter;            rectF.bottom = ve + diameter;            canvas.drawArc(rectF, -90, progress, false, mPaint);            /**             * 画进度文字显示,位置在图片中间向左编译30px,向下移动10px             */            mPaint.setColor(Color.RED);//文字设置成红色            mPaint.setStrokeWidth(1);//画笔宽度            mPaint.setTextSize(25);//字体大小            canvas.drawText(String.valueOf((int) (progress / 3.6)) + "%", getMeasuredWidth() / 2 - 30, getMeasuredHeight() / 2 + 10, mPaint);        }    }    /**     * 设置进度     *     * @param total    总的长度     * @param progress 已加载的进度     */    public void setProgress(int total, int progress) {        this.progress = progress * 360 / total;        postInvalidate();    }}


更多相关文章

  1. Android学习笔记(24):进度条组件ProgressBar及其子类
  2. Android(安卓)ListView 下拉刷新 上拉更多
  3. 【Android(安卓)- 框架】之刷新加载框架Ultra-Pull-To-Refresh的
  4. Android:控件WebView显示网页
  5. 重画Progressbar的进度
  6. android实现瀑布流加载图片
  7. Android(安卓)Framework中的PolicyManager简介
  8. Android图片加载之认识bitmap的四种加载方式
  9. 第一次运行Android(安卓)Studio

随机推荐

  1. sqlite3 表结构查询
  2. android获取屏幕大小
  3. Fresco使用的注意事项
  4. Android(安卓)ApiDemos 系列解析【View-I
  5. 关于安卓开发实现拖动条和星级评论
  6. android 瀑布流
  7. 腾讯代理下载Android SDK
  8. android反编译
  9. Android应用程序键盘(Keyboard)消息处理机
  10. Android ListView使用