show the Undo button only if there is something drawn

This commit is contained in:
tibbi 2016-06-01 20:35:25 +02:00
parent 95d885e3d5
commit 4ff11c3fee
3 changed files with 29 additions and 3 deletions

View File

@ -32,7 +32,7 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements MyCanvas.PathsChangedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String FOLDER_NAME = "images";
private static final String FILE_NAME = "simple-draw.png";
@ -40,6 +40,7 @@ public class MainActivity extends AppCompatActivity {
private static final int STORAGE_PERMISSION = 1;
@BindView(R.id.my_canvas) MyCanvas myCanvas;
@BindView(R.id.undo) View undoBtn;
@BindView(R.id.color_picker) View colorPicker;
private int color;
@ -50,6 +51,7 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
myCanvas.setListener(this);
setColor(Color.BLACK);
}
@ -235,4 +237,9 @@ public class MainActivity extends AppCompatActivity {
colorPicker.setBackgroundColor(color);
myCanvas.setColor(color);
}
@Override
public void pathsChanged(int cnt) {
undoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE);
}
}

View File

@ -22,6 +22,7 @@ public class MyCanvas extends View {
private float curY;
private float startX;
private float startY;
private PathsChangedListener listener;
public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
@ -37,6 +38,11 @@ public class MyCanvas extends View {
paths = new LinkedHashMap<>();
paths.put(path, paint.getColor());
pathsUpdated();
}
public void setListener(PathsChangedListener listener) {
this.listener = listener;
}
public void undo() {
@ -49,6 +55,7 @@ public class MyCanvas extends View {
}
paths.remove(lastKey);
pathsUpdated();
invalidate();
}
@ -93,7 +100,7 @@ public class MyCanvas extends View {
private void actionUp() {
path.lineTo(curX, curY);
// drawing dots on click
// draw a dot on click
if (startX == curX && startY == curY) {
path.lineTo(curX, curY + 2);
path.lineTo(curX + 1, curY + 2);
@ -101,9 +108,16 @@ public class MyCanvas extends View {
}
paths.put(path, paint.getColor());
pathsUpdated();
path = new Path();
}
private void pathsUpdated() {
if (listener != null && paths != null) {
listener.pathsChanged(paths.size());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
@ -128,4 +142,8 @@ public class MyCanvas extends View {
invalidate();
return true;
}
public interface PathsChangedListener {
void pathsChanged(int cnt);
}
}

View File

@ -22,6 +22,7 @@
android:layout_alignParentRight="true"
android:layout_below="@id/color_picker"
android:padding="@dimen/icon_padding"
android:src="@mipmap/undo"/>
android:src="@mipmap/undo"
android:visibility="gone"/>
</RelativeLayout>