如何在不使用任何外部API的情况下在Android上绘制图形? [英] How do I draw a graph on Android without using any external API?
本文介绍了如何在不使用任何外部API的情况下在Android上绘制图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我懂得如何使用画布绘制线条,但是如何使用画布绘制图形的相同线条?
问题出在坐标。 (0,0)从设备的左上角开始。我如何设置(0,0)作为边距,并相对于边距绘制特定的线?
解决方案
<1>创建一个活动。
public class GraphView1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
float [] values = new float [] {your values};
String [] verlabels = new String [] {your values};
String [] horlabels = new String [] {your values
GraphView graphView = new GraphView(this,values,GraphView,horlabels,verlabels,GraphView.BAR);
setContentView(graphView);
2 b)然后创建另一个类extends View: p>
public class GraphView2 extends View {
public static boolean LINE = true;
私人油漆;
私人浮动[]值;
private String [] str;
private String [] verlabels;
私有字符串标题;
私有布尔类型;
上下文上下文;
$ b public graphView(Context context,float [] values,String title,String [] str,String [] verlabels,boolean type){
super上下文);
if(values == null)
values = new float [0];
else
this.values = values;
if(title == null)
title =;
else
this.title = title;
if(str == null)
this.str = new String [0];
else
this.str = str;
if(verlabels == null)
this.verlabels = new String [0];
else
this.verlabels = verlabels;
this.type = type;
paint = new Paint();
}
@Override
protected void onDraw(final Canvas canvas){
context = getContext();
float border = 15;
float horstart = border * 2;
float height = getHeight();
float width = getWidth();
float max = getMax();
Log.w(max,+ max);
float min = getMin();
Log.w(min,+ min);
float diff = max - min;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);
paint.setTextAlign(Align.LEFT);
int vers = verlabels.length;
for(int i = 0; i< verlabels.length; i ++){
paint.setColor(Color.DKGRAY);
float y =((graphheight / vers)* i)+ border;
canvas.drawLine(horstart,y,width,y,paint);
paint.setColor(Color.WHITE);
paint.setTextSize(10);
canvas.drawText(verlabels [i],0,y,paint);
}
int hors = values.length;
for(int i = 0; i< str.length; i ++){
paint.setColor(Color.DKGRAY);
float x =((graphwidth / hors)* i)+ horstart;
canvas.drawLine(x,height - border,x,border,paint);
paint.setTextAlign(Align.LEFT);
if(i == str.length)
paint.setTextAlign(Align.RIGHT);
if(i == 0)
paint.setTextAlign(Align.LEFT);
paint.setColor(Color.WHITE);
paint.setTextSize(9);
canvas.drawText(str [i],x,height - 4,paint);
}
paint.setTextAlign(Align.CENTER);
canvas.drawText(title,(graphwidth / 2)+ horstart,border - 4,paint);
if(max!= min){
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
if(type == BAR){
float datalength = values.length;
float colwidth =(width - (2 * border))/ datalength;
for(int i = 0; i< values.length; i ++){
// float val = values [i] - min;
// float rat = val / diff;
// float h = graphheight * rat;
// canvas.drawRect((i * colwidth)+ horstart,(border -h)+ graphheight,((i * colwidth)+ horstart)+(colwidth - 1),height - (border - 1),涂料);
float graph_h = getHeight() - (border * 2);
// Log.e(,graph_h>+ graph_h);
float ind_h = graph_h / 7;
//Log.e(,ind_h>+ ind_h);
float t = values [i] / 5;
float top =(graph_h - ind_h *(t));
// Log.e(,>+ i + 1);
// Log.e(,top>+ top);
//对于0到5之间的值,反之亦然
//Log.e(,values [i]>+ values [i]);
float acc = ind_h / 5;
acc = acc *(values [i]%5);
// Log.e(,acc>+ acc);
canvas.drawRect((i * colwidth)+ horstart,top + border-acc,((i * colwidth)+ horstart)+(colwidth - 1),graph_h + border,paint);
}
} else {
float datalength = values.length;
float colwidth =(width - (2 * border))/ datalength;
float halfcol = colwidth / 2;
float lasth = 0;
for(int i = 0; i< values.length; i ++){
float val = values [i] - min;
float rat = val / diff;
float h = graphheight * rat;
if(i> 0)
canvas.drawLine(((i - 1)* colwidth)+(horstart + 1)+ halfcol,(border - lasth)+ graphheight,(i * colwidth) +(horstart + 1)+ halfcol,(border-h)+ graphheight,paint);
lasth = h;
private float getMax(){
float maximum = Integer .MIN_VALUE;
for(int i = 0; i< values.length; i ++)
if(values [i]> maximum)
largest = values [i]
回报最大;
private float getMin(){
float minimum = Integer.MAX_VALUE;
for(int i = 0; i if(values [i] <最小)
最小=值[i];
回报最小;
}
}
I understand how to draw lines using a canvas, but how can I use the same lines using a canvas to draw a graph?
The problem is with the coordinates. (0,0) starts right at the top left corner of the device. How can I set (0,0) as the margin and draw the particular line with respect to the margin?
解决方案 1)Create an activity.
public class GraphView1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
float[] values = new float[] { "your values"};
String[] verlabels = new String[] { "your values" };
String[] horlabels = new String[] { "your values"
GraphView graphView = new GraphView(this, values,"GraphView",horlabels, verlabels, GraphView.BAR);
setContentView(graphView);
}
}
2)Then create another class extends View:
public class GraphView2 extends View{
public static boolean LINE = true;
private Paint paint;
private float[] values;
private String[] str;
private String[] verlabels;
private String title;
private boolean type;
Context context;
public GraphView(Context context, float[] values, String title, String[] str,String[] verlabels, boolean type) {
super(context);
if (values == null)
values = new float[0];
else
this.values = values;
if (title == null)
title = "";
else
this.title = title;
if (str == null)
this.str = new String[0];
else
this.str = str;
if (verlabels == null)
this.verlabels = new String[0];
else
this.verlabels = verlabels;
this.type = type;
paint = new Paint();
}
@Override
protected void onDraw(final Canvas canvas) {
context=getContext();
float border = 15;
float horstart = border * 2;
float height = getHeight();
float width = getWidth();
float max = getMax();
Log.w("max", ""+max);
float min = getMin();
Log.w("min", ""+min);
float diff = max - min;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);
paint.setTextAlign(Align.LEFT);
int vers = verlabels.length;
for (int i = 0; i < verlabels.length; i++) {
paint.setColor(Color.DKGRAY);
float y = ((graphheight / vers) * i) + border;
canvas.drawLine(horstart, y, width, y, paint);
paint.setColor(Color.WHITE);
paint.setTextSize(10);
canvas.drawText(verlabels[i], 0, y, paint);
}
int hors = values.length;
for (int i = 0; i < str.length; i++) {
paint.setColor(Color.DKGRAY);
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawLine(x, height - border, x, border, paint);
paint.setTextAlign(Align.LEFT);
if (i==str.length)
paint.setTextAlign(Align.RIGHT);
if (i==0)
paint.setTextAlign(Align.LEFT);
paint.setColor(Color.WHITE);
paint.setTextSize(9);
canvas.drawText( str[i], x, height - 4, paint);
}
paint.setTextAlign(Align.CENTER);
canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
if (max != min) {
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
if (type == BAR) {
float datalength = values.length;
float colwidth = (width - (2 * border)) / datalength;
for (int i = 0; i < values.length; i++) {
// float val = values[i] - min;
// float rat = val / diff;
// float h = graphheight * rat;
// canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
float graph_h = getHeight()-(border*2);
// Log.e("", "graph_h > "+graph_h);
float ind_h = graph_h/7;
//Log.e("", "ind_h > "+ind_h);
float t = values[i]/5;
float top = (graph_h - ind_h*(t));
// Log.e("", " > "+i+1);
// Log.e("", "top > "+top);
//for values between 0 and 5 ,vice versa
//Log.e("", " values[i] > "+values[i]);
float acc = ind_h/5;
acc = acc * (values[i]%5);
// Log.e("", " acc > "+acc);
canvas.drawRect((i * colwidth) + horstart, top+border-acc , ((i * colwidth) + horstart) + (colwidth - 1), graph_h+border, paint);
}
} else {
float datalength = values.length;
float colwidth = (width - (2 * border)) / datalength;
float halfcol = colwidth / 2;
float lasth = 0;
for (int i = 0; i < values.length; i++) {
float val = values[i] - min;
float rat = val / diff;
float h = graphheight * rat;
if (i > 0)
canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
lasth = h;
}
}
}
}
private float getMax() {
float largest = Integer.MIN_VALUE;
for (int i = 0; i < values.length; i++)
if (values[i] > largest)
largest = values[i];
return largest;
}
private float getMin() {
float smallest = Integer.MAX_VALUE;
for (int i = 0; i < values.length; i++)
if (values[i] < smallest)
smallest = values[i];
return smallest;
}
}
这篇关于如何在不使用任何外部API的情况下在Android上绘制图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文