move the brush size change handling to the activity

This commit is contained in:
tibbi 2018-08-26 20:10:46 +02:00
parent 657ff0b685
commit dc5d7f14b7
5 changed files with 51 additions and 53 deletions

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
@ -44,7 +45,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
private var intentUri: Uri? = null private var intentUri: Uri? = null
private var color = 0 private var color = 0
private var strokeWidth = 0f private var brushSize = 0f
private var isEraserOn = false private var isEraserOn = false
private var isImageCaptureIntent = false private var isImageCaptureIntent = false
private var lastBitmapPath = "" private var lastBitmapPath = ""
@ -60,9 +61,9 @@ class MainActivity : SimpleActivity(), CanvasListener {
setColor(config.brushColor) setColor(config.brushColor)
defaultPath = config.lastSaveFolder defaultPath = config.lastSaveFolder
strokeWidth = config.brushSize brushSize = config.brushSize
my_canvas.setStrokeWidth(strokeWidth) updateBrushSize()
stroke_width_bar.progress = strokeWidth.toInt() stroke_width_bar.progress = brushSize.toInt()
color_picker.setOnClickListener { pickColor() } color_picker.setOnClickListener { pickColor() }
undo.setOnClickListener { my_canvas.undo() } undo.setOnClickListener { my_canvas.undo() }
@ -78,9 +79,9 @@ class MainActivity : SimpleActivity(), CanvasListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val isStrokeWidthBarEnabled = config.showBrushSize val isShowBrushSizeEnabled = config.showBrushSize
stroke_width_bar.beVisibleIf(isStrokeWidthBarEnabled) stroke_width_bar.beVisibleIf(isShowBrushSizeEnabled)
my_canvas.setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled) stroke_width_preview.beVisibleIf(isShowBrushSizeEnabled)
my_canvas.setAllowZooming(config.allowZoomingCanvas) my_canvas.setAllowZooming(config.allowZoomingCanvas)
updateTextColors(main_holder) updateTextColors(main_holder)
if (config.preventPhoneFromSleeping) { if (config.preventPhoneFromSleeping) {
@ -91,7 +92,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
config.brushColor = color config.brushColor = color
config.brushSize = strokeWidth config.brushSize = brushSize
if (config.preventPhoneFromSleeping) { if (config.preventPhoneFromSleeping) {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} }
@ -259,8 +260,8 @@ class MainActivity : SimpleActivity(), CanvasListener {
val oldColor = (my_canvas.background as ColorDrawable).color val oldColor = (my_canvas.background as ColorDrawable).color
ColorPickerDialog(this, oldColor) { wasPositivePressed, color -> ColorPickerDialog(this, oldColor) { wasPositivePressed, color ->
if (wasPositivePressed) { if (wasPositivePressed) {
setBackgroundColor(color)
config.canvasBackgroundColor = color config.canvasBackgroundColor = color
setBackgroundColor(color)
} }
} }
} }
@ -396,6 +397,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
redo.applyColorFilter(contrastColor) redo.applyColorFilter(contrastColor)
my_canvas.updateBackgroundColor(pickedColor) my_canvas.updateBackgroundColor(pickedColor)
defaultExtension = PNG defaultExtension = PNG
getBrushPreviewView().setStroke(getBrushStrokeSize(), contrastColor)
} }
private fun setColor(pickedColor: Int) { private fun setColor(pickedColor: Int) {
@ -404,8 +406,13 @@ class MainActivity : SimpleActivity(), CanvasListener {
my_canvas.setColor(color) my_canvas.setColor(color)
isEraserOn = false isEraserOn = false
updateEraserState() 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) { override fun toggleUndoVisibility(visible: Boolean) {
undo.beVisibleIf(visible) undo.beVisibleIf(visible)
} }
@ -429,8 +436,8 @@ class MainActivity : SimpleActivity(), CanvasListener {
private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener { private var onStrokeWidthBarChangeListener: SeekBar.OnSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
my_canvas.setStrokeWidth(progress.toFloat()) brushSize = progress.toFloat()
strokeWidth = progress.toFloat() updateBrushSize()
} }
override fun onStartTrackingTouch(seekBar: SeekBar) {} override fun onStartTrackingTouch(seekBar: SeekBar) {}
@ -438,6 +445,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {} 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() { private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply { arrayListOf<Release>().apply {
add(Release(18, R.string.release_18)) add(Release(18, R.string.release_18))

View File

@ -11,7 +11,6 @@ import android.view.View
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.R import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.interfaces.CanvasListener 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 mStartX = 0f
private var mStartY = 0f private var mStartY = 0f
private var mIsSaving = false private var mIsSaving = false
private var mIsStrokeWidthBarEnabled = false
private var mAllowZooming = true private var mAllowZooming = true
private var mIsEraserOn = false private var mIsEraserOn = false
private var mWasMultitouch = false private var mWasMultitouch = false
@ -114,9 +112,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
fun setColor(newColor: Int) { fun setColor(newColor: Int) {
mPaintOptions.color = newColor mPaintOptions.color = newColor
if (mIsStrokeWidthBarEnabled) {
invalidate()
}
} }
fun updateBackgroundColor(newColor: Int) { fun updateBackgroundColor(newColor: Int) {
@ -125,16 +120,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
mBackgroundBitmap = null mBackgroundBitmap = null
} }
fun setStrokeWidth(newStrokeWidth: Float) { fun setBrushSize(newBrushSize: Float) {
mPaintOptions.strokeWidth = newStrokeWidth mPaintOptions.strokeWidth = resources.getDimension(R.dimen.full_brush_size) * (newBrushSize / 100f)
if (mIsStrokeWidthBarEnabled) {
invalidate()
}
}
fun setIsStrokeWidthBarEnabled(isStrokeWidthBarEnabled: Boolean) {
mIsStrokeWidthBarEnabled = isStrokeWidthBarEnabled
invalidate()
} }
fun setAllowZooming(allowZooming: Boolean) { fun setAllowZooming(allowZooming: Boolean) {
@ -206,30 +193,9 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
changePaint(mPaintOptions) changePaint(mPaintOptions)
canvas.drawPath(mPath, mPaint) canvas.drawPath(mPath, mPaint)
if (mIsStrokeWidthBarEnabled && !mIsSaving) {
drawPreviewCircle(canvas)
}
canvas.restore() 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) { private fun changePaint(paintOptions: PaintOptions) {
mPaint.color = if (paintOptions.isEraser) mBackgroundColor else paintOptions.color mPaint.color = if (paintOptions.isEraser) mBackgroundColor else paintOptions.color
mPaint.strokeWidth = paintOptions.strokeWidth mPaint.strokeWidth = paintOptions.strokeWidth
@ -299,9 +265,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
actionDown(x, y) actionDown(x, y)
mUndonePaths.clear() mUndonePaths.clear()
mListener?.toggleRedoVisibility(false) mListener?.toggleRedoVisibility(false)
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
if (!mAllowZooming || (!mScaleDetector!!.isInProgress && event.pointerCount == 1 && !mWasMultitouch)) { if (!mAllowZooming || (!mScaleDetector!!.isInProgress && event.pointerCount == 1 && !mWasMultitouch)) {
actionMove(x, y) actionMove(x, y)

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/color_primary"/>
<stroke
android:width="@dimen/preview_dot_stroke_size"
android:color="@color/white"/>
</shape>

View File

@ -44,6 +44,15 @@
android:src="@drawable/ic_undo_big" android:src="@drawable/ic_undo_big"
android:visibility="gone"/> android:visibility="gone"/>
<ImageView
android:id="@+id/stroke_width_preview"
android:layout_width="@dimen/full_brush_size"
android:layout_height="@dimen/full_brush_size"
android:layout_above="@+id/stroke_width_bar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/medium_margin"
android:background="@drawable/circle_background"/>
<com.simplemobiletools.commons.views.MySeekBar <com.simplemobiletools.commons.views.MySeekBar
android:id="@+id/stroke_width_bar" android:id="@+id/stroke_width_bar"
android:layout_width="@dimen/stroke_bar_size" android:layout_width="@dimen/stroke_bar_size"
@ -51,9 +60,8 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp" android:layout_marginBottom="12dp"
android:max="75" android:max="100"
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:progress="5" android:progress="50"/>
android:visibility="gone"/>
</RelativeLayout> </RelativeLayout>

View File

@ -1,5 +1,5 @@
<resources> <resources>
<dimen name="stroke_bar_size">150dp</dimen> <dimen name="stroke_bar_size">150dp</dimen>
<dimen name="preview_dot_stroke_size">1dp</dimen> <dimen name="preview_dot_stroke_size">1dp</dimen>
<dimen name="preview_dot_offset_y">70dp</dimen> <dimen name="full_brush_size">30dp</dimen>
</resources> </resources>