在Android Plot中更改正弦波宽度

时间:2014-01-30 09:52:22

标签: android graph androidplot android-graphview

我正在使用android绘图来绘制我的动态日期。任何人都可以告诉我如何改变 下图中正弦波的宽度。

enter image description here

我正在使用Dynamic XY Plot的示例代码。

以下代码是我的活动。

public class MyActivity extends Activity {

// redraws a plot whenever an update is received:
private class MyPlotUpdater implements Observer {
    Plot plot;
    public MyPlotUpdater(Plot plot) {
        this.plot = plot;
    }
    @Override
    public void update(Observable o, Object arg) {
        try {
            plot.postRedraw();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

private XYPlot dynamicPlot;
private XYPlot staticPlot;
private MyPlotUpdater plotUpdater;

@Override
public void onCreate(Bundle savedInstanceState) {

    // android boilerplate stuff
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // get handles to our View defined in layout.xml:
    dynamicPlot = (XYPlot) findViewById(R.id.dynamicPlot);

    plotUpdater = new MyPlotUpdater(dynamicPlot);

    // only display whole numbers in domain labels
    dynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));

    // getInstance and position datasets:
    SampleDynamicXYDatasource data = new SampleDynamicXYDatasource();
    SampleDynamicSeries sine1Series = new SampleDynamicSeries(data, 0, "Sine 1");
    SampleDynamicSeries sine2Series = new SampleDynamicSeries(data, 1, "Sine 2");

    dynamicPlot.addSeries(sine1Series, new LineAndPointFormatter(Color.rgb(0, 0, 0), null, Color.rgb(0, 80, 0)));

    // create a series using a formatter with some transparency applied:
    LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));
    f1.getFillPaint().setAlpha(220);
    dynamicPlot.addSeries(sine2Series, f1);
    dynamicPlot.setGridPadding(5, 0, 5, 0);

    // hook up the plotUpdater to the data model:
    data.addObserver(plotUpdater);

    dynamicPlot.setDomainStepMode(XYStepMode.SUBDIVIDE);
    dynamicPlot.setDomainStepValue(sine1Series.size());

    // thin out domain/range tick labels so they dont overlap each other:
    dynamicPlot.setTicksPerDomainLabel(5);
    dynamicPlot.setTicksPerRangeLabel(3);
    dynamicPlot.disableAllMarkup();

    // freeze the range boundaries:
    dynamicPlot.setRangeBoundaries(-100, 100, BoundaryMode.FIXED);

    // kick off the data generating thread:
    new Thread(data).start();
}

}

1 个答案:

答案 0 :(得分:1)

在文件SampleDynamicXYDatasource.java中,您可以找到

double amp = sinAmp * Math.sin(index + phase + 4);

产生正弦波。通过改变索引(例如,通过将其乘以0.5),可以使正弦波的周期更大,或者在图形中说,在x轴上缩放它。