# Creation of sine wave in android

Page 1 of 1

## 1 Replies - 533 Views - Last Post: 13 May 2011 - 02:25 AM

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 01-February 11

# Creation of sine wave in android

Posted 13 May 2011 - 12:31 AM

Hi..

i wrote a code for creation of sine wave in android, i am giving my code below, to make it easy and helpful to the android learners..
and it's coding it's peaty simple, just what logic i used here is, drawing a small,small lines in the form of sin wave, just see ( & trace)the code and u can understand the logic behind this easily .

```package com.lnties.sinewave;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class sinewave extends Activity {
/** Called when the activity is first created. */

//static int x1=35,y1=35,x2=36,y2=39;

static final int SCALEFACTOR = 200;
static   int cycles;
static  int points;
static  double[] sines;
static  int[] pts;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
}
private class myView extends View{

public myView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {

// set drawing color
Paint p = new Paint();
p.setColor(Color.GREEN);
p.setStrokeWidth(1);

cycles = 10;
points = SCALEFACTOR * cycles * 2;
sines = new double[points];
for (int i = 0; i < points; i++) {
double radians = (Math.PI / SCALEFACTOR) * i;
}

int maxWidth = getWidth();
double hstep = (double) maxWidth / (double) points;
int maxHeight = 189;
pts = new int[points];
for (int i = 0; i < points; i++)
pts[i] = (int) (sines[i] * maxHeight / 2 * .95 + maxHeight / 2);

for (int i = 1; i < points; i++) {
int x1 = (int) ((i - 1) * hstep);
int x2 = (int) (i * hstep);
int y1 = pts[i - 1];
int y2 = pts[i];

canvas.drawLine(x1,y1,x2,y2, p);

//

}
}

}
}
```

and output is also attaching to this.

Is This A Good Question/Topic? 0

## Replies To: Creation of sine wave in android

### #2 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Creation of sine wave in android

Posted 13 May 2011 - 02:25 AM

you can add it as a snippet here:
http://www.dreaminco...nput.php?cid=23

Thanks for contributing, but if you want to share snippets, you should do it through tutorials and snippets section. Thanks!