Separated background color functionality from Clear action

This commit is contained in:
Lonami Exo 2016-12-04 17:40:13 +01:00
parent 9a90f8db00
commit f8bd541f9a
5 changed files with 21 additions and 25 deletions

View File

@ -14,8 +14,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
public class MyCanvas extends View {
private Paint mBackgroundPaint;
private Paint mPaint;
private Path mPath;
private Map<Path, Integer> mPaths;
@ -30,10 +28,6 @@ public class MyCanvas extends View {
public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(Color.WHITE);
mBackgroundPaint.setStyle(Paint.Style.FILL);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
@ -66,10 +60,6 @@ public class MyCanvas extends View {
invalidate();
}
public int getBackgroundColor() {
return mBackgroundPaint.getColor();
}
public void setColor(int newColor) {
mColor = newColor;
}
@ -86,9 +76,6 @@ public class MyCanvas extends View {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Clear canvas background
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
for (Map.Entry<Path, Integer> entry : mPaths.entrySet()) {
mPaint.setColor(entry.getValue());
canvas.drawPath(entry.getKey(), mPaint);
@ -98,9 +85,7 @@ public class MyCanvas extends View {
canvas.drawPath(mPath, mPaint);
}
public void clearCanvas(int color) {
mBackgroundPaint.setColor(color);
public void clearCanvas() {
mPath.reset();
mPaths.clear();
pathsUpdated();

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
@ -85,17 +86,21 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.clear:
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mMyCanvas.getBackgroundColor(),
mMyCanvas.clearCanvas();
return true;
case R.id.change_background:
int oldColor = ((ColorDrawable)mMyCanvas.getBackground()).getColor();
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, oldColor,
new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int pickedColor) {
mMyCanvas.clearCanvas(pickedColor);
}
});
@Override
public void onOk(AmbilWarnaDialog dialog, int pickedColor) {
mMyCanvas.setBackgroundColor(pickedColor);
}
});
dialog.show();
return true;

View File

@ -15,6 +15,10 @@
android:id="@+id/clear"
android:title="@string/clear"
app:showAsAction="never"/>
<item
android:id="@+id/change_background"
android:title="@string/change_background"
app:showAsAction="never"/>
<item
android:id="@+id/settings"
android:title="@string/settings"

View File

@ -17,6 +17,7 @@
<string name="settings">Ajustes</string>
<string name="dark_theme">Tema oscuro</string>
<string name="clear">Limpiar</string>
<string name="change_background">Cambiar fondo</string>
<!-- About -->
<string name="about">Acerca de</string>

View File

@ -17,6 +17,7 @@
<string name="settings">Settings</string>
<string name="dark_theme">Dark theme</string>
<string name="clear">Clear</string>
<string name="change_background">Change background</string>
<!-- About -->
<string name="about">About</string>