package com.kdanmobile.reader.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;

/**
 * @类名:WaveLineView
 * @类描述:自定义控件实现波浪线
 * @作者:zhouguifang
 * @创建时间:2015-7-7-上午11:35:01
 * @修改人:
 * @修改时间:
 * @修改备注:
 * @版本:
 * @Copyright:(c)-2015kdan mobile
 */
public class WaveLineView extends View {
	private Paint paint;
	private Path path;
	private int color = 0xffff0000;
	private float thickness = 20;
	private float bili = 1.f / 6;

	public WaveLineView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setColor(color);
		paint.setAntiAlias(true);
		paint.setStrokeCap(Cap.ROUND);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(20f);
	}

	public void setThickness(float thickness) {
		this.thickness = thickness;
		invalidate();
	}

	public void setColor(int color) {
		this.color = color;
		invalidate();
	}

	@Override
	public void draw(@NonNull Canvas canvas) {
		super.draw(canvas);
		if (getWidth() > 0) {
			if (path == null) {
				int w = getWidth();
				int h = getHeight();
				path = new Path();
				path.moveTo(0, h / 2f);
				path.lineTo((w * 30) / 360, (h / 2) - (h * bili * 0.5f));
				path.lineTo((w * 45) / 360, (h / 2) - (h * bili * 0.707f));
				path.lineTo((w * 60) / 360, (h / 2) - (h * bili * 0.866f));
				path.lineTo((w * 90) / 360, (h / 2) - (h * bili * 1.0f));
				path.lineTo((w * 120) / 360, (h / 2) - (h * bili * 0.866f));
				path.lineTo((w * 135) / 360, (h / 2) - (h * bili * 0.707f));
				path.lineTo((w * 150) / 360, (h / 2) - (h * bili * 0.5f));
				path.lineTo((w * 180) / 360, (h / 2) - (h * bili * 0.0f));
				path.lineTo((w * 210) / 360, (h / 2) + (h * bili * 0.5f));
				path.lineTo((w * 225) / 360, (h / 2) + (h * bili * 0.707f));
				path.lineTo((w * 240) / 360, (h / 2) + (h * bili * 0.866f));
				path.lineTo((w * 270) / 360, (h / 2) + (h * bili * 1.0f));
				path.lineTo((w * 300) / 360, (h / 2) + (h * bili * 0.866f));
				path.lineTo((w * 315) / 360, (h / 2) + (h * bili * 0.707f));
				path.lineTo((w * 330) / 360, (h / 2) + (h * bili * 0.5f));
				path.lineTo((w * 360) / 360, (h / 2) + (h * bili * 0.0f));
			}
			paint.setColor(color);
			paint.setStrokeWidth(thickness);
			canvas.drawPath(path, paint);
		}
	}
}
