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

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -85,7 +86,11 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
startActivity(new Intent(getApplicationContext(), SettingsActivity.class)); startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true; return true;
case R.id.clear: 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() { new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override @Override
public void onCancel(AmbilWarnaDialog dialog) { public void onCancel(AmbilWarnaDialog dialog) {
@ -93,7 +98,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
@Override @Override
public void onOk(AmbilWarnaDialog dialog, int pickedColor) { public void onOk(AmbilWarnaDialog dialog, int pickedColor) {
mMyCanvas.clearCanvas(pickedColor); mMyCanvas.setBackgroundColor(pickedColor);
} }
}); });

View File

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

View File

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

View File

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