Implement changing brush size (saves in preferences)

This commit is contained in:
Brian Pinsard 2017-01-14 18:44:43 +01:00
parent aabc906321
commit ffc79f5578
7 changed files with 105 additions and 15 deletions

View File

@ -39,6 +39,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);
}

View File

@ -4,6 +4,7 @@ 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";

View File

@ -17,29 +17,31 @@ 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;
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 +64,22 @@ public class MyCanvas extends View {
}
public void setColor(int newColor) {
mColor = newColor;
mPaintOptions.color = newColor;
invalidate();
}
public void setStrokeWidth(float newStrokeWidth){
mPaintOptions.strokeWidth = newStrokeWidth;
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 +87,30 @@ 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(!mIsSaving) {
drawPreviewDot(canvas);
}
}
private void drawPreviewDot(Canvas canvas) {
mPaint.setColor(Utils.shouldUseWhite(mPaintOptions.color)?Color.WHITE:Color.BLACK);
mPaint.setStrokeWidth(100);
canvas.drawPoint(getWidth()/2, getHeight() - 100, mPaint);
changePaint(mPaintOptions);
canvas.drawPoint(getWidth()/2, getHeight() - 100, mPaint);
}
private void changePaint(PaintOptions paintOptions) {
mPaint.setColor(paintOptions.color);
mPaint.setStrokeWidth(paintOptions.strokeWidth);
}
public void clearCanvas() {
@ -116,9 +143,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 +207,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 +217,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 +241,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);
}
}
}

View File

@ -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;
}
}

View File

@ -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,21 @@ 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 onPause() {
super.onPause();
mConfig.setBrushColor(color);
mConfig.setStrokeWidth(strokeWidth);
}
@Override
@ -290,4 +299,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) { }
};
}

View File

@ -26,4 +26,14 @@
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" />
</RelativeLayout>

View File

@ -5,6 +5,7 @@
<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="normal_text_size">14sp</dimen>
</resources>