android自定义view---横向的柱状图

这里写图片描述
知识点:
1.计算基线
在基线的上面为负值,基线的下面为正值。
这里写图片描述
需要借助 Paint.FontMetricsInt fm = paint.getFontMetricsInt();
FontMetricsInt 类 有 top 、bottom 两个成员,
top表示基线到文字最上面的位置的距离 是个负值 bottom为基线到最下面的距离,为正值
如果想要基于一个位置竖直居中,那么居中的位置 坐标假设为 centerY
baseLineY = centerY - (fm.bottom-fm.top)/2- fm.top;

2.属性动画
使用ValueAnimator的属性动画,目的是进行获取百分比后进行重绘。

3.文字从右端向左绘制
mContentPaint.setTextAlign(Paint.Align.RIGHT);

4.使用drawLine()时候设置线的宽度
mLinePaint.setStrokeWidth(dp2px(0.8f));

5.获取文字的长度和高度
①仅仅获取文字的长度
mContentPaint.measureText(hoBarEntityList.get(i).content);
②能同时获取文字的长度和高度
Rect rect = new Rect();
mContentPaint.getTextBounds(hoBarEntityList.get(i).content, 0, hoBarEntityList.get(i).content.length(), rect);
contentTextWidth= rect.width();
contentTextHeight = rect.height();

6.获取屏幕宽高
private int getScreenWidth() {
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getWidth();
}

具体代码
1.测量
这里写图片描述
2.绘制
这里写图片描述
3.设置数据并启动动画
这里写图片描述

demo地址:https://github.com/yunzheyue/horizontalbar

猜你喜欢

转载自blog.csdn.net/yuezheyue123/article/details/82428108