android 自定义雷达chart


package cf.midea.workspace.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by chengfan on 2018-3-15.
 */

public class RadarChart extends View {
    private static String TAG="RadarChart";
    private static int CIRCLE_LINE_COLOR=0xffffffff;
    private static int LINE_COLOR=0xff979797;
    private static int TXT_COLOR=0x88ffffff;
    private static float BULGE_HEIGHT;
    private static int TXT_SIZE;
    private String[] labels=new String[]{"蓬松度","含水率","膨胀率","还原糖","香气","糊化度","弹性","软硬度","粘性"};
    private int[] vals=    new int[]{2,3,4,6,1,7,4,5,3};
    private int[] maxVals= new int[]{7,7,7,7,7,7,7,7,7};
    private PointF mSizeOfView=new PointF();
    private float mDensity;
    private float mBaseZero;
    public RadarChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mDensity=context.getResources().getDisplayMetrics().density;
        TXT_SIZE=(int)(22*mDensity);
    }

    /**
     * @param _labels
     * @param _vals
     * @param _maxVals
     */
    public void initView(String[] _labels,int[] _vals,int[] _maxVals){
        labels=_labels;
        vals=_vals;
        maxVals=_maxVals;
        postInvalidate();
    }
    /**
     * @param val
     * @param index
     */
    public void setValue(int val,int index){
        vals[index]=val;
        postInvalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(mSizeOfView.x/2,mSizeOfView.x/2);
        Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        float outRadius=drawCircles_first(canvas,paint);
        drawLines_second(canvas,paint,outRadius);
//        canvas.translate(-mSizeOfView.x/2,-mSizeOfView.x/2);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        if(widthMode==MeasureSpec.EXACTLY)
            mSizeOfView.x=MeasureSpec.getSize(widthMeasureSpec);
        if(heightMode==MeasureSpec.EXACTLY)
            mSizeOfView.y=MeasureSpec.getSize(heightMeasureSpec);
        BULGE_HEIGHT=  (mSizeOfView.x*0.037f);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * @param canvas
     * @param paint
     * @return
     */
    private float drawCircles_first(Canvas canvas, Paint paint){
        paint.setColor(CIRCLE_LINE_COLOR);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2*mDensity);
        float outRadius=0.55f*mSizeOfView.x/2;
        float gap=outRadius/4;
        mBaseZero=outRadius - gap * 3;
        canvas.drawCircle(0, 0, outRadius , paint);
        paint.setStrokeWidth(1*mDensity);
        for(int i=1;i<4;i++) {
            canvas.drawCircle(0, 0, outRadius - gap * i, paint);
        }
        return  outRadius;
    }
    private void drawLines_second(Canvas canvas,Paint paint,float outRadius){
        canvas.save();
        int legth=vals.length;
        float angle=360f/legth;
        paint.setColor(LINE_COLOR);
        paint.setTextSize(TXT_SIZE);
        float line_length=outRadius+BULGE_HEIGHT;
        for (int i=0;i<legth;i++)
        {
            if(i>0)
                canvas.rotate(angle,0,0);
            canvas.drawLine(0,-mBaseZero,0,-line_length,paint);
        }
        canvas.restore();

        float angle_total;
        float x ;
        float y ;
        Rect rect=new Rect();
        paint.setStyle(Paint.Style.FILL);
        for(int i=0;i<legth;i++)
        {
            angle_total=angle*i;
            x= (float) (line_length*Math.sin(Math.PI*angle_total/180));
            y= -(float) (line_length*Math.cos(Math.PI*angle_total/180));
            paint.getTextBounds(labels[i],0,labels[i].length(),rect);
            if(angle_total>=0&&angle_total<90)
                canvas.drawText(labels[i],x+2,y,paint);
            else if(angle_total>=90&&angle_total<180)
                canvas.drawText(labels[i],x+2,y+rect.height(),paint);
            else if(angle_total>=180&&angle_total<270)
                canvas.drawText(labels[i],x-rect.width()-2,y+rect.height(),paint);
            else
                canvas.drawText(labels[i],x-rect.width()-2,y,paint);
        }

//        paint.setColor(0x660000ff);
        float val_line_length=outRadius-mBaseZero;
        Path path=new Path();
        LinearGradient linearGradient=new LinearGradient(0,-outRadius,3,outRadius,0xee11F9C9 ,0xee5694F4, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
        x= (float) ((   (float)vals[0]/(float)maxVals[0]*val_line_length+mBaseZero  )*Math.sin(Math.PI*0/180));
        y= -(float) ((   (float)vals[0]/(float)maxVals[0]*val_line_length+mBaseZero  )*Math.cos(Math.PI*0/180));
        path.moveTo(x,y);
        for (int i=1;i<legth;i++){
            x= (float) ((   (float)vals[i]/(float)maxVals[i]*val_line_length+mBaseZero  )*Math.sin(Math.PI*angle*i/180));
            y= -(float) ((   (float)vals[i]/(float)maxVals[i]*val_line_length+mBaseZero  )*Math.cos(Math.PI*angle*i/180));
            path.lineTo(x,y);
        }
        path.close();
        canvas.drawPath(path,paint);
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无v邪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值