From dc5d7f14b71c9885a457dba01e2d4f3a94e6f6b8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 26 Aug 2018 20:10:46 +0200 Subject: [PATCH] move the brush size change handling to the activity --- .../draw/activities/MainActivity.kt | 35 +++++++++++----- .../simplemobiletools/draw/views/MyCanvas.kt | 40 +------------------ .../main/res/drawable/circle_background.xml | 13 ++++++ app/src/main/res/layout/activity_main.xml | 14 +++++-- app/src/main/res/values/dimens.xml | 2 +- 5 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 app/src/main/res/drawable/circle_background.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt index 85d2872..3d83f02 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Intent import android.graphics.Bitmap import android.graphics.drawable.ColorDrawable +import android.graphics.drawable.GradientDrawable import android.net.Uri import android.os.Bundle import android.provider.MediaStore @@ -44,7 +45,7 @@ class MainActivity : SimpleActivity(), CanvasListener { private var intentUri: Uri? = null private var color = 0 - private var strokeWidth = 0f + private var brushSize = 0f private var isEraserOn = false private var isImageCaptureIntent = false private var lastBitmapPath = "" @@ -60,9 +61,9 @@ class MainActivity : SimpleActivity(), CanvasListener { setColor(config.brushColor) defaultPath = config.lastSaveFolder - strokeWidth = config.brushSize - my_canvas.setStrokeWidth(strokeWidth) - stroke_width_bar.progress = strokeWidth.toInt() + brushSize = config.brushSize + updateBrushSize() + stroke_width_bar.progress = brushSize.toInt() color_picker.setOnClickListener { pickColor() } undo.setOnClickListener { my_canvas.undo() } @@ -78,9 +79,9 @@ class MainActivity : SimpleActivity(), CanvasListener { override fun onResume() { super.onResume() - val isStrokeWidthBarEnabled = config.showBrushSize - stroke_width_bar.beVisibleIf(isStrokeWidthBarEnabled) - my_canvas.setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled) + val isShowBrushSizeEnabled = config.showBrushSize + stroke_width_bar.beVisibleIf(isShowBrushSizeEnabled) + stroke_width_preview.beVisibleIf(isShowBrushSizeEnabled) my_canvas.setAllowZooming(config.allowZoomingCanvas) updateTextColors(main_holder) if (config.preventPhoneFromSleeping) { @@ -91,7 +92,7 @@ class MainActivity : SimpleActivity(), CanvasListener { override fun onPause() { super.onPause() config.brushColor = color - config.brushSize = strokeWidth + config.brushSize = brushSize if (config.preventPhoneFromSleeping) { window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } @@ -259,8 +260,8 @@ class MainActivity : SimpleActivity(), CanvasListener { val oldColor = (my_canvas.background as ColorDrawable).color ColorPickerDialog(this, oldColor) { wasPositivePressed, color -> if (wasPositivePressed) { - setBackgroundColor(color) config.canvasBackgroundColor = color + setBackgroundColor(color) } } } @@ -396,6 +397,7 @@ class MainActivity : SimpleActivity(), CanvasListener { redo.applyColorFilter(contrastColor) my_canvas.updateBackgroundColor(pickedColor) defaultExtension = PNG + getBrushPreviewView().setStroke(getBrushStrokeSize(), contrastColor) } private fun setColor(pickedColor: Int) { @@ -404,8 +406,13 @@ class MainActivity : SimpleActivity(), CanvasListener { my_canvas.setColor(color) isEraserOn = false updateEraserState() + getBrushPreviewView().setColor(color) } + private fun getBrushPreviewView() = stroke_width_preview.background as GradientDrawable + + private fun getBrushStrokeSize() = resources.getDimension(R.dimen.preview_dot_stroke_size).toInt() + override fun toggleUndoVisibility(visible: Boolean) { undo.beVisibleIf(visible) } @@ -429,8 +436,8 @@ class MainActivity : SimpleActivity(), CanvasListener { private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { - my_canvas.setStrokeWidth(progress.toFloat()) - strokeWidth = progress.toFloat() + brushSize = progress.toFloat() + updateBrushSize() } override fun onStartTrackingTouch(seekBar: SeekBar) {} @@ -438,6 +445,12 @@ class MainActivity : SimpleActivity(), CanvasListener { override fun onStopTrackingTouch(seekBar: SeekBar) {} } + private fun updateBrushSize() { + my_canvas.setBrushSize(brushSize) + stroke_width_preview.scaleX = brushSize / 100f + stroke_width_preview.scaleY = brushSize / 100f + } + private fun checkWhatsNewDialog() { arrayListOf().apply { add(Release(18, R.string.release_18)) diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/views/MyCanvas.kt b/app/src/main/kotlin/com/simplemobiletools/draw/views/MyCanvas.kt index 69da4c3..f5fd1d7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/views/MyCanvas.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/views/MyCanvas.kt @@ -11,7 +11,6 @@ import android.view.View import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.request.RequestOptions -import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.draw.R import com.simplemobiletools.draw.interfaces.CanvasListener @@ -41,7 +40,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { private var mStartX = 0f private var mStartY = 0f private var mIsSaving = false - private var mIsStrokeWidthBarEnabled = false private var mAllowZooming = true private var mIsEraserOn = false private var mWasMultitouch = false @@ -114,9 +112,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { fun setColor(newColor: Int) { mPaintOptions.color = newColor - if (mIsStrokeWidthBarEnabled) { - invalidate() - } } fun updateBackgroundColor(newColor: Int) { @@ -125,16 +120,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { mBackgroundBitmap = null } - fun setStrokeWidth(newStrokeWidth: Float) { - mPaintOptions.strokeWidth = newStrokeWidth - if (mIsStrokeWidthBarEnabled) { - invalidate() - } - } - - fun setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled: Boolean) { - mIsStrokeWidthBarEnabled = isStrokeWidthBarEnabled - invalidate() + fun setBrushSize(newBrushSize: Float) { + mPaintOptions.strokeWidth = resources.getDimension(R.dimen.full_brush_size) * (newBrushSize / 100f) } fun setAllowZooming(allowZooming: Boolean) { @@ -206,30 +193,9 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { changePaint(mPaintOptions) canvas.drawPath(mPath, mPaint) - - if (mIsStrokeWidthBarEnabled && !mIsSaving) { - drawPreviewCircle(canvas) - } - canvas.restore() } - private fun drawPreviewCircle(canvas: Canvas) { - val res = resources - mPaint.style = Paint.Style.FILL - - var y = height - res.getDimension(R.dimen.preview_dot_offset_y) - canvas.drawCircle((width / 2).toFloat(), y, mPaintOptions.strokeWidth / 2, mPaint) - mPaint.style = Paint.Style.STROKE - mPaint.color = if (mPaintOptions.isEraser) mBackgroundColor.getContrastColor() else mPaintOptions.color.getContrastColor() - mPaint.strokeWidth = res.getDimension(R.dimen.preview_dot_stroke_size) - - y = height - res.getDimension(R.dimen.preview_dot_offset_y) - val radius = (mPaintOptions.strokeWidth + res.getDimension(R.dimen.preview_dot_stroke_size)) / 2 - canvas.drawCircle((width / 2).toFloat(), y, radius, mPaint) - changePaint(mPaintOptions) - } - private fun changePaint(paintOptions: PaintOptions) { mPaint.color = if (paintOptions.isEraser) mBackgroundColor else paintOptions.color mPaint.strokeWidth = paintOptions.strokeWidth @@ -299,9 +265,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { actionDown(x, y) mUndonePaths.clear() mListener?.toggleRedoVisibility(false) - } - MotionEvent.ACTION_MOVE -> { if (!mAllowZooming || (!mScaleDetector!!.isInProgress && event.pointerCount == 1 && !mWasMultitouch)) { actionMove(x, y) diff --git a/app/src/main/res/drawable/circle_background.xml b/app/src/main/res/drawable/circle_background.xml new file mode 100644 index 0000000..5ef6967 --- /dev/null +++ b/app/src/main/res/drawable/circle_background.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b9be9b8..b40b63b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -44,6 +44,15 @@ android:src="@drawable/ic_undo_big" android:visibility="gone"/> + + + android:progress="50"/> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index f455cfd..83e447a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,5 +1,5 @@ 150dp 1dp - 70dp + 30dp