2019独角兽企业重金招聘Python工程师标准>>>

<lib.view.progressbar.ColorArcProgressBar
    android:layout_width="match_parent"
    android:layout_height="220dip"
    android:id="@+id/barInterest"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    app:back_color="@android:color/darker_gray"
    app:back_width="8dp"
    app:current_value="66"
    app:front_color2="@color/common_gray_fa"
    app:front_color1="@color/common_gray_fa"
    app:front_color3="@color/common_orange_m"
    app:front_width="8dp"
    app:is_need_content="true"
    app:is_need_title="true"
    app:is_need_unit="true"
    app:max_value="100"
    app:string_title="年化收益(%)"
    app:string_unit=""/>
package lib.view.progressbar;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.lsy.hebao.R;

import lib.util.MathUtil;
public class ColorArcProgressBar extends View {private int diameter = 500;  //直径
    private float centerX;  //圆心X坐标
    private float centerY;  //圆心Y坐标

    private Paint allArcPaint;
    private Paint progressPaint;
    private Paint vTextPaint;
    private Paint hintPaint;
    private Paint degreePaint;
    private Paint curSpeedPaint;

    private RectF bgRect;

    private ValueAnimator progressAnimator;
    private PaintFlagsDrawFilter mDrawFilter;

    private float startAngle = 150;
    private float sweepAngle = 240;
    private float currentAngle = 0;
    private float lastAngle;
    private float maxValues = 60;
    private float curValues = 0;
    private float bgArcWidth = dipToPx(2);
    private float progressWidth = dipToPx(10);
    private float textSize = dipToPx(60);
    private float hintSize = dipToPx(15);
    private float curSpeedSize = dipToPx(13);
    private int aniSpeed = 1000;
    private float longdegree = dipToPx(13);
    private float shortdegree = dipToPx(5);
    private final int DEGREE_PROGRESS_DISTANCE = dipToPx(18);

    private String longDegreeColor = "#111111";
    private String shortDegreeColor = "#111111";
    private String bgArcColor = "#88F0f0f0";
    private String currentArcColor = "#FFB700";
    private String titleString;
    private String hintString;

    private boolean isNeedTitle;
    private boolean isNeedUnit;
    private boolean isNeedDial;
    private boolean isNeedContent;

    // sweepAngle / maxValues 的值
    private float k;
    private int color1,color2,color3;

