mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-02-20 05:30:57 +01:00
show the Undo button only if there is something drawn
This commit is contained in:
parent
95d885e3d5
commit
4ff11c3fee
@ -32,7 +32,7 @@ import butterknife.ButterKnife;
|
|||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
import yuku.ambilwarna.AmbilWarnaDialog;
|
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 TAG = MainActivity.class.getSimpleName();
|
||||||
private static final String FOLDER_NAME = "images";
|
private static final String FOLDER_NAME = "images";
|
||||||
private static final String FILE_NAME = "simple-draw.png";
|
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;
|
private static final int STORAGE_PERMISSION = 1;
|
||||||
|
|
||||||
@BindView(R.id.my_canvas) MyCanvas myCanvas;
|
@BindView(R.id.my_canvas) MyCanvas myCanvas;
|
||||||
|
@BindView(R.id.undo) View undoBtn;
|
||||||
@BindView(R.id.color_picker) View colorPicker;
|
@BindView(R.id.color_picker) View colorPicker;
|
||||||
|
|
||||||
private int color;
|
private int color;
|
||||||
@ -50,6 +51,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
myCanvas.setListener(this);
|
||||||
|
|
||||||
setColor(Color.BLACK);
|
setColor(Color.BLACK);
|
||||||
}
|
}
|
||||||
@ -235,4 +237,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
colorPicker.setBackgroundColor(color);
|
colorPicker.setBackgroundColor(color);
|
||||||
myCanvas.setColor(color);
|
myCanvas.setColor(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pathsChanged(int cnt) {
|
||||||
|
undoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ public class MyCanvas extends View {
|
|||||||
private float curY;
|
private float curY;
|
||||||
private float startX;
|
private float startX;
|
||||||
private float startY;
|
private float startY;
|
||||||
|
private PathsChangedListener listener;
|
||||||
|
|
||||||
public MyCanvas(Context context, AttributeSet attrs) {
|
public MyCanvas(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
@ -37,6 +38,11 @@ public class MyCanvas extends View {
|
|||||||
|
|
||||||
paths = new LinkedHashMap<>();
|
paths = new LinkedHashMap<>();
|
||||||
paths.put(path, paint.getColor());
|
paths.put(path, paint.getColor());
|
||||||
|
pathsUpdated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(PathsChangedListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undo() {
|
public void undo() {
|
||||||
@ -49,6 +55,7 @@ public class MyCanvas extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
paths.remove(lastKey);
|
paths.remove(lastKey);
|
||||||
|
pathsUpdated();
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +100,7 @@ public class MyCanvas extends View {
|
|||||||
private void actionUp() {
|
private void actionUp() {
|
||||||
path.lineTo(curX, curY);
|
path.lineTo(curX, curY);
|
||||||
|
|
||||||
// drawing dots on click
|
// draw a dot on click
|
||||||
if (startX == curX && startY == curY) {
|
if (startX == curX && startY == curY) {
|
||||||
path.lineTo(curX, curY + 2);
|
path.lineTo(curX, curY + 2);
|
||||||
path.lineTo(curX + 1, curY + 2);
|
path.lineTo(curX + 1, curY + 2);
|
||||||
@ -101,9 +108,16 @@ public class MyCanvas extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
paths.put(path, paint.getColor());
|
paths.put(path, paint.getColor());
|
||||||
|
pathsUpdated();
|
||||||
path = new Path();
|
path = new Path();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void pathsUpdated() {
|
||||||
|
if (listener != null && paths != null) {
|
||||||
|
listener.pathsChanged(paths.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
final float x = event.getX();
|
final float x = event.getX();
|
||||||
@ -128,4 +142,8 @@ public class MyCanvas extends View {
|
|||||||
invalidate();
|
invalidate();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface PathsChangedListener {
|
||||||
|
void pathsChanged(int cnt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
android:layout_alignParentRight="true"
|
android:layout_alignParentRight="true"
|
||||||
android:layout_below="@id/color_picker"
|
android:layout_below="@id/color_picker"
|
||||||
android:padding="@dimen/icon_padding"
|
android:padding="@dimen/icon_padding"
|
||||||
android:src="@mipmap/undo"/>
|
android:src="@mipmap/undo"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user