Android 绘制饼图

抛物线大神绘制相关的文章https://hencoder.com/ui-1-1/
先放效果图:

Android 绘制饼图_第1张图片
未标题-2.gif

绘制思路:
绘制饼图,考虑到各饼块之间有间隔,先定义基准圆心 , 圆半径,普通饼图偏移量以及点击状态饼图偏移量。

private static final float COMMONPIARCSPACE = 2;//普通饼图偏移量
private static final float PUSHOUTPIARCSPACE = 15;//点击状态饼图偏移量
private static final Point point = new Point(300,300);//圆心
private static final float circleR = 200;//圆半径

饼图圆心的位置与饼图的中线的夹角相关,举个例子:


Android 绘制饼图_第2张图片
image.png

当饼图弧度为 PI/2时,中线夹角为 PI/4,圆心从O(0,0)移动4个偏移量的位置为O(2,2),即圆心分别在X轴及Y轴移动2个位置。上代码:

    /**
 * 绘制每个百分比饼图
 * @param canvas
 * @param startDegree 圆弧开始的角度
 * @param degree 圆弧划过的角度
 * @param color 饼图颜色
 * @param drawDeltaValue 饼图与基准圆心的偏移量
 */
private void commonDrawPi(Canvas canvas,double startDegree,float degree,String color,float drawDeltaValue){
    double averageDegree = startDegree + degree/2;
    double deltaX = Math.cos(Math.toRadians(averageDegree));
    double deltaY = Math.sin(Math.toRadians(averageDegree));

    BigDecimal bigDeltaX = new BigDecimal(String.valueOf(deltaX)).setScale(16, BigDecimal.ROUND_HALF_UP);
    BigDecimal bigDeltaY = new BigDecimal(String.valueOf(deltaY)).setScale(16, BigDecimal.ROUND_HALF_UP);

    float changeX = bigDeltaX.floatValue() * drawDeltaValue;
    float changeY = bigDeltaY.floatValue() * drawDeltaValue;

    RectF rectF = new RectF(point.x - circleR + changeX
            ,point.y - circleR + changeY
            ,point.x + circleR + changeX
            ,point.y + circleR + changeY);
    paint.setColor(Color.parseColor(color));
    canvas.drawArc(rectF,BigDecimal.valueOf(startDegree).floatValue(),degree,true,paint);
}

判断点击事件所在饼图位置。
判断点击位置,只需要判断点击位置与基准圆心的夹角 以及 点击位置与基准圆心间距离 即可。

private ListpercentValueList = Arrays.asList(12.9,15.0,18.0,16.0,38.1);//饼图所占百分比

/**
 * 计算点击位置
 * @param event
 * @return
 */
private int caculateShowPie(MotionEvent event){
    float pointX = event.getX();
    float pointY = event.getY();

    float deltaX = pointX - point.x;
    float deltaY = pointY - point.y;

    double deltaXPow = Math.pow(BigDecimal.valueOf(deltaX).doubleValue(),2);
    double deltaYPow = Math.pow(BigDecimal.valueOf(deltaY).doubleValue(),2);
    double rPow = deltaXPow + deltaYPow;

    if (rPow < Math.pow(COMMONPIARCSPACE,2) || rPow > Math.pow(circleR + COMMONPIARCSPACE,2)){
        return PINOTSELECTED;
    }
    
    double sinA = deltaY/Math.sqrt(rPow); ;

    double positionDegree = Math.toDegrees(Math.asin(sinA));
    double scrennDegree = turnDegreeToScreenNormal(deltaX,deltaY,positionDegree);
    float degree = 0;
    double startDegree = 0;

    for (int i = 0;i < percentValueList.size(); i++) {
        Double percent = percentValueList.get(i);
        degree = (float) (percent * CIRCLETOTALDEGREE / 100);

        if (scrennDegree > startDegree && scrennDegree < startDegree + degree){
            return i;
        }
        startDegree += degree;
    }
    return PINOTSELECTED;
}

因为绘制饼图角度为顺时针方向,而反正弦得到的角度范围 为[-PI/2,PI/2]。因此需要进行转换。

/**
 * 由于是采用的顺时针绘制饼图,所以需要将角度调整
 * 计算角度所属象限,返回正确角度
 * @param deltaX
 * @param deltaY
 * @param degree
 * @return
 */
private double turnDegreeToScreenNormal(float deltaX,float deltaY,double degree){
    if (deltaX > 0 && deltaY < 0){
        //第一象限
        return 360 + degree;
    }else if (deltaX < 0 && deltaY < 0){
        //第二象限
        return 180 - degree;
    }else if (deltaX < 0 && deltaY > 0){
        //第三象限
        return 90 + degree;
    }else{
        return degree;
    }
}

