Zoom In/Out  Example

Zoom tool is used to zoom in picture or picture out. In this first we hide the ZoomControls from the screen and show it on the touch event of image. We also perform setOnZoomInClickListener and setOnZoomOutClickListener events for implementing zoom in and zoom out functionality. After zoom in and zoom out the ZoomControls is automatically hide from the screen and re-shown if user click on the image. A toast is displayed to show the zoom in and zoom out message on the screen.

actvity_main.xml

This xml shows the code for displaying a ImageView and ZoomControls.

 




    

    



MainActivity.java

This activity first we initiate the ImageView and ZoomControls and then hide the ZoomControls from the screen and show it on the touch event of image. We also perform setOnZoomInClickListener and setOnZoomOutClickListener events for implementing zoom in and zoom out functionality

package com.example.icowboysradio.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ZoomControls;

public class MainActivity extends AppCompatActivity{

    ImageView imageView;
    ZoomControls zoomControls;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        zoomControls = (ZoomControls) findViewById(R.id.zoomControl);
        zoomControls.hide();
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                zoomControls.show();
                return false;
            }
        });
        zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float x = imageView.getScaleX();
                float y = imageView.getScaleY();
                imageView.setScaleX((float) (x + 1));
                imageView.setScaleY((float) (y + 1));
                Toast.makeText(getApplicationContext(),"Picture In Zoom",Toast.LENGTH_SHORT).show();
                zoomControls.hide();
            }
        });
        zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float x = imageView.getScaleX();
                float y = imageView.getScaleY();
                imageView.setScaleX((float) (x - 1));
                imageView.setScaleY((float) (y - 1));
                Toast.makeText(getApplicationContext(),"Picture Out Zoom",Toast.LENGTH_SHORT).show();
                zoomControls.hide();
            }
        });
    }

}