Skip to content

Commit

Permalink
Added Arrow Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
shrikanth7698 committed Dec 15, 2021
1 parent e617f96 commit 0e900b7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mProperties.onShapePicked(ShapeType.OVAL);
} else if (checkedId == R.id.rectRadioButton) {
mProperties.onShapePicked(ShapeType.RECTANGLE);
} else if (checkedId == R.id.arrowRadioButton) {
mProperties.onShapePicked(ShapeType.ARROW);
} else {
mProperties.onShapePicked(ShapeType.BRUSH);
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/fragment_bottom_shapes_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_line" />
<RadioButton
android:id="@+id/arrowRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arrow" />

<RadioButton
android:id="@+id/ovalRadioButton"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Stack;

import ja.burhanrashid52.photoeditor.shape.AbstractShape;
import ja.burhanrashid52.photoeditor.shape.ArrowShape;
import ja.burhanrashid52.photoeditor.shape.BrushShape;
import ja.burhanrashid52.photoeditor.shape.LineShape;
import ja.burhanrashid52.photoeditor.shape.OvalShape;
Expand Down Expand Up @@ -69,9 +70,15 @@ private Paint createPaint() {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
if(currentShapeBuilder.getShapeType() == ShapeType.ARROW) {
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeJoin(Paint.Join.MITER);
paint.setStrokeCap(Paint.Cap.BUTT);
} else {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

// apply shape builder parameters
Expand Down Expand Up @@ -175,6 +182,8 @@ private void createShape() {
shape = new RectangleShape();
} else if (currentShapeBuilder.getShapeType() == ShapeType.LINE) {
shape = new LineShape();
} else if (currentShapeBuilder.getShapeType() == ShapeType.ARROW) {
shape = new ArrowShape();
} else {
shape = new BrushShape();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ja.burhanrashid52.photoeditor.shape;

import static java.lang.Math.cos;
import static java.lang.Math.sin;

import android.graphics.Path;
import android.util.Log;

import androidx.annotation.NonNull;

public class ArrowShape extends AbstractShape {

private float lastX, lastY;

@Override
protected String getTag() { return "LineShape"; }

@Override
public void startShape(float x, float y) {
Log.d(getTag(), "startShape@ " + x + "," + y);
left = x;
top = y;
}

@Override
public void moveShape(float x, float y) {
right = x;
bottom = y;

float dx = Math.abs(x - lastX);
float dy = Math.abs(y - lastY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
path = createLinePath();
lastX = x;
lastY = y;
}
}

private @NonNull Path createLinePath() {
float angle = 90;
float radius = 35;
float angleRad = (float) (Math.PI*angle/180.0f);
float lineAngle = (float) (Math.atan2(bottom-top,right-left));
Path path = new Path();
// line
path.moveTo(left, top);
path.lineTo(right, bottom);
// arrow head
path.moveTo(right, bottom);
path.lineTo(
(float)(right-radius*cos(lineAngle - (angleRad / 2.0))),
(float)(bottom-radius*sin(lineAngle - (angleRad / 2.0)))
);
path.lineTo(
(float)(right-radius*cos(lineAngle + (angleRad / 2.0))),
(float)(bottom-radius*sin(lineAngle + (angleRad / 2.0)))
);
path.close();
return path;
}

@Override
public void stopShape() {
Log.d(getTag(), "stopShape");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum ShapeType {
BRUSH,
OVAL,
RECTANGLE,
LINE
LINE,
ARROW
}

0 comments on commit 0e900b7

Please sign in to comment.