-1

I'm new to the Android SDK, but am pretty well versed in Java. I need to create a graphing function for a class, but I'm not entirely sure how to do it without using swing and in XML. Any suggestions?

4

2 回答 2

0

您不必使用 XML。它只是 eclipse 希望您创建 UI 的默认方式。做你想做的最好的方法是创建一个扩展视图并覆盖 ondraw 方法的类。然后你调用 setContentView(yourclass);

下面是一些代码的示例,它只是在屏幕上画了一条线,足以让您入门。

主要活动:

package com.example.myexample;

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Create class that extends view
    Example myexample = new Example(this);

    setContentView(myexample);
}

}

Example 类看起来像这样:

package com.example.myexample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class Example extends View{

    //You will need to declare a variable of paint to draw
    Paint mypaint = new Paint();


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

    //This is the function where you draw to the screen
    @Override
    public void onDraw (Canvas canvas){


        canvas.drawLine(25, 25, 25, 50, mypaint);   
    }

}
于 2013-08-30T00:01:53.410 回答
0

如果您正在谈论显示诸如条形图或折线图之类的图形,我建议您使用库来帮助您。看看这里:

https://stackoverflow.com/questions/424752/any-good-graphing-packages-for-android?lq=1

于 2013-08-29T23:32:11.510 回答