    public ColorArcProgressBar(Context context) {super(context, null);
        initView();
    }public ColorArcProgressBar(Context context, AttributeSet attrs) {super(context, attrs, 0);
        initCofig(context, attrs);
        initView();
    }public ColorArcProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
        initCofig(context, attrs);
        initView();
    }/**
     * 初始化布局配置
     * @param context
     * @param attrs
     */
    private void initCofig(Context context, AttributeSet attrs) {TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorArcProgressBar);
        color1 = a.getColor(R.styleable.ColorArcProgressBar_front_color1, Color.GREEN);
        color2 = a.getColor(R.styleable.ColorArcProgressBar_front_color2, color1);
        color3 = a.getColor(R.styleable.ColorArcProgressBar_front_color3, color1);

        sweepAngle = a.getInteger(R.styleable.ColorArcProgressBar_total_engle, 240);
        bgArcWidth = a.getDimension(R.styleable.ColorArcProgressBar_back_width, dipToPx(2));
        progressWidth = a.getDimension(R.styleable.ColorArcProgressBar_front_width, dipToPx(10));
        isNeedTitle = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_title, false);
        isNeedContent = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_content, false);
        isNeedUnit = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_unit, false);
        isNeedDial = a.getBoolean(R.styleable.ColorArcProgressBar_is_need_dial, false);
        hintString = a.getString(R.styleable.ColorArcProgressBar_string_unit);
        titleString = a.getString(R.styleable.ColorArcProgressBar_string_title);
        curValues = a.getFloat(R.styleable.ColorArcProgressBar_current_value, 0);
        maxValues = a.getFloat(R.styleable.ColorArcProgressBar_max_value, 60);
        setCurrentValues(curValues);
        setMaxValues(maxValues);
        a.recycle();

    }@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = (int) (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE);
        int height= (int) (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE);
        setMeasuredDimension(width, height);
    }private void initView() {diameter = 3 * getScreenWidth() / 5;
        //弧形的矩阵区域
        bgRect = new RectF();
        bgRect.top = longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE;
        bgRect.left = longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE;
        bgRect.right = diameter + (longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE);
        bgRect.bottom = diameter + (longdegree + progressWidth/2 + DEGREE_PROGRESS_DISTANCE);

        //圆心
        centerX = (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE)/2;
        centerY = (2 * longdegree + progressWidth + diameter + 2 * DEGREE_PROGRESS_DISTANCE)/2;

        //外部刻度线
        degreePaint = new Paint();
        degreePaint.setColor(Color.parseColor(longDegreeColor));

        //整个弧形
        allArcPaint = new Paint();
        allArcPaint.setAntiAlias(true);
        allArcPaint.setStyle(Paint.Style.STROKE);
        allArcPaint.setStrokeWidth(bgArcWidth);
        allArcPaint.setColor(Color.parseColor(bgArcColor));
        allArcPaint.setStrokeCap(Paint.Cap.ROUND);

        //当前进度的弧形
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStrokeWidth(progressWidth);
        progressPaint.setColor(Color.parseColor(currentArcColor));

        //内容显示文字
        vTextPaint = new Paint();
        vTextPaint.setTextSize(textSize);
        vTextPaint.setColor(color2);
        vTextPaint.setTextAlign(Paint.Align.CENTER);

        //显示单位文字
        hintPaint = new Paint();
        hintPaint.setTextSize(hintSize);
        hintPaint.setColor(color3);
        hintPaint.setTextAlign(Paint.Align.CENTER);

        //显示标题文字
        curSpeedPaint = new Paint();
        curSpeedPaint.setTextSize(curSpeedSize);
        curSpeedPaint.setColor(color1);
        curSpeedPaint.setTextAlign(Paint.Align.CENTER);

        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }@Override
    protected void onDraw(Canvas canvas) {//抗锯齿
        canvas.setDrawFilter(mDrawFilter);

        if (isNeedDial) {//画刻度线
            for (int i = 0; i < 40; i++) {if (i > 15 && i < 25) {canvas.rotate(9, centerX, centerY);
                    continue;
                }if (i % 5 == 0) {degreePaint.setStrokeWidth(dipToPx(2));
                    degreePaint.setColor(Color.parseColor(longDegreeColor));
                    canvas.drawLine(centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE,
                            centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - longdegree, degreePaint);
                } else {degreePaint.setStrokeWidth(dipToPx(1.4f));
                    degreePaint.setColor(Color.parseColor(shortDegreeColor));
                    canvas.drawLine(centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - (longdegree - shortdegree) / 2,
                            centerX, centerY - diameter / 2 - progressWidth / 2 - DEGREE_PROGRESS_DISTANCE - (longdegree - shortdegree) / 2 - shortdegree, degreePaint);
                }canvas.rotate(9, centerX, centerY);
            }}//整个弧
        canvas.drawArc(bgRect, startAngle, sweepAngle, false, allArcPaint);
        //当前进度
        canvas.drawArc(bgRect, startAngle, currentAngle, false, progressPaint);

        if (isNeedContent) {canvas.drawText(MathUtil.mathTodecimaltwo(curValues/maxValues*10.88), centerX, centerY ,
                    vTextPaint);
        }if (isNeedUnit) {canvas.drawText(hintString, centerX, centerY+textSize/2, hintPaint);
        }if (isNeedTitle) {canvas.drawText(titleString, centerX, centerY -  textSize , curSpeedPaint);
        }invalidate();

    }/**
     * 设置最大值
     * @param maxValues
     */
    public void setMaxValues(float maxValues) {this.maxValues = maxValues;
        k = sweepAngle/maxValues;
    }/**
     * 设置当前值
     * @param currentValues
     */
    public void setCurrentValues(float currentValues) {if (currentValues > maxValues) {currentValues = maxValues;
        }if (currentValues < 0) {currentValues = 0;
        }this.curValues = currentValues;
        lastAngle = currentAngle;
        setAnimation(lastAngle, currentValues * k, aniSpeed);
    }/**
     * 设置整个圆弧宽度
     * @param bgArcWidth
     */
    public void setBgArcWidth(int bgArcWidth) {this.bgArcWidth = bgArcWidth;
    }/**
     * 设置进度宽度
     * @param progressWidth
     */
    public void setProgressWidth(int progressWidth) {this.progressWidth = progressWidth;
    }/**
     * 设置速度文字大小
     * @param textSize
     */
    public void setTextSize(int textSize) {this.textSize = textSize;
    }/**
     * 设置单位文字大小
     * @param hintSize
     */
    public void setHintSize(int hintSize) {this.hintSize = hintSize;
    }/**
     * 设置单位文字
     * @param hintString
     */
    public void setUnit(String hintString) {this.hintString = hintString;
        invalidate();
    }/**
     * 设置直径大小
     * @param diameter
     */
    public void setDiameter(int diameter) {this.diameter = dipToPx(diameter);
    }/**
     * 设置标题
     * @param title
     */
    private void setTitle(String title){this.titleString = title;
    }/**
     * 设置是否显示标题
     * @param isNeedTitle
     */
    private void setIsNeedTitle(boolean isNeedTitle) {this.isNeedTitle = isNeedTitle;
    }/**
     * 设置是否显示单位文字
     * @param isNeedUnit
     */
    private void setIsNeedUnit(boolean isNeedUnit) {this.isNeedUnit = isNeedUnit;
    }/**
     * 设置是否显示外部刻度盘
     * @param isNeedDial
     */
    private void setIsNeedDial(boolean isNeedDial) {this.isNeedDial = isNeedDial;
    }/**
     * 为进度设置动画
     * @param last
     * @param current
     */
    private void setAnimation(float last, float current, int length) {progressAnimator = ValueAnimator.ofFloat(last, current);
        progressAnimator.setDuration(length);
        progressAnimator.setTarget(currentAngle);
        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Override
            public void onAnimationUpdate(ValueAnimator animation) {currentAngle= (float) animation.getAnimatedValue();
                curValues = currentAngle/k;
            }});
        progressAnimator.start();
    }/**
     * dip 转换成px
     * @param dip
     * @return
     */
    private int dipToPx(float dip) {float density = getContext().getResources().getDisplayMetrics().density;
        return (int)(dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }/**
     * 得到屏幕宽度
     * @return
     */
    private int getScreenWidth() {WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }
}

