Android开发Android开发经验谈Android知识

自定义UI学习---Paint画直方图

2017-07-17  本文已影响111人  zsgnaw

简单的直方图效果,属性什么的没有整理。。。

效果图:

效果图.png
package com.wangsz.uidemo.view;

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

import java.util.ArrayList;
import java.util.List;

/**
 * 直方图
 */
public class HistogramView extends View {

    private static final String TAG = "直方图";

    private Paint paint;                                   //画笔
    private List<DataModel> datas = new ArrayList<>();     //数据
    private int max = 0;                                   //最大值

    private int textSizeLeft = 30;                         //左侧字体大小
    private int textSizeBottom = 30;                       //底部字体大小
    private float lineSize = 3;                            //线的高度
    private float space = 20;                              //间距
    private float textPaddingLine = 10;                    //字与线之间的距离

    private float textBottomHeight;                        //底部字的高度
    private float textLeftWidth;                           //左边字的宽度
    private float eachWidth;                               //每个条目的宽度
    private int colorDefault = 0xFFB3EE3A;                 //条目默认颜色
    private float bottomLineToBottom;                      //底部线距离底部的高度
    private float maxRectHeight;                           //最大值的矩形高度

    private float leftTop = 40;                            //左边线条多余

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

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

    public HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        paint = new Paint();

    }


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

        if (!datas.isEmpty()) {
            drawText(canvas);
            drawLines(canvas);
            drawRect(canvas);
        }
    }

    /**
     * 设置数据
     * @param list
     */
    public void setDatas(List<DataModel> list){
        datas = list;
        dealwithData();
        invalidate();
    }

    /**
     * 画字
     * @param canvas
     */
    private void drawText(Canvas canvas) {

        paint.setTextSize(textSizeLeft);
        textLeftWidth = paint.measureText(String.valueOf(max));

        // 底部的字
        // 文字绘制学习http://blog.csdn.net/zly921112/article/details/50401976
        paint.setTextSize(textSizeBottom);
        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
        textBottomHeight = Math.abs(top) + Math.abs(bottom);//底部字的高度

        //每个条目的宽度
        eachWidth = (getWidth() - getPaddingLeft() - getPaddingRight() - textPaddingLine - lineSize - space * (datas.size() + 1) - textLeftWidth) / datas.size();

        float yb = getHeight() - getPaddingBottom() - Math.abs(bottom); // 底部字的y坐标

        for (int i = 0; i < datas.size();i++){
            DataModel dataModel = datas.get(i);
            float x = getPaddingLeft() + textPaddingLine + textLeftWidth + space + eachWidth / 2 + (eachWidth + space) * i;
            canvas.drawText(dataModel.name,x,yb,paint);
        }

        paint.setTextSize(textSizeLeft);
        paint.setTextAlign(Paint.Align.RIGHT);
        //底部线距离底部的高度
        bottomLineToBottom = getPaddingBottom() + textBottomHeight + textPaddingLine;
        //最大值的矩形高度
        maxRectHeight = getHeight() - bottomLineToBottom - getPaddingTop() - leftTop;

        for (int i = 0; i <= 5;i++){
            float yl = getHeight() - bottomLineToBottom - maxRectHeight * i / 5;
            canvas.drawText(String.valueOf(max * i / 5),getPaddingLeft() + textLeftWidth,yl,paint);
        }
    }

    /**
     * 画线条
     * @param canvas
     */
    private void drawLines(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(lineSize);
        float left = getPaddingLeft() + textLeftWidth + textPaddingLine;
        float bottom = getHeight() - bottomLineToBottom;

        canvas.drawLine(left, getPaddingTop(), left, bottom, paint);

        canvas.drawLine(left, bottom, getWidth() - getPaddingRight(), bottom, paint);
    }

    private void drawRect(Canvas canvas) {

        for (int i = 0; i < datas.size();i++){

            DataModel dataModel = datas.get(i);

            if (dataModel.colorRes == 0){
                paint.setColor(colorDefault);
            } else {
                paint.setColor(dataModel.colorRes);
            }

            float x = getPaddingLeft() + textLeftWidth + textPaddingLine + space + eachWidth / 2 + (eachWidth + space) * i;
            canvas.drawRect(
                    x - eachWidth / 2,
                    getHeight() - bottomLineToBottom - maxRectHeight * dataModel.num / max,
                    x + eachWidth / 2,
                    getHeight() - bottomLineToBottom,
                    paint);
        }

    }

    /**
     * 处理数据,获取最大值
     */
    private void dealwithData() {
        for (DataModel dataModel : datas){
            max = dataModel.num > max ? dataModel.num : max;
        }
    }

    /**
     * 数据
     */
    public static class DataModel{
        int num;
        String name;
        @ColorInt int colorRes;

        public DataModel(int num, String name) {
            this.num = num;
            this.name = name;
            this.colorRes = 0;
        }

        public DataModel(int num, String name,@ColorInt int colorRes) {
            this.num = num;
            this.name = name;
            this.colorRes = colorRes;
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读