自定义view 圆柱

package com.example.cll.zidingyiview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Arrays;
import java.util.List;

/**
 * Created by cll on 2017/12/20.
 */

public class Zhuxing extends View{

    private String[] xData={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    private String[] yData={"100","200","300","400","500","600","700","800","900","1000"};
    private List list= Arrays.asList(300,200,460,120,730,920,80);
    private int margin=20;
    private int marginX=30;
    private int marginY=30;
    private int pointX;
    private int pointY;
    private int scaleX;
    private int scaleY;
    private Paint paintXY;
    private Paint paintRect;
    private Paint paintValue;
    public Zhuxing(Context context) {
        this(context,null);
    }

    public Zhuxing(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Zhuxing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void initView(String[] xData,String[] yData,List list){
        this.xData = xData;
        this.yData = yData;
        this.list = list;
    }
    private void init(){
        pointX=margin+marginX;
        pointY=getHeight()-(margin+marginY);
        scaleX=(getWidth()-2*margin-marginX)/xData.length;
        scaleY=(getHeight()-2*margin-marginY)/yData.length;

        paintXY=new Paint();
        paintXY.setStyle(Paint.Style.STROKE);
        paintXY.setAntiAlias(true);
        paintXY.setDither(true);
        paintXY.setColor(Color.BLACK);
        paintXY.setStrokeWidth(3);

        paintRect=new Paint();
        paintRect.setStyle(Paint.Style.STROKE);
        paintRect.setDither(true);
        paintRect.setAntiAlias(true);
        paintRect.setColor(Color.BLACK);
        paintRect.setTextSize(15);

        paintValue=new Paint();
        paintValue.setStyle(Paint.Style.FILL);
        paintValue.setDither(true);
        paintValue.setAntiAlias(true);
        paintValue.setStrokeWidth(1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        init();
        canvas.drawLine(pointX,pointY,getWidth()-margin,pointY,paintXY);
        canvas.drawLine(pointX,pointY,pointX,margin,paintXY);
        for (int i=1;i<xData.length;i++){
              int height=pointY-margin;
              float single = height / Integer.valueOf(yData[yData.length - 1]);
              int marginLeft = pointX + scaleX * i;
            // 单位
            canvas.drawText(xData[i-1],marginLeft - 53,pointY+25,paintValue);
            // 柱状图
            RectF rectF = new RectF(marginLeft - 50,height - (list.get(i-1) * single),marginLeft - 10,pointY);
            canvas.drawRect(rectF,paintRect);
            canvas.drawText(list.get(i-1)+"",marginLeft - 43,height - (list.get(i-1) * single) - 10,paintValue);
            canvas.drawText(xData[i-1],marginLeft-53,pointY+25,paintValue);
        }
        for (int i=1;i<yData.length;i++){
            int marginTop = scaleY * i;
            canvas.drawText(yData[i-1],pointX,pointY-marginTop,paintValue);
        }
    }
}

你可能感兴趣的:(自定义view 圆柱)