问题描述
我正在尝试使用具有辅助功能的手势描述来连续向下滚动。这是我的向下滑动代码
val displayMetrics = resources.displayMetrics
val height : Int = displayMetrics.heightPixels;
val top : Float = height*0.25.toFloat()
val bottom :Float = height *0.75.toFloat()
val swipePath = Path()
swipePath.moveto(top,bottom)
swipePath.lineto(top,top)
val gestureBuilder = GestureDescription.Builder()
gestureBuilder.addstroke(GestureDescription.strokeDescription(swipePath,1000))
dispatchGesture(gestureBuilder.build(),null,null)
请帮助。
解决方法
path.moveTo()
和path.lineTo()
都接受两个参数,分别是x和y坐标,并且要垂直滑动起点和终点的x坐标应该相同。您从高度而不是宽度获取x坐标,这可能还会产生其他问题,例如屏幕外的点。所以我修改了一点代码
val displayMetrics = resources.displayMetrics
val height : Int = displayMetrics.heightPixels
val xCenter: Float = displayMetrics.widthPixels.toFloat()/2
val top : Float = height*0.25.toFloat()
val bottom :Float = height *0.75.toFloat()
val swipePath = Path()
swipePath.moveTo(xCenter,bottom)
swipePath.lineTo(xCenter,top)
val gestureBuilder = GestureDescription.Builder()
gestureBuilder.addStroke(GestureDescription.StrokeDescription(swipePath,1000))
dispatchGesture(gestureBuilder.build(),null,null)