Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the pinch-zoom event with OnGestureListener in Android?

Tags:

android

How to detect the pinch-zoom event with OnGestureListener in Android?

like image 589
user403015 Avatar asked Apr 18 '11 16:04

user403015


People also ask

How do I add pinch zoom on Android?

Tap anywhere on the screen, except the keyboard or navigation bar. Drag 2 fingers to move around the screen. Pinch with 2 fingers to adjust zoom. To stop magnification, use your magnification shortcut again.

What is pinch zoom in Android?

Pinch 2 or more fingers together or apart to adjust zoom. To zoom temporarily, quickly tap the screen 3 times and hold down your finger on the third tap. Drag your finger to move around the screen. Lift your finger to zoom out.


2 Answers

Take a look at this post on the Android Developers blog: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

The later sections talk about using ScaleGestureDetector with code examples.

like image 133
adamp Avatar answered Oct 07 '22 18:10

adamp


You can achieve this in 3 easy steps:

  1. Create SimpleOnScaleGestureListener class like this:

     class CustomOnScaleGestureListener : 
       ScaleGestureDetector.SimpleOnScaleGestureListener() {
       override fun onScale(detector: ScaleGestureDetector): Boolean {
         val scaleFactor = detector.scaleFactor
         if (scaleFactor > 1) {
             // Zooming Out
         } else {
            // Zooming In
         }
         return true
     }
    
     override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
         return true
     }
    
     override fun onScaleEnd(detector: ScaleGestureDetector) { }
    }
    
  2. Create ScaleGestureDetector object

     scaleGestureDetector = ScaleGestureDetector(this, 
       CustomOnScaleGestureListener())
    
  3. Override onTouchEvent in your activity

     override fun onTouchEvent(event: MotionEvent): Boolean {
         scaleGestureDetector?.onTouchEvent(event)
         return true
     }
    

Full Example:

class MainActivity : AppCompatActivity(){

private lateinit var mBinding: ActivityMainBinding

var scaleGestureDetector: ScaleGestureDetector? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    mBinding = ActivityMainBinding.inflate(layoutInflater)

    setContentView(mBinding.root)

    scaleGestureDetector = ScaleGestureDetector(this, CustomOnScaleGestureListener())

}

override fun onTouchEvent(event: MotionEvent): Boolean {
    scaleGestureDetector?.onTouchEvent(event)
    return true
}

inner class CustomOnScaleGestureListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
    override fun onScale(detector: ScaleGestureDetector): Boolean {
        val scaleFactor = detector.scaleFactor
        if (scaleFactor > 1) {
           // Zooming Out
        } else {
           // Zooming In
        }
        return true
    }

    override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
        return true
    }

    override fun onScaleEnd(detector: ScaleGestureDetector) { }
}


}
like image 20
Simple UX Apps Avatar answered Oct 07 '22 20:10

Simple UX Apps