Skip to content

Instantly share code, notes, and snippets.

@gabriel-TheCode
Forked from raviteja83/SineView.java
Last active April 30, 2019 08:46
Show Gist options
  • Save gabriel-TheCode/bc4d57332d34ac29a941f601ec06b965 to your computer and use it in GitHub Desktop.
Save gabriel-TheCode/bc4d57332d34ac29a941f601ec06b965 to your computer and use it in GitHub Desktop.
sinewave
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by rt12148 on 14/8/16.
**/
public class SineView extends View {
Path path = new Path();
RectF rect = new RectF();
public SineView(Context context) {
super(context);
}
public SineView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
rect = new RectF(0,0,w,h);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setUp(canvas);
}
private void setUp(Canvas canvas) {
path.moveTo(rect.left,rect.centerY());
path.quadTo(rect.left + 50, rect.top, rect.left+ 100, rect.centerY());
path.quadTo(rect.left + 150, rect.bottom, rect.left+ 200, rect.centerY());
path.quadTo(rect.left + 250, rect.top, rect.left+ 300, rect.centerY());
path.quadTo(rect.left + 350, rect.bottom, rect.left+ 400, rect.centerY());
path.quadTo(rect.left + 450, rect.top, rect.left+ 500, rect.centerY());
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment