Skip to content

Commit 7b9f76e

Browse files
committed
Adding an Eraser and Starting a New Drawing
1 parent 34c985f commit 7b9f76e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

app/src/main/java/com/dragosholban/androiddrawing/DrawingView.java

+7
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ public void setCurrentColor(int color) {
7373
public void setCurrentWidth(int width) {
7474
currentWidth = (width + 1) * 2;
7575
}
76+
77+
public void erase() {
78+
paths.clear();
79+
colors.clear();
80+
widths.clear();
81+
invalidate();
82+
}
7683
}

app/src/main/java/com/dragosholban/androiddrawing/MainActivity.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.dragosholban.androiddrawing;
22

3+
import android.content.DialogInterface;
34
import android.graphics.drawable.ColorDrawable;
5+
import android.support.v7.app.AlertDialog;
46
import android.support.v7.app.AppCompatActivity;
57
import android.os.Bundle;
68
import android.view.View;
@@ -41,5 +43,28 @@ public void onStopTrackingTouch(SeekBar seekBar) {
4143
public void setColor(View view) {
4244
ColorDrawable buttonColor = (ColorDrawable) view.getBackground();
4345
drawingView.setCurrentColor(buttonColor.getColor());
46+
if (view.getTag() != null && view.getTag().equals("eraser")) {
47+
drawingView.setCurrentWidth(seekBar.getProgress() * 4);
48+
} else {
49+
drawingView.setCurrentWidth(seekBar.getProgress());
50+
}
51+
}
52+
53+
public void deleteDrawing(View view) {
54+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
55+
builder.setMessage("Are you sure you want to erase everything?")
56+
.setTitle("Delete Drawing")
57+
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
58+
public void onClick(DialogInterface dialog, int id) {
59+
drawingView.erase();
60+
}
61+
})
62+
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
63+
public void onClick(DialogInterface dialog, int id) {
64+
65+
}
66+
});
67+
AlertDialog dialog = builder.create();
68+
dialog.show();
4469
}
4570
}

app/src/main/res/layout/activity_main.xml

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@
6262
android:layout_margin="1dp"
6363
android:background="#000000"
6464
android:onClick="setColor"/>
65+
<View
66+
android:layout_width="0dp"
67+
android:layout_height="match_parent"
68+
android:layout_weight="1"
69+
android:layout_margin="1dp"
70+
android:background="#FFFFFF"
71+
android:onClick="setColor"
72+
android:tag="eraser"/>
73+
<Button
74+
android:id="@+id/button"
75+
android:layout_width="0dp"
76+
android:layout_height="wrap_content"
77+
android:layout_weight="1"
78+
android:background="@android:drawable/ic_menu_delete"
79+
android:onClick="deleteDrawing" />
6580
</LinearLayout>
6681
<SeekBar
6782
android:id="@+id/seekBar"

0 commit comments

Comments
 (0)