博主之前做的项目中,需要用到报表功能,之前在网上百度谷歌各种结果,没有一个是能够满足博主的老板的需求的,无奈之前博主只好自己去研究。终于研究出了一个不错的结果。先上效果图:
这是博主的一个项目的一个报表。老板要求可以点击左侧的月份进行月份比较,也可以点击选择月份,右边柱形图条状是可以响应点击事件的,并且可以左右滑动切换月份,如果柱形图内容过长超出界面,也是可以左右滑动查看未显示全的内容的。这里只讲柱形图的生成,其他界面效果博主就不讲解了。实际效果和这样有点差异,但不大,主要是实际效果在柱形图的顶端都还会显示数值。
自定义柱形图提供两种定义方式,可以在XML文件中写入,也可以再代码中new出来后加入一个容器中。这里博主就不在详细描述了,博主花了1个多小时才将该view从项目中独立出来。有兴趣的同学可以下载完整的项目下来学习。
github下载地址:https://github.com/victorfreedom1989/FreedomHistogram
优快云下载地址:http://download.youkuaiyun.com/detail/victorfreedom/8309505
1、自定义柱形图第一步,自定义柱形条:
package com.freedom.histogram;
import java.text.DecimalFormat;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
/**
* @ClassName: FreedomHistogram
* @author victor_freedom (x_freedom_reddevil@126.com)
* @createddate 2014-12-28 下午10:44:52
* @Description:
*/
public class FreedomHistogram {
// 柱形条宽度
private int with;
// 柱形条高度
private int height;
// 柱形条对应数值
private double count;
// 起始Y坐标
private int axisY;
// 标题起始Y坐标
private int titleY;
// 柱形条起始X坐标
private int axisX;
private Context context;
private DecimalFormat df;
private Paint mPaint;
public FreedomHistogram(Context context, int with, float axisY) {
this.with = with;
this.axisY = ScreenUtil.dip2px(context, axisY);
this.titleY = ScreenUtil.dip2px(context, axisY + 35);
this.context = context;
this.df = new DecimalFormat("#0.0");
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
}
public FreedomHistogram() {
};
public int getAxisX() {
return axisX;
}
public void setAxisX(int axisX) {
this.axisX = axisX;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getCount() {
return count;
}
public void setCount(double count) {
this.count = count;
}
public void drawHistogram(Canvas canvas, Paint paint) {
// 绘制第一层
canvas.drawRect(axisX - 1, axisY - height - 1, with + axisX + 1, axisY,
mPaint);
// 绘制第二层,让柱形条有白边,好看,根据个人喜好可以不绘制第二层。
canvas.drawRect(axisX, axisY - height, with + axisX, axisY - 1, paint);
// 绘制柱形条对应的数值
canvas.drawText(df.format(count),
axisX - ScreenUtil.dip2px(context, 5), axisY - height - 10,
paint);
}
public void drawTitle(Canvas canvas, Paint paint, String name) {
// 绘制标题
canvas.drawRect(axisX, titleY - height, with + axisX, titleY - 1, paint);
canvas.drawText(name, with + axisX + ScreenUtil.dip2px(context, 5),
titleY - 1, paint);
}
}
2、自定义柱形图第二步:构建柱形图
package com.freedom.histogram;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import