import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
*
* 画EQ(均衡器)频响曲线
*/
public class GainView extends View {
private static final String TAG = "GainView";
private int[] mGains;
private Paint mPain;
public GainView(Context context) {
this(context, null);
}
public GainView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GainView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//8个点,对应8个频段
mGains = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
mPain = new Paint();
}
//更新增益曲线
public void updateGain(int index, int gain) {
mGains[index] = gain;
postInvalidate();
}
//横纵坐标
private int[] getXIndex() {
int[] xs = new int[8];
int realWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int step = realWidth / (xs.length - 1);
for (int i = 0; i < xs.length; i++) {
xs[i] = step * i;
}
return xs;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int[] gains = new int[mGains.length];
for (int i = 0; i < mGains.length; i++) {
gains[i] = -mGains[i];
}
int[] xs = getXIndex();
int[] ys = new int[xs.length];
int half_height = (getHeight() - getPaddingTop() - getPaddingBottom()) / 2;
for (int i = 0; i < ys.length; i++) {
ys[i] = half_height * gains[i] / 12 + half_height;
}
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;