diff --git a/app/src/main/java/com/simplemobiletools/draw/Config.java b/app/src/main/java/com/simplemobiletools/draw/Config.java index 8bbfa34..973047c 100644 --- a/app/src/main/java/com/simplemobiletools/draw/Config.java +++ b/app/src/main/java/com/simplemobiletools/draw/Config.java @@ -31,6 +31,14 @@ public class Config { mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply(); } + public boolean getIsStrokeWidthBarEnabled() { + return mPrefs.getBoolean(Constants.IS_STROKE_WIDTH_BAR_ENABLED, false); + } + + public void setIsStrokeWidthBarEnabled(boolean isStrokeWidthBarEnabled) { + mPrefs.edit().putBoolean(Constants.IS_STROKE_WIDTH_BAR_ENABLED, isStrokeWidthBarEnabled).apply(); + } + public int getBrushColor() { return mPrefs.getInt(Constants.BRUSH_COLOR_KEY, Color.BLACK); } @@ -39,6 +47,14 @@ public class Config { mPrefs.edit().putInt(Constants.BRUSH_COLOR_KEY, color).apply(); } + public float getStrokeWidth() { + return mPrefs.getFloat(Constants.STROKE_WIDTH_KEY, 5.0f); + } + + public void setStrokeWidth(float strokeWidth) { + mPrefs.edit().putFloat(Constants.STROKE_WIDTH_KEY, strokeWidth).apply(); + } + public int getBackgroundColor() { return mPrefs.getInt(Constants.BACKGROUND_COLOR_KEY, Color.WHITE); } diff --git a/app/src/main/java/com/simplemobiletools/draw/Constants.java b/app/src/main/java/com/simplemobiletools/draw/Constants.java index 64f7709..6e3696c 100644 --- a/app/src/main/java/com/simplemobiletools/draw/Constants.java +++ b/app/src/main/java/com/simplemobiletools/draw/Constants.java @@ -4,7 +4,9 @@ public class Constants { // shared preferences public static final String PREFS_KEY = "Draw"; public static final String BRUSH_COLOR_KEY = "brush_color"; + public static final String STROKE_WIDTH_KEY = "stroke_width"; public static final String BACKGROUND_COLOR_KEY = "background_color"; public static final String IS_FIRST_RUN = "is_first_run"; public static final String IS_DARK_THEME = "is_dark_theme"; + public static final String IS_STROKE_WIDTH_BAR_ENABLED = "is_stroke_width_bar_enabled"; } diff --git a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java index 4c28e94..4b1f2bc 100644 --- a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java +++ b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java @@ -1,6 +1,7 @@ package com.simplemobiletools.draw; import android.content.Context; +import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -17,29 +18,32 @@ import java.util.Map; public class MyCanvas extends View { private Paint mPaint; private MyPath mPath; - private Map mPaths; + private Map mPaths; private PathsChangedListener mListener; - private int mColor; + private PaintOptions mPaintOptions; private float mCurX; private float mCurY; private float mStartX; private float mStartY; + private boolean mIsSaving = false; + private boolean mIsStrokeWidthBarEnabled = false; public MyCanvas(Context context, AttributeSet attrs) { super(context, attrs); mPath = new MyPath(); mPaint = new Paint(); - mPaint.setColor(Color.BLACK); + mPaintOptions = new PaintOptions(); + mPaint.setColor(mPaintOptions.color); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); - mPaint.setStrokeWidth(5f); + mPaint.setStrokeWidth(mPaintOptions.strokeWidth); mPaint.setAntiAlias(true); mPaths = new LinkedHashMap<>(); - mPaths.put(mPath, mPaint.getColor()); + mPaths.put(mPath, mPaintOptions); pathsUpdated(); } @@ -62,14 +66,31 @@ public class MyCanvas extends View { } public void setColor(int newColor) { - mColor = newColor; + mPaintOptions.color = newColor; + if (mIsStrokeWidthBarEnabled) { + invalidate(); + } + } + + public void setStrokeWidth(float newStrokeWidth) { + mPaintOptions.strokeWidth = newStrokeWidth; + if (mIsStrokeWidthBarEnabled) { + invalidate(); + } + } + + public void setIsStrokeWidthBarEnabled(boolean isStrokeWidthBarEnabled) { + mIsStrokeWidthBarEnabled = isStrokeWidthBarEnabled; + invalidate(); } public Bitmap getBitmap() { final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); + mIsSaving = true; draw(canvas); + mIsSaving = false; return bitmap; } @@ -77,13 +98,31 @@ public class MyCanvas extends View { protected void onDraw(Canvas canvas) { super.onDraw(canvas); - for (Map.Entry entry : mPaths.entrySet()) { - mPaint.setColor(entry.getValue()); + for (Map.Entry entry : mPaths.entrySet()) { + changePaint(entry.getValue()); canvas.drawPath(entry.getKey(), mPaint); } - mPaint.setColor(mColor); + changePaint(mPaintOptions); canvas.drawPath(mPath, mPaint); + + if (mIsStrokeWidthBarEnabled && !mIsSaving) { + drawPreviewDot(canvas); + } + } + + private void drawPreviewDot(Canvas canvas) { + Resources res = getResources(); + mPaint.setColor(Utils.shouldUseWhite(mPaintOptions.color) ? Color.WHITE : Color.BLACK); + mPaint.setStrokeWidth(mPaintOptions.strokeWidth + res.getDimension(R.dimen.preview_dot_stroke_size)); + canvas.drawPoint(getWidth() / 2, getHeight() - res.getDimension(R.dimen.preview_dot_offset_y), mPaint); + changePaint(mPaintOptions); + canvas.drawPoint(getWidth() / 2, getHeight() - res.getDimension(R.dimen.preview_dot_offset_y), mPaint); + } + + private void changePaint(PaintOptions paintOptions) { + mPaint.setColor(paintOptions.color); + mPaint.setStrokeWidth(paintOptions.strokeWidth); } public void clearCanvas() { @@ -116,9 +155,10 @@ public class MyCanvas extends View { mPath.lineTo(mCurX + 1, mCurY); } - mPaths.put(mPath, mPaint.getColor()); + mPaths.put(mPath, mPaintOptions); pathsUpdated(); mPath = new MyPath(); + mPaintOptions = new PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth); } private void pathsUpdated() { @@ -179,7 +219,7 @@ public class MyCanvas extends View { } static class SavedState extends BaseSavedState { - Map mPaths; + Map mPaths; SavedState(Parcelable superState) { super(superState); @@ -189,9 +229,11 @@ public class MyCanvas extends View { public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(mPaths.size()); - for (Map.Entry entry : mPaths.entrySet()) { + for (Map.Entry entry : mPaths.entrySet()) { out.writeSerializable(entry.getKey()); - out.writeInt(entry.getValue()); + PaintOptions paintOptions = entry.getValue(); + out.writeInt(paintOptions.color); + out.writeFloat(paintOptions.strokeWidth); } } @@ -211,8 +253,8 @@ public class MyCanvas extends View { int size = in.readInt(); for (int i = 0; i < size; i++) { MyPath key = (MyPath) in.readSerializable(); - int value = in.readInt(); - mPaths.put(key, value); + PaintOptions paintOptions = new PaintOptions(in.readInt(), in.readFloat()); + mPaths.put(key, paintOptions); } } } diff --git a/app/src/main/java/com/simplemobiletools/draw/PaintOptions.java b/app/src/main/java/com/simplemobiletools/draw/PaintOptions.java new file mode 100644 index 0000000..4a72bdd --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/draw/PaintOptions.java @@ -0,0 +1,17 @@ +package com.simplemobiletools.draw; + +import android.graphics.Color; + +class PaintOptions { + int color = Color.BLACK; + float strokeWidth = 5f; + + PaintOptions() { + //Empty constructor for instantiating with default values + } + + PaintOptions(int color, float strokeWidth) { + this.color = color; + this.strokeWidth = strokeWidth; + } +} diff --git a/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java index e3775de..ca4069f 100644 --- a/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java @@ -19,6 +19,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageView; +import android.widget.SeekBar; import android.widget.Toast; import com.simplemobiletools.draw.Config; @@ -46,10 +47,12 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange @BindView(R.id.my_canvas) MyCanvas mMyCanvas; @BindView(R.id.undo) View mUndoBtn; @BindView(R.id.color_picker) View mColorPicker; + @BindView(R.id.stroke_width_bar) SeekBar mStrokeWidthBar; private String curFileName; private int color; + private float strokeWidth; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,15 +60,29 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange setContentView(R.layout.activity_main); ButterKnife.bind(this); mMyCanvas.setListener(this); + mStrokeWidthBar.setOnSeekBarChangeListener(onStrokeWidthBarChangeListener); setBackgroundColor(mConfig.getBackgroundColor()); setColor(mConfig.getBrushColor()); + + float savedStrokeWidth = mConfig.getStrokeWidth(); + mMyCanvas.setStrokeWidth(savedStrokeWidth); + mStrokeWidthBar.setProgress((int) savedStrokeWidth); + } + + @Override + protected void onResume() { + super.onResume(); + boolean isStrokeWidthBarEnabled = mConfig.getIsStrokeWidthBarEnabled(); + mStrokeWidthBar.setVisibility(isStrokeWidthBarEnabled ? View.VISIBLE : View.GONE); + mMyCanvas.setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled); } @Override protected void onPause() { super.onPause(); mConfig.setBrushColor(color); + mConfig.setStrokeWidth(strokeWidth); } @Override @@ -290,4 +307,18 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange public void pathsChanged(int cnt) { mUndoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE); } + + SeekBar.OnSeekBarChangeListener onStrokeWidthBarChangeListener = new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + mMyCanvas.setStrokeWidth(progress); + strokeWidth = progress; + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { } + }; } diff --git a/app/src/main/java/com/simplemobiletools/draw/activities/SettingsActivity.java b/app/src/main/java/com/simplemobiletools/draw/activities/SettingsActivity.java index fd9db8d..c4093e5 100644 --- a/app/src/main/java/com/simplemobiletools/draw/activities/SettingsActivity.java +++ b/app/src/main/java/com/simplemobiletools/draw/activities/SettingsActivity.java @@ -13,6 +13,7 @@ import butterknife.OnClick; public class SettingsActivity extends SimpleActivity { @BindView(R.id.settings_dark_theme) SwitchCompat mDarkThemeSwitch; + @BindView(R.id.settings_brush_size) SwitchCompat mBrushSizeSwitch; private static Config mConfig; @@ -23,11 +24,12 @@ public class SettingsActivity extends SimpleActivity { mConfig = Config.newInstance(getApplicationContext()); ButterKnife.bind(this); - setupDarkTheme(); + setupSwitches(); } - private void setupDarkTheme() { + private void setupSwitches() { mDarkThemeSwitch.setChecked(mConfig.getIsDarkTheme()); + mBrushSizeSwitch.setChecked(mConfig.getIsStrokeWidthBarEnabled()); } @OnClick(R.id.settings_dark_theme_holder) @@ -37,6 +39,12 @@ public class SettingsActivity extends SimpleActivity { restartActivity(); } + @OnClick(R.id.settings_brush_size_holder) + public void handleBrushSize() { + mBrushSizeSwitch.setChecked(!mBrushSizeSwitch.isChecked()); + mConfig.setIsStrokeWidthBarEnabled(mBrushSizeSwitch.isChecked()); + } + private void restartActivity() { TaskStackBuilder.create(getApplicationContext()).addNextIntentWithParentStack(getIntent()).startActivities(); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ab8c976..64ab360 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -26,4 +26,15 @@ android:src="@mipmap/undo_black" android:visibility="gone"/> + + diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 7c4901f..e9478be 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -36,5 +36,31 @@ android:clickable="false"/> + + + + + + + + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 2e137ab..ea7dc1d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -16,6 +16,7 @@ Einstellungen Dunkles Design + Show brush size tool Leeren Change background diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index d44a84d..8e625c0 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -16,6 +16,7 @@ Ajustes Tema oscuro + Show brush size tool Limpiar Cambiar fondo diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 5122cdb..c5e8e1e 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -16,6 +16,7 @@ Paramètres Thème sombre + Show brush size tool Tout effacer Changer le fond diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index e739b75..09e5619 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -16,6 +16,7 @@ Impostazioni Tema scuro + Show brush size tool Clear Change background diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 4db33f5..f788bad 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -16,6 +16,7 @@ 設定 ダークテーマ + Show brush size tool クリア Change background diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index 5502e4e..8fd34f5 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -16,6 +16,7 @@ Definições Tema escuro + Show brush size tool Limpar Alterar fundo diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index ee2e161..3408baa 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -16,6 +16,7 @@ Inställningar Mörkt tema + Show brush size tool Clear Change background diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 23c2330..c48feeb 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -5,6 +5,9 @@ 8dp 40dp 8dp + 150dp + 2dp + 50dp 14sp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eca8301..27afbaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,7 @@ Settings Dark theme + Show brush size tool Clear Change background