java – pinch zoom android

Question:

Guys, my problem is the following. I need to enlarge an ImageView with that well-known pinch movement, but I'm already on the third day searching the internet for how to do it and hitting my head, but the most I got was a code from a tutorial that only enlarges the image, not allowing the movement of the enlarged image it only enlarges at a fixed point and stays. I'll be putting the code and would like to know how to implement the image movement or if someone can help, put some code right, because as I said I hit my head too much and nothing came out here lol. Hugs.

public class tela87 extends SherlockFragmentActivity {

    private ImageView img;
    private Matrix matrix = new Matrix();
    private float scale = 1f;
    private ScaleGestureDetector SGD;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tela835gavetti);
        img = (ImageView)findViewById(R.id.imageView1);
        SGD = new ScaleGestureDetector(this,new ScaleListener());
    }

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

    private class ScaleListener extends ScaleGestureDetector.
            SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.1f, Math.min(scale, 5.0f));
            matrix.setScale(scale, scale);
            img.setImageMatrix(matrix);
            return true;
        }
    }
}

Answer:

Try using TouchImageView , it supports pinch zoom, double tap and other features.

To implement it, just reference it in your layout:

<br.com.seupacote.TouchImageView
    android:id="@+id/touch_img_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:scaleType="centerCrop"/>
Scroll to Top