Northumber
Northumber

Reputation: 315

Android java: draw image of a sine waveform

How to draw an image (of any type, png, jpg, bmp etc) of a Sine wave knowing frequency with amplitude = 1?

Example:

sine wave: amplitude over time

Upvotes: 0

Views: 1534

Answers (1)

ᴘᴀɴᴀʏɪᴏᴛɪs
ᴘᴀɴᴀʏɪᴏᴛɪs

Reputation: 7529

One option is to use AndroidPlot. Firstly add it to your dependencies:

dependencies {
    compile "com.androidplot:androidplot-core:1.4.0"
}

Add an XYPlot to your layout and then start by amending the following example

More specifically have a look at:

   public Number getY(int series, int index) {
        if (index >= SAMPLE_SIZE) {
            throw new IllegalArgumentException();
        }
        double angle = (index + (phase))/FREQUENCY;
        double amp = sinAmp * Math.sin(angle);
        switch (series) {
            case SINE1:
                return amp;
            case SINE2:
                return -amp;
            default:
                throw new IllegalArgumentException();
        }
    }

And try changing sinAmp to your desired value.

Upvotes: 2

Related Questions