一直在维护www.halloandroid.com我的个人博客网站,发现漏了一篇没发在上面,补回来。
上次写了一篇有关自定义折线图的文章,今天写一篇关于柱形图的文章,因为看了网上一位阿拉丁神灯大神的文章,所以自己也想动手写一个,熟练和学习一下自定义View。话不多说,开始撸!
整体的思路:
画柱状图 –>画竖线 –>画顶部横线 –>画文字
自定义View四步骤走起;
还是我们自定View的那几个步骤:
- 1、自定义View的属性
- 2、在View的构造方法中获得我们自定义的属性
- [ 3、重写onMesure ]
- [ 4、重写onLayout ]
- 5、重写onDraw
一,在attrs里面进行声明
<declare-styleable name="HistogramView">
<attr name="cylinderColor" format="color|reference"/><!--柱体颜色-->
<attr name="axesColor" format="color|reference"/><!--坐标线颜色-->
<attr name="textColor"/><!--文字颜色-->
<attr name="cylinderWith" format="integer"/><!--主题宽度-->
<attr name="maxSize" format="float"/><!--最大值-->
<attr name="textSize"/><!--文字字体大小-->
</declare-styleable>
二、在View的构造方法中获得我们自定义的属性
public HistogramView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HistogramView);
maxSize = a.getFloat(R.styleable.HistogramView_maxSize, 100f);
cylinderWidth = a.getInt(R.styleable.HistogramView_cylinderWith, 50);
textSize = a.getDimensionPixelSize(R.styleable.RoundProgressBar_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX<