Skip to content

Commit

Permalink
Ref Line Feature Added !!
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddhesh2377 committed Apr 9, 2024
1 parent 7d99884 commit 5b7c44b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 61 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>
5 changes: 5 additions & 0 deletions app/src/main/java/com/dark/canves/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import com.dark.canves.view.DrawingView;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.materialswitch.MaterialSwitch;

public class MainActivity extends AppCompatActivity {

DrawingView drawingView;
MaterialButton clear;
MaterialButtonToggleGroup drawState;
MaterialSwitch ref;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -22,6 +24,9 @@ protected void onCreate(Bundle savedInstanceState) {
drawingView = findViewById(R.id.drawingView);
clear = findViewById(R.id.clear);
drawState = findViewById(R.id.draw_state);
ref = findViewById(R.id.refBtn);

ref.setOnCheckedChangeListener((buttonView, isChecked) -> drawingView.isRef = isChecked);

drawState.addOnButtonCheckedListener((materialButtonToggleGroup, i, b) -> {
if (b) {
Expand Down
135 changes: 75 additions & 60 deletions app/src/main/java/com/dark/canves/view/DrawingView.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.dark.canves.R;

import java.util.ArrayList;
import java.util.List;

public class DrawingView extends View {

private final Paint paint, dottedPaint, grid, text;
private final Paint paint, dottedPaint, grid, text, refPaint;
private final List<Path> paths;
private final List<LineData> lines;
private final List<LineData> circles;
private final List<RectF> boxes;
public boolean isRef = true;
private boolean drawBox = false;
private boolean drawFree = true;
private boolean drawLine = false;
private boolean drawCircle = false;
private boolean isDrawing = false;
private Path currentPath;
private LineData currentLine, currentCircle;
private LineData currentLine, currentCircle, refLine, refLine2;
private RectF currentBox;

public DrawingView(Context context, AttributeSet attrs) {
Expand All @@ -53,13 +57,18 @@ public DrawingView(Context context, AttributeSet attrs) {
dottedPaint.setStyle(Paint.Style.STROKE);
dottedPaint.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0)); // Set a dash effect

refPaint = new Paint();
refPaint.setColor(ContextCompat.getColor(getContext(), R.color.refColor));
refPaint.setStrokeWidth(5);
refPaint.setStyle(Paint.Style.STROKE);
refPaint.setPathEffect(new DashPathEffect(new float[]{15, 15}, 0)); // Set a dash effect

grid = new Paint();
grid.setColor(Color.LTGRAY);
grid.setStrokeWidth(2);
grid.setStyle(Paint.Style.FILL);
// grid.setPathEffect(new DashPathEffect(new float[]{10, 20}, 0)); // Set a dash effect

// Initialize lists to store paths and boxes

paths = new ArrayList<>();
boxes = new ArrayList<>();
lines = new ArrayList<>();
Expand All @@ -70,41 +79,7 @@ public DrawingView(Context context, AttributeSet attrs) {
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);

int width = getWidth();
int height = getHeight();

int line = 10;

// Define the number of grid lines you want
int numHorizontalLines, numVerticalLines;

if (width < height) {
numHorizontalLines = (int) (4.5 * line);
numVerticalLines = 2 * line;
} else {
numHorizontalLines = 2 * line;
numVerticalLines = (int) (4.5 * line);
}


// Calculate the spacing between grid lines
float horizontalSpacing = (float) height / (numHorizontalLines + 1);
float verticalSpacing = (float) width / (numVerticalLines + 1);

// Draw horizontal grid lines
float y = horizontalSpacing;
for (int i = 0; i < numHorizontalLines; i++) {
canvas.drawLine(0, y, width, y, grid);
y += horizontalSpacing;
}

// Draw vertical grid lines
float x = verticalSpacing;
for (int i = 0; i < numVerticalLines; i++) {
canvas.drawLine(x, 0, x, height, grid);
x += verticalSpacing;
}
drawGrids(canvas);

for (RectF box : boxes) {
canvas.drawRect(box, paint);
Expand All @@ -123,32 +98,29 @@ protected void onDraw(@NonNull Canvas canvas) {
}


if (drawBox) {
if (currentBox != null) {
canvas.drawRect(currentBox, dottedPaint);
}
if (currentBox != null && drawBox) {
canvas.drawRect(currentBox, dottedPaint);
}

if (drawFree) {
if (currentPath != null) {
canvas.drawPath(currentPath, dottedPaint);
}
if (currentPath != null && drawFree) {
canvas.drawPath(currentPath, dottedPaint);
}

if (drawLine) {
if (currentPath != null) {
canvas.drawPath(currentPath, dottedPaint);
}
if (currentPath != null && drawLine) {
canvas.drawPath(currentPath, dottedPaint);
}

if (drawCircle) {
if (currentPath != null) {
if (isDrawing) {
canvas.drawLine(currentLine.sX, currentLine.sY, currentLine.eX, currentLine.eY, paint);
canvas.drawText("Radius: " + currentCircle.eX, currentLine.eX, currentLine.eY, text);
}
canvas.drawCircle(currentCircle.sX, currentCircle.sY, currentCircle.eX, dottedPaint);
if (currentPath != null && drawCircle) {
if (isDrawing) {
canvas.drawLine(currentLine.sX, currentLine.sY, currentLine.eX, currentLine.eY, paint);
canvas.drawText("Radius: " + currentCircle.eX, currentLine.eX, currentLine.eY, text);
}
canvas.drawCircle(currentCircle.sX, currentCircle.sY, currentCircle.eX, dottedPaint);
}

if (isDrawing && isRef) {
canvas.drawLine(refLine.sX, refLine.sY, refLine.eX, refLine.eY, refPaint); //Vertical Reference Line
canvas.drawLine(refLine2.sX, refLine2.sY, refLine2.eX, refLine2.eY, refPaint); //Horizontal Reference Line
}

}
Expand All @@ -166,6 +138,8 @@ public boolean onTouchEvent(MotionEvent event) {
currentPath = new Path();
currentLine = new LineData(x, y, x, y);
currentCircle = new LineData(x, y, 0, 0);
refLine = new LineData(x, 0, x, y); // Vertical Reference Line
refLine2 = new LineData(0, y, x, y); // Horizontal Reference Line
currentBox = new RectF(x, y, x, y);
currentPath.moveTo(x, y);
return true;
Expand All @@ -178,6 +152,14 @@ public boolean onTouchEvent(MotionEvent event) {
currentLine.eX = x;
currentLine.eY = y;

refLine.sX = x;
refLine.eX = x;
refLine.eY = y;

refLine2.sY = y;
refLine2.eX = x;
refLine2.eY = y;

// Calculate the radius for the circle
float dx = currentCircle.sX - x;
float dy = currentCircle.sY - y;
Expand All @@ -197,6 +179,7 @@ public boolean onTouchEvent(MotionEvent event) {
currentPath = null;
currentBox = null;
currentCircle = null;
refLine = null;
invalidate();
break;
default:
Expand All @@ -206,8 +189,41 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}

private void drawGrids(Canvas canvas) {
int width = getWidth();
int height = getHeight();

// Define the number of grid lines you want
int numHorizontalLines, numVerticalLines;

if (width < height) {
numHorizontalLines = (int) (4.5 * 10);
numVerticalLines = 2 * 10;
} else {
numHorizontalLines = 2 * 10;
numVerticalLines = (int) (4.5 * 10);
}


// Calculate the spacing between grid lines
float horizontalSpacing = (float) height / (numHorizontalLines + 1);
float verticalSpacing = (float) width / (numVerticalLines + 1);

// Draw horizontal grid lines
float y = horizontalSpacing;
for (int i = 0; i < numHorizontalLines; i++) {
canvas.drawLine(0, y, width, y, grid);
y += horizontalSpacing;
}

// Draw vertical grid lines
float x = verticalSpacing;
for (int i = 0; i < numVerticalLines; i++) {
canvas.drawLine(x, 0, x, height, grid);
x += verticalSpacing;
}
}

// Method to clear the drawing
public void clearDrawing() {
paths.clear();
boxes.clear();
Expand Down Expand Up @@ -243,7 +259,6 @@ public void DrawCircle() {
this.drawLine = false;
this.drawCircle = true;
}

}

class LineData {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/font/ubuntu_mono.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Ubuntu Mono"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>
25 changes: 24 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:text="Clear" />
android:text="@string/clear" />

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/draw_state"
Expand Down Expand Up @@ -84,4 +84,27 @@
android:background="#1A1A1A" />


<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
android:layout_below="@id/topPanel"
android:layout_margin="8dp"
app:cardCornerRadius="12dp">

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/refBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="6dp"
android:fontFamily="@font/ubuntu_mono"
android:text="@string/ref_line"
android:textSize="16sp"
android:checked="true"
android:textStyle="bold"
app:thumbTextPadding="0dp" />
</com.google.android.material.card.MaterialCardView>


</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<color name="white">#FFFFFFFF</color>
<color name="un_check">#7B7B7B</color>
<color name="check">#3F5FFF</color>
<color name="refColor">#FF5959</color>
</resources>
17 changes: 17 additions & 0 deletions app/src/main/res/values/font_certs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="com_google_android_gms_fonts_certs">
<item>@array/com_google_android_gms_fonts_certs_dev</item>
<item>@array/com_google_android_gms_fonts_certs_prod</item>
</array>
<string-array name="com_google_android_gms_fonts_certs_dev">
<item>
MIIEqDCCA5CgAwIBAgIJANWFuGx90071MA0GCSqGSIb3DQEBBAUAMIGUMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQ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CxURaSxaBvGdGDhfXE28LWuT9ozCl5xw4Yq5OGazvV24mZVSoOO0yZ31j7kYvtwYK6NeADwbSxDdJEqO4k//0zOHKrUiGYXtqw/A0LFFtqoZKFjnkCAQOjgdkwgdYwHQYDVR0OBBYEFMd9jMIhF1Ylmn/Tgt9r45jk14alMIGmBgNVHSMEgZ4wgZuAFMd9jMIhF1Ylmn/Tgt9r45jk14aloXikdjB0MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLR29vZ2xlIEluYy4xEDAOBgNVBAsTB0FuZHJvaWQxEDAOBgNVBAMTB0FuZHJvaWSCCQDC4IdGZEowjTAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBAUAA4IBAQBt0lLO74UwLDYKqs6Tm8/yzKkEu116FmH4rkaymUIE0P9KaMftGlMexFlaYjzmB2OxZyl6euNXEsQH8gjwyxCUKRJNexBiGcCEyj6z+a1fuHHvkiaai+KL8W1EyNmgjmyy8AW7P+LLlkR+ho5zEHatRbM/YAnqGcFh5iZBqpknHf1SKMXFh4dd239FJ1jWYfbMDMy3NS5CTMQ2XFI1MvcyUTdZPErjQfTbQe3aDQsQcafEQPD+nqActifKZ0Np0IS9L9kR/wbNvyz6ENwPiTrjV2KRkEjH78ZMcUQXg0L3BYHJ3lc69Vs5Ddf9uUGGMYldX3WfMBEmh/9iFBDAaTCK
</item>
</string-array>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/preloaded_fonts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/ubuntu_mono</item>
</array>
</resources>

0 comments on commit 5b7c44b

Please sign in to comment.