android - 如何在不使用任何外部 API 的情况下在 Android 上绘制图形?

标签 android graph

<分区>

我了解如何使用 Canvas 绘制线条,但如何使用相同的线条使用 Canvas 绘制图形?

问题出在坐标上。 (0,0) 从设备的左上角开始。如何将 (0,0) 设置为边距并绘制相对于边距的特定线?

最佳答案

1)创建一个 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)然后创建另一个类扩展 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;
}


}   

关于android - 如何在不使用任何外部 API 的情况下在 Android 上绘制图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11897582/

相关文章:

android检测图像在webview中点击?

android - Android 上的默认模式 MVC

algorithm - 完全断开二分图

python - 如何对非正态分布进行标准化?

python - 从没有值(value)的字典中保存一个键

excel - 使用 SUMIF 进行图表和分组?

android - 对于单个布局 XML 文件,多少个 ViewStub 太多了?

安卓 XML : drawing StackOverflow's GraphicDesign's diamond/rhombus button with diamond/rhombus borders

android - 如何授予不属于应用程序的文件的权限

python - python中的文本挖掘图句子