点击饼图动画,通过ValueAnimator改变偏移量达到动画效果。

ValueAnimator mValueAnimator;//点击动画
float mAnimatedValue = 0f;//动画瞬时值
float mTotalAnimateValue = 0f;//动画总值

public void startAnim() {
    stopAnim();
    startAnimation(0f, 1f, ANIMATIONTIME);
}

public void stopAnim() {
    if (mValueAnimator != null) {
        clearAnimation();
        mAnimatedValue = 0f;
        mValueAnimator.end();
    }
}

private ValueAnimator startAnimation(float startF, final float endF, long time){
    mValueAnimator = ValueAnimator.ofFloat(startF, endF);
    mTotalAnimateValue = endF - startF;
    mValueAnimator.setDuration(time);
    mValueAnimator.setInterpolator(new LinearInterpolator());
    mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            mAnimatedValue = (float) valueAnimator.getAnimatedValue();
            postInvalidate();
        }
    });

    if (!mValueAnimator.isRunning()) {
        mValueAnimator.start();

    }
    return mValueAnimator;
}

最后贴上完整代码:

public class DrawPieCircleView  extends View{
String[] mColors = {"#FF1EBC71","#FFF55253","#FFDAA520"
        ,"#FF90EE90","#FB4E09","#AA49D2CC","#00A0E9"};

private static final int ANIMATIONTIME = 300;
private static final float CIRCLETOTALDEGREE = 360.0f;

private static final float COMMONPIARCSPACE = 2;//普通饼图偏移量
private static final float PUSHOUTPIARCSPACE = 15;//点击状态饼图偏移量

private static final Point point = new Point(300,300);//圆心
private static final float circleR = 200;//圆半径

ValueAnimator mValueAnimator;//点击动画
float mAnimatedValue = 0f;//动画瞬时值
float mTotalAnimateValue = 0f;//动画总值

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

public static final int PINOTSELECTED = -1;
private int piSelectedPosition = PINOTSELECTED;//当前点击的饼图
private int prePiSelectedPosition = PINOTSELECTED;//上一个点击的饼图
private boolean hasPieNeedPushOutShow = false;//是否有点击状态的饼图需要展示

private ListpercentValueList = Arrays.asList(12.9,15.0,18.0,16.0,38.1);//饼图所占百分比

public void setPercentValueList(List percentValueList) {
    this.percentValueList = percentValueList;
    postInvalidate();
}

public DrawPieCircleView(Context context){
    super(context);
}

public DrawPieCircleView(Context context, AttributeSet attrs){
    super(context, attrs);
}

public DrawPieCircleView(Context context,AttributeSet attrs,int defStyleAttr){
    super(context, attrs,defStyleAttr);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN){
        int nowSelectedPi  = caculateShowPie(event);
        if (nowSelectedPi == PINOTSELECTED) return super.onTouchEvent(event);
        if (hasPieNeedPushOutShow){
            if (piSelectedPosition == nowSelectedPi){
                hasPieNeedPushOutShow = false;
                prePiSelectedPosition = piSelectedPosition;
                piSelectedPosition = PINOTSELECTED;
            }else{
                prePiSelectedPosition = piSelectedPosition;
                piSelectedPosition = nowSelectedPi;
            }
        }else{
            hasPieNeedPushOutShow = true;
            prePiSelectedPosition = PINOTSELECTED;
            piSelectedPosition = nowSelectedPi;
        }
        startAnim();
        return true;
    }

    return super.onTouchEvent(event);
}

/**
 * 计算点击位置
 * @param event
 * @return
 */
private int caculateShowPie(MotionEvent event){
    float pointX = event.getX();
    float pointY = event.getY();

    float deltaX = pointX - point.x;
    float deltaY = pointY - point.y;

    double deltaXPow = Math.pow(BigDecimal.valueOf(deltaX).doubleValue(),2);
    double deltaYPow = Math.pow(BigDecimal.valueOf(deltaY).doubleValue(),2);
    double rPow = deltaXPow + deltaYPow;

    if (rPow < Math.pow(COMMONPIARCSPACE,2) || rPow > Math.pow(circleR + COMMONPIARCSPACE,2)){
        return PINOTSELECTED;
    }

    double sinA = deltaY/Math.sqrt(rPow); ;

    double positionDegree = Math.toDegrees(Math.asin(sinA));
    double scrennDegree = turnDegreeToScreenNormal(deltaX,deltaY,positionDegree);
    float degree = 0;
    double startDegree = 0;

    for (int i = 0;i < percentValueList.size(); i++) {
        Double percent = percentValueList.get(i);
        degree = (float) (percent * CIRCLETOTALDEGREE / 100);

        if (scrennDegree > startDegree && scrennDegree < startDegree + degree){
            return i;
        }
        startDegree += degree;
    }
    return PINOTSELECTED;
}

