mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
Implement changing brush size (saves in preferences)
This commit is contained in:
@ -39,6 +39,14 @@ public class Config {
|
|||||||
mPrefs.edit().putInt(Constants.BRUSH_COLOR_KEY, color).apply();
|
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() {
|
public int getBackgroundColor() {
|
||||||
return mPrefs.getInt(Constants.BACKGROUND_COLOR_KEY, Color.WHITE);
|
return mPrefs.getInt(Constants.BACKGROUND_COLOR_KEY, Color.WHITE);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ public class Constants {
|
|||||||
// shared preferences
|
// shared preferences
|
||||||
public static final String PREFS_KEY = "Draw";
|
public static final String PREFS_KEY = "Draw";
|
||||||
public static final String BRUSH_COLOR_KEY = "brush_color";
|
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 BACKGROUND_COLOR_KEY = "background_color";
|
||||||
public static final String IS_FIRST_RUN = "is_first_run";
|
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_DARK_THEME = "is_dark_theme";
|
||||||
|
@ -17,29 +17,31 @@ import java.util.Map;
|
|||||||
public class MyCanvas extends View {
|
public class MyCanvas extends View {
|
||||||
private Paint mPaint;
|
private Paint mPaint;
|
||||||
private MyPath mPath;
|
private MyPath mPath;
|
||||||
private Map<MyPath, Integer> mPaths;
|
private Map<MyPath, PaintOptions> mPaths;
|
||||||
private PathsChangedListener mListener;
|
private PathsChangedListener mListener;
|
||||||
|
|
||||||
private int mColor;
|
private PaintOptions mPaintOptions;
|
||||||
private float mCurX;
|
private float mCurX;
|
||||||
private float mCurY;
|
private float mCurY;
|
||||||
private float mStartX;
|
private float mStartX;
|
||||||
private float mStartY;
|
private float mStartY;
|
||||||
|
private boolean mIsSaving = false;
|
||||||
|
|
||||||
public MyCanvas(Context context, AttributeSet attrs) {
|
public MyCanvas(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
mPath = new MyPath();
|
mPath = new MyPath();
|
||||||
mPaint = new Paint();
|
mPaint = new Paint();
|
||||||
mPaint.setColor(Color.BLACK);
|
mPaintOptions = new PaintOptions();
|
||||||
|
mPaint.setColor(mPaintOptions.color);
|
||||||
mPaint.setStyle(Paint.Style.STROKE);
|
mPaint.setStyle(Paint.Style.STROKE);
|
||||||
mPaint.setStrokeJoin(Paint.Join.ROUND);
|
mPaint.setStrokeJoin(Paint.Join.ROUND);
|
||||||
mPaint.setStrokeCap(Paint.Cap.ROUND);
|
mPaint.setStrokeCap(Paint.Cap.ROUND);
|
||||||
mPaint.setStrokeWidth(5f);
|
mPaint.setStrokeWidth(mPaintOptions.strokeWidth);
|
||||||
mPaint.setAntiAlias(true);
|
mPaint.setAntiAlias(true);
|
||||||
|
|
||||||
mPaths = new LinkedHashMap<>();
|
mPaths = new LinkedHashMap<>();
|
||||||
mPaths.put(mPath, mPaint.getColor());
|
mPaths.put(mPath, mPaintOptions);
|
||||||
pathsUpdated();
|
pathsUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,14 +64,22 @@ public class MyCanvas extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(int newColor) {
|
public void setColor(int newColor) {
|
||||||
mColor = newColor;
|
mPaintOptions.color = newColor;
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStrokeWidth(float newStrokeWidth){
|
||||||
|
mPaintOptions.strokeWidth = newStrokeWidth;
|
||||||
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap getBitmap() {
|
public Bitmap getBitmap() {
|
||||||
final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
|
final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
|
||||||
final Canvas canvas = new Canvas(bitmap);
|
final Canvas canvas = new Canvas(bitmap);
|
||||||
canvas.drawColor(Color.WHITE);
|
canvas.drawColor(Color.WHITE);
|
||||||
|
mIsSaving = true;
|
||||||
draw(canvas);
|
draw(canvas);
|
||||||
|
mIsSaving = false;
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,13 +87,30 @@ public class MyCanvas extends View {
|
|||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
for (Map.Entry<MyPath, Integer> entry : mPaths.entrySet()) {
|
for (Map.Entry<MyPath, PaintOptions> entry : mPaths.entrySet()) {
|
||||||
mPaint.setColor(entry.getValue());
|
changePaint(entry.getValue());
|
||||||
canvas.drawPath(entry.getKey(), mPaint);
|
canvas.drawPath(entry.getKey(), mPaint);
|
||||||
}
|
}
|
||||||
|
|
||||||
mPaint.setColor(mColor);
|
changePaint(mPaintOptions);
|
||||||
canvas.drawPath(mPath, mPaint);
|
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() {
|
public void clearCanvas() {
|
||||||
@ -116,9 +143,10 @@ public class MyCanvas extends View {
|
|||||||
mPath.lineTo(mCurX + 1, mCurY);
|
mPath.lineTo(mCurX + 1, mCurY);
|
||||||
}
|
}
|
||||||
|
|
||||||
mPaths.put(mPath, mPaint.getColor());
|
mPaths.put(mPath, mPaintOptions);
|
||||||
pathsUpdated();
|
pathsUpdated();
|
||||||
mPath = new MyPath();
|
mPath = new MyPath();
|
||||||
|
mPaintOptions = new PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pathsUpdated() {
|
private void pathsUpdated() {
|
||||||
@ -179,7 +207,7 @@ public class MyCanvas extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static class SavedState extends BaseSavedState {
|
static class SavedState extends BaseSavedState {
|
||||||
Map<MyPath, Integer> mPaths;
|
Map<MyPath, PaintOptions> mPaths;
|
||||||
|
|
||||||
SavedState(Parcelable superState) {
|
SavedState(Parcelable superState) {
|
||||||
super(superState);
|
super(superState);
|
||||||
@ -189,9 +217,11 @@ public class MyCanvas extends View {
|
|||||||
public void writeToParcel(Parcel out, int flags) {
|
public void writeToParcel(Parcel out, int flags) {
|
||||||
super.writeToParcel(out, flags);
|
super.writeToParcel(out, flags);
|
||||||
out.writeInt(mPaths.size());
|
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.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();
|
int size = in.readInt();
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
MyPath key = (MyPath) in.readSerializable();
|
MyPath key = (MyPath) in.readSerializable();
|
||||||
int value = in.readInt();
|
PaintOptions paintOptions = new PaintOptions(in.readInt(), in.readFloat());
|
||||||
mPaths.put(key, value);
|
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.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
import android.widget.SeekBar;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.simplemobiletools.draw.Config;
|
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.my_canvas) MyCanvas mMyCanvas;
|
||||||
@BindView(R.id.undo) View mUndoBtn;
|
@BindView(R.id.undo) View mUndoBtn;
|
||||||
@BindView(R.id.color_picker) View mColorPicker;
|
@BindView(R.id.color_picker) View mColorPicker;
|
||||||
|
@BindView(R.id.stroke_width_bar) SeekBar mStrokeWidthBar;
|
||||||
|
|
||||||
private String curFileName;
|
private String curFileName;
|
||||||
|
|
||||||
private int color;
|
private int color;
|
||||||
|
private float strokeWidth;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -57,15 +60,21 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
|||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
mMyCanvas.setListener(this);
|
mMyCanvas.setListener(this);
|
||||||
|
mStrokeWidthBar.setOnSeekBarChangeListener(onStrokeWidthBarChangeListener);
|
||||||
|
|
||||||
setBackgroundColor(mConfig.getBackgroundColor());
|
setBackgroundColor(mConfig.getBackgroundColor());
|
||||||
setColor(mConfig.getBrushColor());
|
setColor(mConfig.getBrushColor());
|
||||||
|
|
||||||
|
float savedStrokeWidth = mConfig.getStrokeWidth();
|
||||||
|
mMyCanvas.setStrokeWidth(savedStrokeWidth);
|
||||||
|
mStrokeWidthBar.setProgress((int) savedStrokeWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
mConfig.setBrushColor(color);
|
mConfig.setBrushColor(color);
|
||||||
|
mConfig.setStrokeWidth(strokeWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -290,4 +299,18 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange
|
|||||||
public void pathsChanged(int cnt) {
|
public void pathsChanged(int cnt) {
|
||||||
mUndoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE);
|
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) { }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -26,4 +26,14 @@
|
|||||||
android:src="@mipmap/undo_black"
|
android:src="@mipmap/undo_black"
|
||||||
android:visibility="gone"/>
|
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>
|
</RelativeLayout>
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
<dimen name="social_padding">8dp</dimen>
|
<dimen name="social_padding">8dp</dimen>
|
||||||
<dimen name="social_logo">40dp</dimen>
|
<dimen name="social_logo">40dp</dimen>
|
||||||
<dimen name="settings_padding">8dp</dimen>
|
<dimen name="settings_padding">8dp</dimen>
|
||||||
|
<dimen name="stroke_bar_size">150dp</dimen>
|
||||||
|
|
||||||
<dimen name="normal_text_size">14sp</dimen>
|
<dimen name="normal_text_size">14sp</dimen>
|
||||||
</resources>
|
</resources>
|
||||||
|
Reference in New Issue
Block a user