package com.appli.camera;


import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;

import com.appli.patoche.R;

//https://javapapers.com/android/android-pinch-zoom/

// https://medium.com/@ali.muzaffar/android-detecting-a-pinch-gesture-64a0a0ed4b41

// https://androidcookbook.com/Recipe.seam?recipeId=2273

// https://androidcookbook.com/Recipe.seam?recipeId=3071

public class pintch02 extends Activity
{

    TextView textMsg;
    ImageView myImage;

    private ScaleGestureDetector scaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pintch02_layout);

        textMsg = (TextView)findViewById(R.id.msg);
        myImage = (ImageView)findViewById(R.id.myimage);

        scaleGestureDetector = new ScaleGestureDetector( this, new MySimpleOnScaleGestureListener(textMsg, myImage));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        scaleGestureDetector.onTouchEvent(event);
        return true;
        //return super.onTouchEvent(event);
    }

    private class MySimpleOnScaleGestureListener  extends ScaleGestureDetector.SimpleOnScaleGestureListener
    {

        TextView viewMessage;
        ImageView viewMyImage;

        float factor;

        public MySimpleOnScaleGestureListener(TextView v, ImageView iv)
        {
            super();
            viewMessage = v;
            viewMyImage = iv;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector)
        {
            factor = 1.0f;
            return true;
            //return super.onScaleBegin(detector);
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector)
        {

            float scaleFactor = detector.getScaleFactor() - 1;
            factor += scaleFactor;
            viewMessage.setText(String.valueOf(scaleFactor) + "\n" + String.valueOf(factor));
            viewMyImage.setScaleX(factor);
            viewMyImage.setScaleY(factor);
            return true;
            //return super.onScale(detector);
        }
    }
}