在mainActivity里面

private ColorArcProgressBar bar2;
bar2= (ColorArcProgressBar) view.findViewById(R.id.barInterest);
bar2.setCurrentValues(40);

就这样完成了 哈哈哈!!!!

转载于:https://my.oschina.net/u/3407708/blog/887914

自定义ProgressBar(圆)相关推荐

  1. Android 自定义ProgressBar 实现进度圆环

    实现的效果如下图 实现效果图demo 的地址 代码很简单自定义ProgressBar 下面直接列举下代码 progressBarView 的代码如下 public class ProgressBarV ...

  2. android 自定义progressbar demo,Android自定义View――动态ProgressBar之模仿360加速球

    在之前一篇文章中我们讲解了三种ProgressBar的做法,详见-><Android 自定义View--自定义ProgressBar >.这一节中我们模仿360加速球制作一个动态Pr ...

  3. 自定义ProgressBar(自定义View和ClipDrawable)

    开发中经常需要自定义ProgressBar,这里用了自定义View和ClipDrawable实现简单的ProgressBar 自定义View效果: public class CustomProgres ...

  4. android自定义progressbar样式,Android开发中如何实现自定义ProgressBar的样式

    Android开发中如何实现自定义ProgressBar的样式 发布时间:2020-11-20 16:08:10 来源:亿速云 阅读:294 作者:Leah Android开发中如何实现自定义Prog ...

  5. android 自定义背景园,Android 自定义ProgressBar 进度条颜色和背景颜色

    Android 自定义ProgressBar 进度条颜色和背景颜色 首先,在drawable目录下新建文件 personal_center_level_progress_bg.xmlandroid a ...

  6. android 自定义进度条_第一百八十九回:Android中自定义ProgressBar三

    各位看官们大家好,上一回中咱们说的是Android中自定义ProgressBar的例子,这一回咱们继续说该例子.闲话休提,言归正转.让我们一起Talk Android吧! 看官们,我们在上一回是通过自 ...

  7. 自定义android进度条渐变,自定义ProgressBar简单完成颜色渐变功能进度条

    我们在使用电脑或者手机时,经常会遇到进度条,比如下图: 今天我来演示一下,如何做出简单并且漂亮的颜色渐变进度条. 首先我先新建了一个系统默认样式的进度条,代码如下: 运行后显示如下: 大家可以看出,并 ...

  8. android自定义progressbar 图片,自定义ProgressBar(自定义View和ClipDrawable)

    开发中经常需要自定义ProgressBar,这里用了自定义View和ClipDrawable实现简单的ProgressBar 自定义View效果: public class CustomProgres ...

  9. 自定义ProgressBar(包括自定义图片,带进度的圆形进度条、长方形进度条)

    转载请注明原博客地址:http://blog.csdn.net/gdutxiaoxu/article/details/51545889 参考博客:http://blog.csdn.net/lmj623 ...

