0

I followed a simple tutorial on how to zoom an image. The problem is i can't drag and scale the image after zooming it. i did read android Dragging and scalling and tried other answers but it doesn't work. below is the code for zooming in the image. Can someone guide me here?

public class MainActivity extends AppCompatActivity{

    ImageView imageView;
    Matrix matrix=new Matrix();
    Float scale=1f;
    ScaleGestureDetector SGD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        imageView=(ImageView)findViewById(R.id.imageView);
        SGD= new ScaleGestureDetector(this,new ScaleListener());

    }

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        SGD.onTouchEvent(event);
                return true;
    }
William Anputra
  • 91
  • 1
  • 1
  • 8
  • 1
    see [this](http://stackoverflow.com/a/21657145/2252830) – pskink Dec 31 '15 at 09:40
  • Great! thank you. it's a little bit confusing for me. Can you please summarize the process for me? – William Anputra Dec 31 '15 at 11:04
  • confusing? all it does is a `Matrix` transforming: all (dragging, scaling and rotating) is done in `Matrix` transformation – pskink Dec 31 '15 at 11:15
  • nevermind, it worked. THANKS AGAIN pskink! – William Anputra Dec 31 '15 at 11:32
  • @pskink this solution you gave me is very effective for image taken from drawable, how do i get the same result if i set the imageView from an URL? i've been trying again and again but still couldn't get the result i want. can you help me again ? – William Anputra Jan 01 '16 at 16:15
  • There is really no difference where you get the Bitmap from – pskink Jan 01 '16 at 16:19
  • @pskink your code worked well for my APP but i wanted to ask you about the zooming part.when i try to zoom in to the point i want, it will zoom to the center of the screen instead to the point i want, i tried playing with the point x and y to get the result i want but it didn't work. can you guide me how to zoom in exactly to the point i want? – William Anputra Jan 03 '16 at 11:05
  • see `Matrix#postScale(float sx, float sy, float px, float py)`, the last two parameters: `px` and `py` define the scale pivot point – pskink Jan 03 '16 at 11:09
  • all right, let me see what i can do – William Anputra Jan 04 '16 at 07:07

0 Answers0