Search code examples
androidmotioneventzooming

Android: Zoom in-out ImageView with buttons (without MotionEvent)


I need to be able to Zoom an ImageView in/out with two buttons: one for zoom in, another for zoom out. I have successfully implemented zoom in/out with MotionEvent, but have no idea how to implement with buttons. Maybe I could create fake MotionEvents or something similar?


Solution

  • Add these 2 method in TouchImageView Class

    public void zoomIn() { //bind on zoomIn Button
        oldScale = saveScale;
    
        if(saveScale<=maxScale)
        {
            saveScale += .5;
            matrix.setScale(saveScale, saveScale);
            setImageMatrix(matrix);
            invalidate();
    
            // Center the image
            // Center the image
            if(bmHeight>bmWidth)
            {
            redundantXSpace = width - (saveScale * bmWidth);
            redundantXSpace /= 2;
            }
            else 
            {
                redundantYSpace = height - (saveScale * bmHeight) ;
                redundantYSpace /= 2;
            }
    
            matrix.postTranslate(redundantXSpace , redundantYSpace );
            setImageMatrix(matrix);
            invalidate();
        }
    }
    
    public void zoomOut() { 
    
    
     //Bind on zoom Out Button
    
        if(saveScale>=minScale)
        {
            saveScale -= .5;
            matrix.setScale(saveScale, saveScale);
            setImageMatrix(matrix);
            invalidate();
    
            // Center the image
            if(bmHeight>bmWidth)
            {
            redundantXSpace = width - (saveScale * bmWidth);
            redundantXSpace /= 2;
            }
            else 
            {
                redundantYSpace = height - (saveScale * bmHeight) ;
                redundantYSpace /= 2;
            }
            matrix.postTranslate(redundantXSpace , redundantYSpace );
            setImageMatrix(matrix);
            invalidate();
        }
    }
    

    for on Button click protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    setContentView(R.layout.LAYOUT_NAME);
    
    Button zoonIn = (Button)findViewById(R.id.ZOOM_IN_BUTTON_ID);
    Button zoonOut = (Button)findViewById(R.id.ZOOM_OUT_BUTTON_ID);
    
    final TouchImageView touch = (TouchImageView)findViewById(R.id.YOUR_TOUCH_IMAGE_VIEW_)ID);
    
    Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.DRAWABLE_ID);
    
    touch.setImageBitmap(bImage);
    
    touch.setMaxZoom(4f); //change the max level of zoom, default is 3f
    
    
    zoonIn.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            touch.zoomIn();
        }
    });
    
    
    zoonOut.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            touch.zoomOut();
        }
    });
    
    }