自定义view-绘制统计图

news/2024/7/5 8:29:29

效果图如下:

主要代码:

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);
}
}



http://www.niftyadmin.cn/n/3649385.html

相关文章

怎么计算apk的启动时间?

利用python或者直接用adb命令怎么计算apk的启动时间呢&#xff1f;就是计算从点击图标到apk完全启动所花费的时间。比如&#xff0c;对游戏来说就是点击游戏图标到进入到登录界面的这段时间。 已知的两种方法貌似可以获取&#xff0c;但是感觉结果不准确&#xff1a;一种是&…

如何在Ubuntu 18.04上安装和使用TimescaleDB

The author selected the Computer History Museum to receive a donation as part of the Write for DOnations program. 作者选择“ 计算机历史博物馆”作为“ Write for DOnations”计划的一部分接受捐赠。 介绍 (Introduction) Many applications, such as monitoring sys…

国产操作系统deepin(深度)的安装与体验

Deepin原名Linux Deepin、deepin os、深度系统、深度操作系统&#xff0c;于2014年4月改名Deepin。deepin操作系统是由武汉深之度科技有限公司开发的Linux发行版。deepin操作系统是一个基于Debian的Linux操作系统&#xff0c;专注于使用者对日常办公、学习、生活和娱乐的操作体…

[收藏]再见,CodeArtist

再见&#xff0c;CodeArtistBy [ 赵勖予 ] 2005-3-29 21:32:41 CodeMonkeyMonkeyCodeMonkeyCodeMonkey LeeCodeMonkey2002NGFFSLee Lee进入我们公司的时候已经三十三岁&#xff0c;单身。头发在脑后梳成一个大马尾辫&#xff0c;辫子里白花花的全是头皮。他一年四季只穿一件蓝…

Android 内核解剖 - AMS(Activity Manager Service)

zz:http://www.myexception.cn/android/1792465.htmlandroid内核剖析学习笔记&#xff1a;AMS&#xff08;ActivityManagerService&#xff09;内部原理和工作机制一、ActivityManagerService提供的主要功能&#xff1a; &#xff08;1&#xff09;统一调度各应用程序的…

CentOS 7安装MySQL-5.7数据库

MySQL是一个关系型数据库管理系统&#xff0c;由瑞典MySQL AB 公司开发&#xff0c;属于Oracle旗下产品。MySQL是最流行的关系型数据库管理系统之一&#xff0c;在WEB应用方面&#xff0c;MySQL是最好的RDBMS (Relational Database Management System&#xff0c;关系数据库管理…

bootstrap 小程序_如何将Bootstrap添加到Ruby on Rails应用程序

bootstrap 小程序介绍 (Introduction) If you are developing a Ruby on Rails application, you may be interested in adding styles to your project to facilitate user engagement. One way to do this is by adding Bootstrap, an HTML, CSS, and JavaScript framework d…

server——短信提醒

一、首先需要开启服务 startService(new Intent(MainActivity.this,MyService.class)); 开启肯定也需要关闭服务 stopService(new Intent(MainActivity.this,MyService.class)); 不要忘了在manifest中注册: <service android:name"com.dongge.service.MyService&…