Merge pull request #39 from BrianPinsard/master
Implement changing brush size (saves in preferences)
This commit is contained in:
commit
f77edd5e64
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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<MyPath, Integer> mPaths;
|
||||
private Map<MyPath, PaintOptions> 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<MyPath, Integer> entry : mPaths.entrySet()) {
|
||||
mPaint.setColor(entry.getValue());
|
||||
for (Map.Entry<MyPath, PaintOptions> 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<MyPath, Integer> mPaths;
|
||||
Map<MyPath, PaintOptions> 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<MyPath, Integer> entry : mPaths.entrySet()) {
|
||||
for (Map.Entry<MyPath, PaintOptions> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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) { }
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -26,4 +26,15 @@
|
|||
android:src="@mipmap/undo_black"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/stroke_width_bar"
|
||||
android:layout_width="@dimen/stroke_bar_size"
|
||||
android:layout_height="wrap_content"
|
||||
android:progress="5"
|
||||
android:max="75"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="9dp"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
|
|
@ -36,5 +36,31 @@
|
|||
android:clickable="false"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/settings_brush_size_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/settings_padding"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:padding="@dimen/activity_margin">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/settings_brush_size_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerVertical="true"
|
||||
android:paddingLeft="@dimen/settings_padding"
|
||||
android:text="@string/brush_size"/>
|
||||
|
||||
<android.support.v7.widget.SwitchCompat
|
||||
android:id="@+id/settings_brush_size"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:background="@null"
|
||||
android:clickable="false"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Einstellungen</string>
|
||||
<string name="dark_theme">Dunkles Design</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Leeren</string>
|
||||
<string name="change_background">Change background</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Ajustes</string>
|
||||
<string name="dark_theme">Tema oscuro</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Limpiar</string>
|
||||
<string name="change_background">Cambiar fondo</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Paramètres</string>
|
||||
<string name="dark_theme">Thème sombre</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Tout effacer</string>
|
||||
<string name="change_background">Changer le fond</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Impostazioni</string>
|
||||
<string name="dark_theme">Tema scuro</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Clear</string>
|
||||
<string name="change_background">Change background</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">設定</string>
|
||||
<string name="dark_theme">ダークテーマ</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">クリア</string>
|
||||
<string name="change_background">Change background</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Definições</string>
|
||||
<string name="dark_theme">Tema escuro</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Limpar</string>
|
||||
<string name="change_background">Alterar fundo</string>
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Inställningar</string>
|
||||
<string name="dark_theme">Mörkt tema</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Clear</string>
|
||||
<string name="change_background">Change background</string>
|
||||
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
<dimen name="social_padding">8dp</dimen>
|
||||
<dimen name="social_logo">40dp</dimen>
|
||||
<dimen name="settings_padding">8dp</dimen>
|
||||
<dimen name="stroke_bar_size">150dp</dimen>
|
||||
<dimen name="preview_dot_stroke_size">2dp</dimen>
|
||||
<dimen name="preview_dot_offset_y">50dp</dimen>
|
||||
|
||||
<dimen name="normal_text_size">14sp</dimen>
|
||||
</resources>
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<!-- Settings -->
|
||||
<string name="settings">Settings</string>
|
||||
<string name="dark_theme">Dark theme</string>
|
||||
<string name="brush_size">Show brush size tool</string>
|
||||
<string name="clear">Clear</string>
|
||||
<string name="change_background">Change background</string>
|
||||
|
||||
|
|
Loading…
Reference in New Issue