439_画出折线图的背景

来源:互联网 发布:都叫兽数据恢复激活码 编辑:程序博客网 时间:2024/05/29 15:59




画出折线图的背景


public class LineChartBackgroundView extends View {


    private Context context;
    private int screenWidth;
    private double singleInterval;
    private Paint grayPaint;
    private int height;
    private int singleHeight;
    private double totalWidth;
    private double maxValue;
    private Paint paint;
    private double singleValue;
    private int dp12;
    private int dp10;
    private int dp9;
    private int dp6;
    private int dp3;
    private Paint bottomLinePaint;


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(screenWidth, height);
    }


    public LineChartBackgroundView(Context context) {
        this(context, null);
    }


    public LineChartBackgroundView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public LineChartBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        screenWidth = DensityUtil.getScreenWidth(context);
        height = DensityUtil.dip2px(context, 200);
        singleHeight = height / 7;


        grayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        grayPaint.setStrokeWidth(1);
        int[] colors = {getResources().getColor(R.color.color_single_white),
                getResources().getColor(R.color.color_single_DDDDDD),
                getResources().getColor(R.color.color_single_white),};
        LinearGradient linearGradient = new LinearGradient(0, 0, screenWidth, 0,
                colors, null, Shader.TileMode.CLAMP);
        grayPaint.setShader(linearGradient);


        dp12 = DensityUtil.dip2px(context, 12);
        dp10 = DensityUtil.dip2px(context, 10);
        dp9 = DensityUtil.dip2px(context, 9);
        dp6 = DensityUtil.dip2px(context, 6);
        dp3 = DensityUtil.dip2px(context, 3);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(getResources().getColor(R.color.color_single_999999));
        paint.setTextSize(dp12);


        bottomLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bottomLinePaint.setColor(getResources().getColor(R.color.color_single_DDDDDD));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        paint.setTextSize(dp10);
        canvas.drawText("收益(元)", dp3, dp12, paint);
        paint.setTextSize(dp12);


        for (int i = 0; i < 7; i++) {
            if (i > 0 && i < 6) {
                canvas.drawLine(0, i * singleHeight + 1, screenWidth, i * singleHeight + 1, grayPaint);


                canvas.drawText(DoubleUtil.getTwoDecimalPlacesString((6 - i) * singleValue), dp3, i * singleHeight + dp6, paint);
            }


            if (i == 6) {
                canvas.drawLine(0, i * singleHeight + 1, screenWidth, i * singleHeight + 1, bottomLinePaint);
            }
        }
    }


    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
        singleValue = maxValue / 5;
    }
}



0 0
原创粉丝点击