We will demonstrate multi-touch with the common pinch-to-zoom gesture.
The following screen-shot shows the icon zoomed in and out using the application created :
Zoom in
Zoom out
Let's get started!
1. Create a new project.
2. Add an imageview widget to your view (you can use any image of your choice).
3. Create a block and name it (zoom_in_out).
4. Add this code to your block.
}
private ScaleGestureDetector mScaleGestureDetector;
private float mScaleFactor = 1.0f;
public boolean onTouchEvent(MotionEvent motionEvent) {
mScaleGestureDetector.onTouchEvent(motionEvent);
return true;
}
{
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector){mScaleFactor *= scaleGestureDetector.getScaleFactor();
mScaleFactor = Math.max(0.1f,Math.min(mScaleFactor, 10.0f));
imageview1.setScaleX(mScaleFactor);
imageview1.setScaleY(mScaleFactor);return true; }
}
{
5. Add this code to your OnCreate event.
mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
6. Save and Run your project.
Watch video:- https://youtu.be/3zL5BCiR0jE



Comments
Post a Comment