/**
 * 由于是采用的顺时针绘制饼图,所以需要将角度调整
 * 计算角度所属象限,返回正确角度
 * @param deltaX
 * @param deltaY
 * @param degree
 * @return
 */
private double turnDegreeToScreenNormal(float deltaX,float deltaY,double degree){
    if (deltaX > 0 && deltaY < 0){
        //第一象限
        return 360 + degree;
    }else if (deltaX < 0 && deltaY < 0){
        //第二象限
        return 180 - degree;
    }else if (deltaX < 0 && deltaY > 0){
        //第三象限
        return 90 + degree;
    }else{
        return degree;
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float degree = 0;
    double startDegree = 0;
    double totalDegre = CIRCLETOTALDEGREE;

    for (int i = 0;i < percentValueList.size(); i++){
        Double percent = percentValueList.get(i);
        degree = (float)(percent*totalDegre/100);
        float animatDistance = 0;
        if ( i != prePiSelectedPosition  && i != piSelectedPosition){
            animatDistance = COMMONPIARCSPACE;
        }else{
            if (i == prePiSelectedPosition){
                animatDistance = (mTotalAnimateValue - mAnimatedValue)/mTotalAnimateValue * (PUSHOUTPIARCSPACE - COMMONPIARCSPACE) + COMMONPIARCSPACE;
            }else{
                animatDistance = mAnimatedValue/mTotalAnimateValue * (PUSHOUTPIARCSPACE - COMMONPIARCSPACE) + COMMONPIARCSPACE;
            }
        }

        commonDrawPi(canvas,startDegree,degree,mColors[i],animatDistance);
        startDegree += degree;
    }

    if (mAnimatedValue == 1f){
        if (hasPieNeedPushOutShow) {
            prePiSelectedPosition = PINOTSELECTED;
        } else {
            prePiSelectedPosition = PINOTSELECTED;
            piSelectedPosition = PINOTSELECTED;
        }
    }
}

/**
 * 绘制每个百分比饼图
 * @param canvas
 * @param startDegree 圆弧开始的角度
 * @param degree 圆弧划过的角度
 * @param color 饼图颜色
 * @param drawDeltaValue 饼图与基准圆心的偏移量
 */
private void commonDrawPi(Canvas canvas,double startDegree,float degree,String color,float drawDeltaValue){
    double averageDegree = startDegree + degree/2;
    double deltaX = Math.cos(Math.toRadians(averageDegree));
    double deltaY = Math.sin(Math.toRadians(averageDegree));

    BigDecimal bigDeltaX = new BigDecimal(String.valueOf(deltaX)).setScale(16, BigDecimal.ROUND_HALF_UP);
    BigDecimal bigDeltaY = new BigDecimal(String.valueOf(deltaY)).setScale(16, BigDecimal.ROUND_HALF_UP);

    float changeX = bigDeltaX.floatValue() * drawDeltaValue;
    float changeY = bigDeltaY.floatValue() * drawDeltaValue;

    RectF rectF = new RectF(point.x - circleR + changeX
            ,point.y - circleR + changeY
            ,point.x + circleR + changeX
            ,point.y + circleR + changeY);
    paint.setColor(Color.parseColor(color));
    canvas.drawArc(rectF,BigDecimal.valueOf(startDegree).floatValue(),degree,true,paint);
}

public void startAnim() {
    stopAnim();
    startAnimation(0f, 1f, ANIMATIONTIME);
}

public void stopAnim() {
    if (mValueAnimator != null) {
        clearAnimation();
        mAnimatedValue = 0f;
        mValueAnimator.end();
    }
}

private ValueAnimator startAnimation(float startF, final float endF, long time){
    mValueAnimator = ValueAnimator.ofFloat(startF, endF);
    mTotalAnimateValue = endF - startF;
    mValueAnimator.setDuration(time);
    mValueAnimator.setInterpolator(new LinearInterpolator());
    mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            mAnimatedValue = (float) valueAnimator.getAnimatedValue();
            postInvalidate();
        }
    });

    if (!mValueAnimator.isRunning()) {
        mValueAnimator.start();

    }
    return mValueAnimator;
  }
}

你可能感兴趣的:(Android 绘制饼图)