首先,我使用下面的网站作为参考,并直接放大表格。但是这里没有任何错误,但是没有用。 https://developer.android.com/training/gestures/scale#kotlin
之后,我尝试在scrollview中制定可缩放的时间表。但是同样的问题又发生了。
注意: 时间表中有一个片段,所以我使用了2个setContentView。
class MainActivity : AppCompatActivity() {
private lateinit var mScaleDetector: ScaleGestureDetector
private var scaleFactor = 1.0f
private lateinit var table : ScrollView
lateinit var gestureDetector :GestureDetector
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_week_timetable)
gestureDetector = GestureDetector(this, GestureListener())
mScaleDetector = ScaleGestureDetector(this, ScaleGestureDetector.SimpleOnScaleGestureListener())
table = findViewById<ScrollView>(R.id.scrollView2)
mScaleDetector = ScaleGestureDetector(this, ScaleListener())
setContentView(R.layout.activity_main)
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
super.dispatchTouchEvent(ev)
mScaleDetector.onTouchEvent(ev)
gestureDetector.onTouchEvent(ev)
return gestureDetector.onTouchEvent(ev)
}
private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(scaleGestureDetector: ScaleGestureDetector): Boolean {
scaleFactor *= scaleGestureDetector.scaleFactor
scaleFactor = max(0.1f, min(scaleFactor, 10.0f))
table.scaleX = scaleFactor
table.scaleY = scaleFactor
return true
}
}
private class GestureListener : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent): Boolean {
return true
}
override fun onDoubleTap(e: MotionEvent): Boolean {
return true;
}
}