update the resize rectangle by drag gestures

This commit is contained in:
tibbi 2022-10-03 22:25:41 +02:00
parent 327656af9a
commit d39348e521
2 changed files with 26 additions and 6 deletions

View File

@ -63,7 +63,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:21a91f2ce7' implementation 'com.github.SimpleMobileTools:Simple-Commons:f74a128e2e'
kapt "androidx.room:room-compiler:2.4.3" kapt "androidx.room:room-compiler:2.4.3"
implementation "androidx.room:room-runtime:2.4.3" implementation "androidx.room:room-runtime:2.4.3"

View File

@ -18,6 +18,7 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
private var resizeWidgetLineDotPaint: Paint private var resizeWidgetLineDotPaint: Paint
private var actionDownCoords = PointF() private var actionDownCoords = PointF()
private var actionDownMS = 0L private var actionDownMS = 0L
private var frameRect = Rect(0, 0, 0, 0)
private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius) private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius)
private val MAX_TOUCH_LINE_DISTANCE = lineDotRadius * 5 // how close we have to be to the widgets side to drag it private val MAX_TOUCH_LINE_DISTANCE = lineDotRadius * 5 // how close we have to be to the widgets side to drag it
var onClickListener: (() -> Unit)? = null var onClickListener: (() -> Unit)? = null
@ -44,10 +45,15 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
} }
fun updateFrameCoords(coords: Rect) { fun updateFrameCoords(coords: Rect) {
layoutParams.width = coords.right - coords.left frameRect = coords
layoutParams.height = coords.bottom - coords.top redrawFrame()
x = coords.left.toFloat() }
y = coords.top.toFloat()
private fun redrawFrame() {
layoutParams.width = frameRect.right - frameRect.left
layoutParams.height = frameRect.bottom - frameRect.top
x = frameRect.left.toFloat()
y = frameRect.top.toFloat()
requestLayout() requestLayout()
} }
@ -61,6 +67,7 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
actionDownCoords.x = event.rawX actionDownCoords.x = event.rawX
actionDownCoords.y = event.rawY actionDownCoords.y = event.rawY
actionDownMS = System.currentTimeMillis() actionDownMS = System.currentTimeMillis()
dragDirection = DRAGGING_NONE
if (event.x < MAX_TOUCH_LINE_DISTANCE) { if (event.x < MAX_TOUCH_LINE_DISTANCE) {
dragDirection = DRAGGING_LEFT dragDirection = DRAGGING_LEFT
} else if (event.y < MAX_TOUCH_LINE_DISTANCE) { } else if (event.y < MAX_TOUCH_LINE_DISTANCE) {
@ -71,6 +78,18 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
dragDirection = DRAGGING_BOTTOM dragDirection = DRAGGING_BOTTOM
} }
} }
MotionEvent.ACTION_MOVE -> {
when (dragDirection) {
DRAGGING_LEFT -> frameRect.left = event.rawX.toInt()
DRAGGING_TOP -> frameRect.top = event.rawY.toInt()
DRAGGING_RIGHT -> frameRect.right = event.rawX.toInt()
DRAGGING_BOTTOM -> frameRect.bottom = event.rawY.toInt()
}
if (dragDirection != DRAGGING_NONE) {
redrawFrame()
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
if (dragDirection == DRAGGING_NONE) { if (dragDirection == DRAGGING_NONE) {
onClickListener?.invoke() onClickListener?.invoke()
@ -81,13 +100,14 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
} }
} }
return super.onTouchEvent(event) return true
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (x != 0f || y != 0f) { if (x != 0f || y != 0f) {
canvas.drawRect(lineDotRadius, lineDotRadius, width.toFloat() - lineDotRadius, height.toFloat() - lineDotRadius, resizeWidgetLinePaint) canvas.drawRect(lineDotRadius, lineDotRadius, width.toFloat() - lineDotRadius, height.toFloat() - lineDotRadius, resizeWidgetLinePaint)
canvas.drawCircle(lineDotRadius, height / 2f, lineDotRadius, resizeWidgetLineDotPaint) canvas.drawCircle(lineDotRadius, height / 2f, lineDotRadius, resizeWidgetLineDotPaint)
canvas.drawCircle(width / 2f, lineDotRadius, lineDotRadius, resizeWidgetLineDotPaint) canvas.drawCircle(width / 2f, lineDotRadius, lineDotRadius, resizeWidgetLineDotPaint)
canvas.drawCircle(width - lineDotRadius, height / 2f, lineDotRadius, resizeWidgetLineDotPaint) canvas.drawCircle(width - lineDotRadius, height / 2f, lineDotRadius, resizeWidgetLineDotPaint)