Displaying Zoom Control: (IN/OUT)
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Adding the image view-->
<ImageView
android:id="@+id/image_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/indiamap" />
<!--Adding the Zoom Controls
within the relative layout-->
<ZoomControls
android:id="@+id/zoom_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp" />
</RelativeLayout>
MainActivity.java
package com.training.zoomcontroldemo;// Java code to implement the zoom
controls
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
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=findViewById(R.id.image_View);
zoomControls=findViewById(R.id.zoom_controls);
zoomControls.setBackgroundColor(Color.BLACK);
zoomControls.show();
// onTouch listener function when the image is clicked
imageView.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent
motionEvent) {
zoomControls.show();
return false;
}
}
);
// This function will be automatically called out,when
// zoom in button is being pressed
zoomControls.setOnZoomInClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
float x=imageView.getScaleX();
float y=imageView.getScaleY();
// setting the new scale
imageView.setScaleX((float)(x+0.5f));
imageView.setScaleY((float)(y+0.5f));
zoomControls.hide();
}
}
);
// This function will be called when
// zoom out button is pressed
zoomControls.setOnZoomOutClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
float x=imageView.getScaleX();
float y=imageView.getScaleY();
if(x==1 && y==1)
{
// the scale will remain same,since
// it is maximum possible zoom out
imageView.setScaleX((float)(x));
imageView.setScaleY((float)(y));
zoomControls.hide();
}
else
{
// setting the new scale
imageView.setScaleX((float)(x-0.5f));
imageView.setScaleY((float)(y-0.5f));
// hiding the zoom controls
zoomControls.hide();
}
}
}
);
}
}
Output: