因為今天比較累,我內容會寫比較少。
binding.resize.setOnClickListener {
if (it.isSelected) {
binding.touchPoint.visibility = View.GONE
binding.leftTopPoint.setOnTouchListener(null)
binding.leftBotPoint.setOnTouchListener(null)
binding.rightTopPoint.setOnTouchListener(null)
binding.rightBotPoint.setOnTouchListener(null)
updateHandler.post(updateRunnable)
} else {
updateHandler.removeCallbacks(updateRunnable)
binding.touchPoint.visibility = View.VISIBLE
binding.leftTopPoint.setOnTouchListener(onFrameResizeListener)
binding.leftBotPoint.setOnTouchListener(onFrameResizeListener)
binding.rightTopPoint.setOnTouchListener(onFrameResizeListener)
binding.rightBotPoint.setOnTouchListener(onFrameResizeListener)
}
binding.recyclerView.touchable = it.isSelected
binding.move.isClickable = it.isSelected
binding.close.isClickable = it.isSelected
binding.back.isClickable = it.isSelected
it.isSelected = !it.isSelected
}
跟Day22中的移動視窗差不多的內容,把其他按鈕的狀態設一設。leftTopPoint、leftBotPoint、rightTopPoint、rightBotPoint是我分別在Layout四個角落放上的圓點ImageView,最後一節的目前畫面能看到。另外開始調整大小時要注意把updateRunnable給取消掉,否則可能會和RecyclerView的畫面更新衝突導致Crash。
private val onFrameResizeListener = View.OnTouchListener { v, motionEvent ->
val params = binding.root.layoutParams as WindowManager.LayoutParams
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
return@OnTouchListener true
}
MotionEvent.ACTION_MOVE -> {
if (motionEvent.historySize < 2) return@OnTouchListener true
var dx = motionEvent.getHistoricalX(1) - motionEvent.getHistoricalX(0)
var dy = motionEvent.getHistoricalY(1) - motionEvent.getHistoricalY(0)
params.x = (params.x + dx).toInt()
params.y = (params.y + dy).toInt()
when (v) {
binding.leftTopPoint -> {
dx = -dx
dy = -dy
}
binding.leftBotPoint -> {
dx = -dx
}
binding.rightTopPoint -> {
dy = -dy
}
}
params.width = (params.width + dx).toInt()
params.height = (params.height + dy).toInt()
windowManager.updateViewLayout(binding.root, params)
return@OnTouchListener true
}
}
return@OnTouchListener false
}
基本上內容也和Day22中移動視窗的部分大同小異,唯一的差別是在移動畫面的同時去修改畫面的大小。當然目前這部分的效果還是很粗糙,可能等後續其他功能都做得差不多再來調整了。