importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.support.annotation.Nullable;
importandroid.util.AttributeSet;
importandroid.view.View;
importjava.util.Iterator;
importjava.util.LinkedHashMap;
importjava.util.Map;
/**
* 这是一个根据给定的百分比画粗细的自定义表格
*/
public classPercentageTableViewextendsView{
//values
privateLinkedHashMapmData;
private intmBarAboutPadding;
private intmTarget;
private booleanmIsShowDatePercentage;
private booleanmIsDayTable;
private intmMax;
private floatmBarWidthPercentage;
//MessageValues
private floatmViewY;
/**
*@paramcontext你懂的
*@paramdata你懂的
*@parambarAboutPadding左右pading
*@paramtarget目标值
*@paramisShowDatePercentage是否显示数值百分比
*@paramisDayTable是否为日标签
*/
publicPercentageTableView(Contextcontext,LinkedHashMap data,intbarAboutPadding,inttarget,booleanisShowDatePercentage,booleanisDayTable) {
super(context);
mData= data;
mBarAboutPadding= barAboutPadding;
mTarget= target;
mIsShowDatePercentage= isShowDatePercentage;
mIsDayTable= isDayTable;
mBarWidthPercentage=0.2f;
}
//可自定义线条宽度比例:mBarWidthPercentage
publicPercentageTableView(Contextcontext,LinkedHashMap data,intbarAboutPadding,inttarget,booleanisShowDatePercentage,booleanisDayTable,floatbarWidthPercentage) {
super(context);
mBarWidthPercentage= barWidthPercentage;
mData= data;
mBarAboutPadding= barAboutPadding;
mTarget= target;
mIsShowDatePercentage= isShowDatePercentage;
mIsDayTable= isDayTable;
}
publicPercentageTableView(Contextcontext,@NullableAttributeSetattrs) {
super(context,attrs);
}
@Override
protected voidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected voidonDraw(Canvascanvas) {
if(mData.size() ==0) {
super.onDraw(canvas);
}else{
mViewY= (float) (getHeight() *0.8);
setMaxValue();
drawLine(canvas);
drawBar(canvas);
}
}
private voidsetMaxValue() {
//map.foreach要jdk8才支持哦.
for(Object obj:mData.entrySet()) {
Map.Entry entry= (Map.Entry)obj;
String key= (String)entry.getKey();
Integer value=mData.get(key);
if(value>mMax) {
mMax=value;
}
}
}
private voiddrawBar(Canvascanvas) {
floataWidth= (getWidth() -mBarAboutPadding*2) /mData.size();
//padding的宽度加上柱子的宽度的一半
floatstartX=aWidth/2+mBarAboutPadding;
Paint paint=newPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.rgb(54,195,241));
paint.setStrokeWidth(aWidth*mBarWidthPercentage);
Paint paintText=newPaint();
paintText.setStyle(Paint.Style.STROKE);
paintText.setAntiAlias(true);
paintText.setDither(true);
paintText.setColor(Color.rgb(156,156,156));
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setTextSize(18);
Iterator>dataIterator=mData.entrySet().iterator();
inti=0;
while(dataIterator.hasNext()) {
Map.Entrynext=dataIterator.next();
//*0.8是为了让顶部数值显示出来.
floatstartY=mViewY-next.getValue() *1f/mMax*mViewY*0.8f;
if(i==0) {
canvas.drawLine(startX,startY,startX,mViewY,paint);
}else{
startX+=aWidth;
canvas.drawLine(startX,startY,startX,mViewY,paint);
}
if(mIsDayTable) {
drawDayTitle(canvas,i,next.getKey(),startX,paintText);
if(next.getValue() ==mMax) {
canvas.drawText(next.getValue() +"",startX,startY-18,paintText);
}
i++;
continue;
}
//画数值
canvas.drawText(next.getValue() +"",startX,startY-18,paintText);
//画列标签Text
canvas.drawText(next.getKey(),startX,mViewY+18,paintText);
//画列百分比Text
if(mIsShowDatePercentage) {
intvalue=next.getValue() *100/mTarget;
canvas.drawText(value+"%",startX,mViewY+36,paintText);
}
i++;
}
}
/*
画指定日期下面的文字
*/
private voiddrawDayTitle(Canvascanvas,intposition,Stringtitle,floatstartX,PaintpaintText) {
switch(position) {
case0:
case8:
case16:
case23:
canvas.drawText(title,startX,mViewY+18,paintText);
break;
}
}
//画底部和目标线
private voiddrawLine(Canvascanvas) {
Paint paint=newPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.rgb(220,221,221));
paint.setStrokeWidth(1);
canvas.drawLine(0,mViewY,getWidth(),mViewY,paint);
if(mIsShowDatePercentage) {
floaty=mTarget*1f/mMax*mViewY*0.8f;
canvas.drawLine(0,mViewY-y,getWidth(),mViewY-y,paint);
}
}
}