自定义view-绘制统计图

效果图如下:

主要代码:

public class MyTestView extends View {
private Paint mPaint;
private static final String title = "2013-2017上半年的销售情况";
private static final String content = "来自公司销售的统计数据";


public MyTestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}


public MyTestView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}


public MyTestView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint=new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(18);
//绘制标题
canvas.drawText(title, 20,20, mPaint);

canvas.drawText("单位:万元", 20,90, mPaint);
mPaint.setTextSize(10);
//绘制线
canvas.drawLine(50, 100, 50, 500, mPaint);//纵坐标
canvas.drawLine(50, 500, 400, 500, mPaint);//横坐标
//纵坐标的值
int[] array = { 0, 50, 100, 150, 200, 250, 300, 350 };
for(int i=0;i<array.length;i++){
//首先绘制分割线
canvas.drawLine(50, 500-array[i], 54, 500-array[i], mPaint);
//绘制值
canvas.drawText(array[i]+"", 20, 500-array[i], mPaint);
}

//横坐标
String[] array2 = { "2008年", "2009年", "2010年", "2011上半年" };
for(int i=0;i<array2.length;i++){
canvas.drawText(array2[i]+"", array[i]+85, 520, mPaint);
}
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL);
canvas.drawRect(new Rect(90, 500 - 56, 110, 500), mPaint);
canvas.drawRect(new Rect(140, 500 - 98, 160, 500), mPaint);
canvas.drawRect(new Rect(190, 500 - 207, 210, 500), mPaint);
canvas.drawRect(new Rect(240, 500 - 318, 260, 500), mPaint);
mPaint.setColor(Color.BLACK);
canvas.drawText("56.32", 88, 500 - 58, mPaint);
canvas.drawText("90.00", 138, 500 - 100, mPaint);
canvas.drawText("207.67", 188, 500 - 209, mPaint);
canvas.drawText("318.56", 238, 500 - 320, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(16);
canvas.drawText(content, 20, 560, mPaint);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值