Hey I don't know why but this loop is not working and giving me same error every time. If you know the fix please answer it down. And I am not able fix the issue.
Here is my loop
double angle = 0;
for (int i = 0; i < 120; i++, angle += 3) {
int x = (int) Math.ceil(i * 8.5);
int t = ((byte) (-Math.abs(bytes[x]) + 128))
* (canvas.getHeight() / 4) / 128;
points[i * 4] = (float) (getWidth() / 2
+ radius
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 1] = (float) (getHeight() / 2
+ radius
* Math.sin(Math.toRadians(angle)));
points[i * 4 + 2] = (float) (getWidth() / 2
+ (radius + t)
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 3] = (float) (getHeight() / 2
+ (radius + t)
* Math.sin(Math.toRadians(angle)));
}
Here is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.androidgrid.faysr, PID: 19297
java.lang.ArrayIndexOutOfBoundsException: length=128; index=128
at app.androidgrid.faysr.visualizer.view.CircleBarVisualizer.onDraw(CircleBarVisualizer.java:94)
at android.view.View.draw(View.java:17096)
at android.view.View.updateDisplayListIfDirty(View.java:16078)
I also tried changing 120 to 128 and i < 120 to i <= 120 but it's not
Here is my java class
public class CircleBarVisualizer extends BaseVisualizer {
private float[] points;
private Paint circlePaint;
private int radius;
public CircleBarVisualizer(Context context) {
super(context);
}
public CircleBarVisualizer(Context context,
@Nullable AttributeSet attrs) {
super(context, attrs);
}
public CircleBarVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void init() {
paint.setStyle(Paint.Style.STROKE);
circlePaint = new Paint();
radius = -1;
}
@Override
protected void onDraw(Canvas canvas) {
if (radius == -1) {
radius = getHeight() < getWidth() ? getHeight() : getWidth();
radius = (int) (radius * 0.65 / 2);
double circumference = 2 * Math.PI * radius;
paint.setStrokeWidth((float) (circumference / 120));
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(4);
}
circlePaint.setColor(color);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, circlePaint);
if (bytes != null) {
if (points == null || points.length < bytes.length * 4) {
points = new float[bytes.length * 4];
}
double angle = 0;
try {
for (int i = 0; i < 120; i++, angle += 3) {
int x = (int) Math.ceil(i * 8.5);
int t = ((byte) (-Math.abs(bytes[x]) + 128))
* (canvas.getHeight() / 4) / 128;
points[i * 4] = (float) (getWidth() / 2
+ radius
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 1] = (float) (getHeight() / 2
+ radius
* Math.sin(Math.toRadians(angle)));
points[i * 4 + 2] = (float) (getWidth() / 2
+ (radius + t)
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 3] = (float) (getHeight() / 2
+ (radius + t)
* Math.sin(Math.toRadians(angle)));
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
canvas.drawLines(points, paint);
}
super.onDraw(canvas);
}}
I am also try-catch so, it won't crash the app, but the result is not as I am excepting.