create a custom view for the widget resizing frame

This commit is contained in:
tibbi 2022-10-02 20:01:46 +02:00
parent 24a4723bb2
commit 626a553331
2 changed files with 56 additions and 17 deletions

View File

@ -32,10 +32,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var roundedCornerRadius = context.resources.getDimension(R.dimen.activity_margin)
private var textPaint: TextPaint
private var dragShadowCirclePaint: Paint
private var resizeWidgetLinePaint: Paint
private var draggedItem: HomeScreenGridItem? = null
private var resizedWidget: HomeScreenGridItem? = null
private var isFirstDraw = true
private var resizeWidgetFrame: MyAppWidgetResizeFrame
// let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps
private var rowXCoords = ArrayList<Int>(COLUMN_COUNT)
@ -68,12 +68,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
style = Paint.Style.STROKE
}
resizeWidgetLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
strokeWidth = context.resources.getDimension(R.dimen.tiny_margin)
style = Paint.Style.STROKE
}
val sideMargin = context.resources.getDimension(R.dimen.normal_margin).toInt()
sideMargins.apply {
top = context.statusBarHeight
@ -83,6 +77,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
fetchGridItems()
resizeWidgetFrame = MyAppWidgetResizeFrame(context)
}
fun fetchGridItems() {
@ -175,11 +170,21 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
fun widgetLongPressed(item: HomeScreenGridItem) {
resizedWidget = item
redrawGrid()
val widgetView = widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }
removeView(resizeWidgetFrame)
if (widgetView != null) {
val viewX = widgetView.x.toInt()
val viewY = widgetView.y.toInt()
val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
resizeWidgetFrame.updateFrameCoords(frameRect)
addView(resizeWidgetFrame)
}
}
fun hideResizeLines() {
resizedWidget = null
redrawGrid()
removeView(resizeWidgetFrame)
}
private fun addAppIcon() {
@ -550,15 +555,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
}
if (resizedWidget != null) {
val widgetView = widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }
if (widgetView != null) {
val viewX = widgetView.x.toInt()
val viewY = widgetView.y.toInt()
canvas.drawRect(Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height), resizeWidgetLinePaint)
}
}
isFirstDraw = false
}

View File

@ -0,0 +1,43 @@
package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.widget.FrameLayout
import android.widget.RelativeLayout
import com.simplemobiletools.launcher.R
@SuppressLint("ViewConstructor")
class MyAppWidgetResizeFrame(context: Context) : FrameLayout(context) {
private var resizeWidgetLinePaint: Paint
init {
layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
background = ColorDrawable(Color.TRANSPARENT)
resizeWidgetLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
strokeWidth = context.resources.getDimension(R.dimen.tiny_margin)
style = Paint.Style.STROKE
}
}
fun updateFrameCoords(coords: Rect) {
layoutParams.width = coords.right - coords.left
layoutParams.height = coords.bottom - coords.top
x = coords.left.toFloat()
y = coords.top.toFloat()
requestLayout()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (x != 0f || y != 0f) {
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), resizeWidgetLinePaint)
}
}
}