Below is the custom view thats used to have a single point on the screen i want to draw a line between two Points basically its a app known as Auto Clicker now for single points its as well as it could be i want to add a gesture func in it the functionilty for gesture is also going well but i only want to update the view The PointView class is as follows:
class PointView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
val pointConfig: PointConfig = PointConfig(0, 0, 200)
) : LifecycleView(context, attrs) {
var pointBinding: PointBinding
var number = MutableLiveData(0)
var params = MutableLiveData<WindowManager.LayoutParams>()
var pointConfigPopupWindow: PopupWindow? = null
private var xInView = 0.0f
private var yInView = 0.0f
private var xInScreen = 0.0f
private var yInScreen = 0.0f
private val windowManager by lazy { context.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
private val linePaint: Paint = Paint().apply {
color = android.graphics.Color.WHITE
strokeWidth = 5f
}
init {
pointBinding = PointBinding.inflate(LayoutInflater.from(context), this, true)
number.observe(this) {
pointBinding.tvPoint.text = it.toString()
}
params.observe(this) {
pointConfig.x = it.x
pointConfig.y = it.y
windowManager.updateViewLayout(this, it)
}
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
LogUtils.d("x: ${event?.x},y: ${event?.y},rawX: ${event?.rawX},rawY: ${event?.rawY}, event: $event")
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
xInView = event.x
yInView = event.y
}
MotionEvent.ACTION_MOVE -> {
xInScreen = event.rawX
yInScreen = event.rawY
if (ScreenUtils.isPortrait()) {
yInScreen -= ScreenUtils.getStatusBarHeight()
} else {
xInScreen -= ScreenUtils.getStatusBarHeight()
}
updateViewPosition()
}
MotionEvent.ACTION_UP -> {
if (event.x == xInView && event.y == yInView) {
// Clicked
LogUtils.d("Point clicked.")
// MyWindowManager.showPointConfigPopupWindow(this)
}
}
}
return super.onTouchEvent(event)
}
private fun updateViewPosition() {
params.value?.x = (xInScreen - xInView).toInt()
params.value?.y = (yInScreen - yInView).toInt()
updateViewLayout()
LogUtils.d("x: ${params.value?.x}, y: ${params.value?.y}")
}
fun drawLineTo(endPoint: PointView) {
val path = Path()
path.moveTo(x + width / 2, y + height / 2)
path.lineTo(endPoint.x + endPoint.width / 2, endPoint.y + endPoint.height / 2)
val canvas = Canvas()
canvas.drawPath(path, linePaint)
(this as View).invalidate()
}
fun updateViewLayout() {
params.postValue(params.value)
}
fun centerPoint(): Point {
return Point(
params.value!!.x + width / 2,
params.value!!.y + ScreenUtils.getStatusBarHeight() + height / 2
)
}
fun bright() {
pointBinding.tvPoint.setBackgroundResource(R.drawable.marrowfocused)
pointBinding.tvPoint.setTextColor(context.getColor(R.color.black))
}
fun dark() {
pointBinding.tvPoint.setBackgroundResource(R.drawable.marrow)
pointBinding.tvPoint.setTextColor(context.getColor(R.color.black))
}
}```
and the class its being extending is as follows
open class LifecycleView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs), LifecycleOwner {
private val lifecycleRegistry by lazy { LifecycleRegistry(this) }
override val lifecycle: Lifecycle
get() = lifecycleRegistry
override fun onAttachedToWindow() {
super.onAttachedToWindow()
lifecycleRegistry.currentState = Lifecycle.State.CREATED
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
}
override fun onWindowVisibilityChanged(visibility: Int) {
super.onWindowVisibilityChanged(visibility)
if (visibility == VISIBLE) {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
} else if (visibility == GONE || visibility == INVISIBLE) {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
}
}
the way i'm calling it as follows
private fun drawLineBetweenPoints(startPoint: PointView, endPoint: PointView) {
startPoint.drawLineTo(endPoint)
}
i tried manipulating the drawLine fun as much as i could but didnt get that
one solution can be that i can make a new view for the gesture and check if gesture then infalte that but all i want is to make chagnes in this so that i could get rid of that headache