Zoom - pinch with 2 fingers

999 views Asked by At

I have a RelativeLayout with TextView, ImageView, EditText, RadioGroup.

How do I simply enable pinch zoom in/out for the complete screen?

Is there no ways to enable an option in the androidmanifest? Do I have to code everything? Can I consider my screen as a bitmap?

Also I have several activities: do I need to duplicate the code in each activity?

Thanks!

3

There are 3 answers

5
IronRabbit On

Unfortunately there's no an easy way to do a pinch to zoom.

Yes you'll need to code "everything".

But, why do you need this functionalitie in this particular case ? Pinch to zoom is use when you have a big image, or any surface where your user will need to move on.

What you describe looks like a form, why your user could use a zoom ?

Yan

2
Rohit On

You do not need to add any special permission to Manifest file

You try to use following code for zoom...

public class MainActivity extends Activity {
    private ImageView imageView;
    private ScaleGestureDetector scaleGestureDetector;
    private Matrix matrix = new Matrix();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView)findViewById(R.id.imageView);
        scaleGestureDetector = new ScaleGestureDetector(this,new ScaleListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        scaleGestureDetector.onTouchEvent(ev);
        return true;
    }

    private class ScaleListener extends ScaleGestureDetector.
            SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scaleFactor = detector.getScaleFactor();
            scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));
            matrix.setScale(scaleFactor, scaleFactor);
            imageView.setImageMatrix(matrix);
            return true;
        }
    }
}
4
Android Noob On

First of all create a class with name TouchImageView in any package for e.e. com.example.pintchzoomdemo and then copy paste the code given here Then all you need to do is use is below line for everywhere you need the imageView to be pintch zoomed like

<com.example.pintchzoomdemo.TouchImageView
                android:id="@+id/ImageView01"
                android:layout_width="1020dp"
                android:layout_height="690dp"/>

Thats it wherever you need image to be pitch zoomed use packagename.TouchImageView instead of ImageView and thats it you dont have to do anything else