最新文章

  1. IMLS:用于3D重构的深层隐式移动最小二乘函数(CVPR2021)
  2. 文件系统管理 之 Linux 创建文件系统及挂载文件系统流程详解
  3. docker in all
  4. iPhone 12 Pro系列终于不怕弯了!
  5. linux卸载tar安装的erlang包,linux - 从tar安装erlang导致错误,想知道如何指定文件夹 - 堆栈内存溢出...
  6. 覆盖索引与联合索引_Mysql性能优化:为什么要用覆盖索引?
  7. 10.企业安全建设入门(基于开源软件打造企业网络安全) --- 数据库安全
  8. mini计算机结构,简单拆机看内部构造_苹果 Mac mini MGEN2CH/A_台式电脑评测-中关村在线...
  9. python修改ppt的字体和颜色_ppt-页面大小和颜色更改
  10. 联想Lephone与Apple iPAD的完美组合
  11. Android内置系统apk问题
  12. 引领西装潮流文化的报喜鸟何以构建大国品牌
  13. 张勋说:棒磨机钢棒直径的配置(热处理调质耐磨钢棒)
  14. android 防止反编译的若干方法
  15. 数据结构——基于顺序存储结构的图书信息表的创建和输出
  16. AVKiller病毒的清除
  17. php 制作生成海报 图片合成 文字合成 上传到OSS
  18. torch各个版本镜像_Anaconda安装Pytorch镜像详细教程
  19. 常驻内存(Redis) ,界哥说 redis里面保存了很多的用户手机,万一宕机了呢
  20. https不能访问时的解决方案

热门文章

  1. 在移动端项目中使用vconsole
  2. php获取当前时间戳方法
  3. 【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】
  4. Android事件总线
  5. 求解:nhibernate2.0操作oralce提交事务时报错
  6. DNN 汉化中的问题????
  7. halcon 单通道图像转成3通道_halcon图像处理基本运算
  8. ARM汇编基础详解(PS学习汇编的原因)
  9. 智能家居 (3) ——智能家居工厂模式介绍实现继电器控制灯
  10. java获取response数据_Java中实现Http请求并获取响应数据