Android中利用View进行绘制折线图,饼状图,柱状图:
绘制这些图形时,主要用到的知识点为:
1.画笔的设置
2.画布的设置
3.让该类继承View,并复写onDraw方法即可
由于源码 较多,不进行代码粘贴了
下面进行截图看看:
1.折线图:
2.饼状图
3.柱状图
下面介绍几个方法关于绘制图形的:
可以参考这个api
http://www.android-doc.com/reference/android/graphics/Canvas.html
这里面方法很多:
//画一个点
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(100);//设置点的宽度
Canvas canvas=new Canvas(bitmap);
canvas.drawPoint(100, 100, paint);
chooseView.setImageBitmap(bitmap);
//画一条直线
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);//设置点的宽度
float startX=10;
float startY=20;
float stopX=100;
float stopY=200;
Canvas canvas=new Canvas(bitmap);
canvas.drawLine(startX, startY, stopX, stopY, paint);
chooseView.setImageBitmap(bitmap);
//画一个矩形
Paint paint=new Paint();
paint.setColor(Color.GRAY);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
Canvas canvas=new Canvas(bitmap);
int left=10;
int top=10;
int right=100;
int bottom=100;
Rect rect=new Rect(left, top, right, bottom);
canvas.drawRect(rect, paint);
// canvas.drawRect(left, top, right, bottom, paint);
chooseView.setImageBitmap(bitmap);
//椭圆
Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
int left=10;
int top=20;
int right=60;
int bottom=100;
RectF oval=new RectF(left, top, right, bottom);
Canvas canvas=new Canvas(bitmap);
canvas.drawOval(oval, paint);
chooseView.setImageBitmap(bitmap);
//圆
Paint paint=new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
Canvas canvas=new Canvas(bitmap);
int cx=100;
int cy=100;
int radius=20;
canvas.drawCircle(cx, cy, radius, paint);
chooseView.setImageBitmap(bitmap);
//路径
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
//路
Path path=new Path();
path.moveTo(20, 20);
path.lineTo(100, 200);
path.lineTo(204, 164);
path.lineTo(250, 175);
path.lineTo(20, 20);
Canvas canvas=new Canvas(bitmap);
canvas.drawPath(path, paint);
chooseView.setImageBitmap(bitmap);
//绘制字体
Paint paint=new Paint();
paint.setColor(Color.YELLOW);
paint.setTextSize(40);
paint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体
//引用外部字体
//Typeface typeface=Typeface.createFromAsset(getAssets(), "newFont.ttf");
//paint.setTypeface(typeface);
Canvas canvas=new Canvas(bitmap);
canvas.drawText("fuck you!", 50, 100, paint);
chooseView.setImageBitmap(bitmap);
//把字画在线上
Paint paint=new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setTypeface(Typeface.DEFAULT);
Path path=new Path();
path.moveTo(20, 20);
path.lineTo(100, 150);
path.lineTo(200, 220);
Canvas canvas=new Canvas(bitmap);
canvas.drawTextOnPath("Fuck you this text is on path!", path, 0, 0, paint);
chooseView.setImageBitmap(bitmap);
=============================