move the brush size change handling to the activity
This commit is contained in:
parent
657ff0b685
commit
dc5d7f14b7
|
@ -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<Release>().apply {
|
||||
add(Release(18, R.string.release_18))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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>
|
|
@ -44,6 +44,15 @@
|
|||
android:src="@drawable/ic_undo_big"
|
||||
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
|
||||
android:id="@+id/stroke_width_bar"
|
||||
android:layout_width="@dimen/stroke_bar_size"
|
||||
|
@ -51,9 +60,8 @@
|
|||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:max="75"
|
||||
android:max="100"
|
||||
android:padding="@dimen/medium_margin"
|
||||
android:progress="5"
|
||||
android:visibility="gone"/>
|
||||
android:progress="50"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<resources>
|
||||
<dimen name="stroke_bar_size">150dp</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>
|
||||
|
|
Loading…
Reference in New Issue