package com.allynav.pilesystem.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
public class RulerViewJava extends View {
private Paint backgroundPaint = new Paint();
private Paint tickPaint = new Paint();
private Paint cursorPaint = new Paint();
private Paint textPaint = new Paint();
private TextView cursorLabel;
private TextView cursorLabelLeft;
private TextView cursorLabelLeft1;
private TextView cursorLabelCenter;
private TextView cursorLabelRight;
private TextView cursorLabelRight1;
private int l1 = 8;
private int r1 = 12;
private int cursorPosition = getWidth() / 2;
private float offsetX;
private float offsetY;
public RulerViewJava(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundPaint.setColor(Color.WHITE);
backgroundPaint.setStyle(Paint.Style.FILL);
tickPaint.setStyle(Paint.Style.FILL);
tickPaint.setStrokeWidth(dpToPx(4f));
cursorPaint.setColor(Color.RED);
cursorPaint.setStyle(Paint.Style.FILL);
cursorLabel = new TextView(context);
cursorLabel.setTextSize(10);
cursorLabel.setTextColor(Color.BLACK);
cursorLabelLeft = new TextView(context);
cursorLabelLeft.setTextSize(13);
cursorLabelLeft.setText("-1.0");
cursorLabelLeft1 = new TextView(context);
cursorLabelLeft1.setTextSize(13);
cursorLabelLeft1.setText("-0.2");
cursorLabelCenter = new TextView(context);
cursorLabelCenter.setTextSize(13);
cursorLabelCenter.setText("0.0");
cursorLabelRight = new TextView(context);
cursorLabelRight.setTextSize(13);
cursorLabelRight.setText("0.2");
cursorLabelRight1 = new TextView(context);
cursorLabelRight1.setTextSize(13);
cursorLabelRight1.setText("1.0");
textPaint.setColor(Color.BLUE);
textPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF backgroundRect = new RectF(0f, 0f, getWidth(), getHeight() / 4);
canvas.drawRect(backgroundRect, backgroundPaint);
float startX = getPaddingLeft();
float endX = getWidth() - getPaddingRight();
float startY = getHeight() / 5f;
float endY = startY;
float tickSpacing = dpToPx(20f);
float tickLength = dpToPx(10f);
int tickCount = 21;
int middleTickIndex = tickCount / 2;
for (int i = 0; i < tickCount; i++) {
float x = startX + i * tickSpacing;
tickPaint.setColor(getTickColor(i, middleTickIndex));
canvas.drawLine(x, startY, x, endY - tickLength, tickPaint);
}
float triangleSide = dpToPx(10f);
float centerX = getWidth() / 2f + offsetX;
float centerY = getHeight() / 7f + offsetY;
float baseMidX = centerX;
float baseMidY = centerY + triangleSide / 2;
float leftX = baseMidX - triangleSide / 2;
float leftY = baseMidY;
float rightX = baseMidX + triangleSide / 2;
float rightY = leftY;
float topX = centerX;
float topY = baseMidY - triangleSide;
Path path = new Path();
path.moveTo(leftX, leftY);
path.lineTo(rightX, rightY);
path.lineTo(topX, topY);
path.close();
canvas.drawPath(path, cursorPaint);
canvas.drawText(cursorLabel.getText().toString(), rightX - 20, rightY + 10, textPaint);
canvas.drawText(cursorLabelLeft.getText().toString(), startX, startY - 15, textPaint);
canvas.drawText(cursorLabelLeft1.getText().toString(), startX + l1 * tickSpacing - 10, startY - 15, textPaint);
canvas.drawText(cursorLabelCenter.getText().toString(), startX + 10 * tickSpacing - 10, startY - 15, textPaint);
canvas.drawText(cursorLabelRight.getText().toString(), startX + r1 * tickSpacing - 10, startY - 15, textPaint);
canvas.drawText(cursorLabelRight1.getText().toString(), startX + 20 * tickSpacing, startY - 15, textPaint);
setOffsetYs(20f);
}
public void setCursorPosition(float position) {
cursorPosition = (int) position;
invalidate();
}
private float dpToPx(float dp) {
float density = getResources().getDisplayMetrics().density;
return dp * density;
}
public void setOffsetXs(float offset) {
this.offsetX = offset;
cursorLabel.setX(offset);
invalidate();
}
public void setOffsetYs(float offset) {
this.offsetY = offset;
invalidate();
}
public void setCursorLabel(String label) {
cursorLabel.setText(label + "˚");
invalidate();
}
public void setScaleSize(String left1, String right, int l1, int r1) {
cursorLabelLeft1.setText(left1);
cursorLabelRight.setText(right);
this.l1 = l1;
this.r1 = r1;
if (l1 == 0){
this.l1 = 8;
}
if (r1 == 0) {
this.r1 = 12;
}
invalidate();
}
private int getTickColor(int tickIndex, int middleTickIndex) {
if (tickIndex == 10) {
return Color.GREEN;
} else if (tickIndex == l1 || tickIndex == r1) {
return Color.RED;
} else {
return Color.GRAY;
}
}
}