Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Android Program Displaying Zoom Control-Unit 6

The document describes adding zoom controls to an image view in an Android application to allow zooming in and out of an image. It includes XML and Java code to display an image, add zoom controls, and implement zoom functionality when the controls are used.

Uploaded by

Vijay Bande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Android Program Displaying Zoom Control-Unit 6

The document describes adding zoom controls to an image view in an Android application to allow zooming in and out of an image. It includes XML and Java code to display an image, add zoom controls, and implement zoom functionality when the controls are used.

Uploaded by

Vijay Bande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like