【安卓知识点汇总】自定义View篇:实现统计图

自定义View篇:实现统计图

这里继承自View,重写onDraw方法:


public class CountView extends View {

	private Paint myPaint;
	private static final String title = "2006-2011上半年的销售情况";
	private static final String content = "来自公司销售的统计数据";

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

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

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		myPaint = new Paint();// 初始化画笔
		myPaint.setColor(Color.BLACK);
		myPaint.setTextSize(18);
		canvas.drawText(title, 20, 20, myPaint);
		// 绘制坐标
		canvas.drawLine(50, 100, 50, 500, myPaint);// 绘制纵坐标
		canvas.drawLine(50, 500, 400, 500, myPaint);// 绘制横坐标
		int[] array = { 0, 50, 100, 150, 200, 250, 300, 350 };
		myPaint.setTextSize(10);// 设置字体大小
		canvas.drawText("单位:万元", 20, 90, myPaint);
		for (int i = 0; i < array.length; i++) {
			canvas.drawLine(50, 500 - array[i], 54, 500 - array[i], myPaint);
			canvas.drawText(array[i] + "", 20, 500 - array[i], myPaint);
		}
		String[] array2 = { "2013年", "2014年", "2015年", "2016上半年" };
		for (int i = 0; i < array2.length; i++) {
			canvas.drawText(array2[i], array[i] + 80, 520, myPaint);
		}
		myPaint.setColor(Color.BLUE);
		myPaint.setStyle(Style.FILL);
		canvas.drawRect(new Rect(90, 500 - 56, 110, 500), myPaint);
		canvas.drawRect(new Rect(140, 500 - 98, 160, 500), myPaint);
		canvas.drawRect(new Rect(190, 500 - 207, 210, 500), myPaint);
		canvas.drawRect(new Rect(240, 500 - 318, 260, 500), myPaint);
		myPaint.setColor(Color.BLACK);
		canvas.drawText("56.32", 88, 500 - 58, myPaint);
		canvas.drawText("90.00", 138, 500 - 100, myPaint);
		canvas.drawText("207.67", 188, 500 - 209, myPaint);
		canvas.drawText("318.56", 238, 500 - 320, myPaint);
		myPaint.setColor(Color.BLACK);
		myPaint.setTextSize(16);
		canvas.drawText(content, 20, 560, myPaint);
	}
}



然后在布局文件中实现:


 <com.xh.count.view.CountView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </com.xh.count.view.CountView>



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值