Breaking

Wednesday, 12 December 2018

Zoom Controls Tutorial With Example In Android Studio | Android Solution | Zoom Control

Zoom Controls Tutorial With Example In Android Studio 

In Android, Zoom Controls class show basic arrangement of controls that is utilized for zooming and gives callback to enroll to occasions. Zoom Controls has two catches ZoomIn and ZoomOut which are utilized to control the zooming usefulness.


Zoom Controls code in XML:
<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Important Methods Of Zoom Controls:

Presently we should talk about some normal techniques which are utilized to arrange ZoomControls in our application. 
1. hide(): This technique is utilized to hide the ZoomControls from the screen. At times we have to hide the ZoomControls from the screen with the goal that we utilize this capacity. 
2. show(): This technique is utilized to show the ZoomControls which we hide from the screen by utilizing hide strategy. 
Beneath we show the utilization of hide and show techniques for ZoomControls: 
Stage 1: In this example first in xml record we show ZoomControls with two catches hide and show which are utilized to hide and show the ZoomControls.

xml code of above image UI:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="#0f0"
android:text="Show"
android:textColor="#fff" />

<Button
android:id="@+id/hide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/show"
android:layout_margin="20dp"
android:background="#f00"
android:text="Hide"
android:textColor="#fff" />

</RelativeLayout>
Stage 2: Now in Java Class we use hide() and show() techniques to hide and show Zoom Controls alternative.

/*Add below setContentView() method in Oncreate()*/
        final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls
        Button show = (Button) findViewById(R.id.show); // initiate show Button
        Button hide = (Button) findViewById(R.id.hide); // initiate hide Button
// perform setOnClickListener on show button
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// show a ZoomControls
                simpleZoomControls.show();
            }
        });
// perform setOnClickListener on hide button
        hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// hide a ZoomControls
                simpleZoomControls.hide();

            }
        });
Output:

3. setOnZoomInClickListener(OnClickListenerlistener): This is an audience occasion naturally called when we tap on the Zoom In catch of ZoomControls. In this audience we add the code to zoom in picture. 
Beneath we show the utilization of setOnZoomInClickListener in android.
final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls

// perform  setOnZoomInClickListener event on ZoomControls
simpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add zoom in code here
}
});
4. setOnZoomOutClickListener(OnClickListenerlistener): This is an audience occasion consequently called when we tap on the Zoom Out catch of ZoomControls. In this audience we include the code for zoom out a picture. 
Beneath we show the utilization of setOnZoomOutClickListener in android.
final ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls

// perform  setOnZoomOutClickListener event on ZoomControls
simpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add zoom out code here
}
});
5. setIsZoomInEnabled(boolean isEnabled): This strategy is utilized to empower or debilitate the zoom In catch of ZoomControls. In this strategy we set a Boolean esteem either evident or false. As a matter of course it has genuine esteem yet at some point after a limit of zoom in we have to handicap the zoom in usefulness i.e. after that we didn't require more zoom in. 
Underneath we set the false incentive for setIsZoomInEnabled that handicap zoom in catch of ZoomControls.
ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls
simpleZoomControls.setIsZoomInEnabled(false); // disable zoom in button of ZoomControls
6. setIsZoomOutEnabled(boolean isEnabled): This strategy is utilized to empower or handicap the zoom Out catch of ZoomControls. In this technique we set a Boolean esteem implies genuine or false. As a matter of course it has genuine esteem however at some point after a limit of zoom out we have to cripple the zoom out usefulness implies around then we didn't require more zoom out. 
Underneath we set the false incentive for setIsZoomOutEnabled that debilitate zoom out catch of ZoomControls.
ZoomControls simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls
simpleZoomControls.setIsZoomOutEnabled(false); // disable zoom out button of ZoomControls

Attributes Of Zoom Controls in Android:

Presently allows we talk about some essential ascribes that encourages us to arrange a ZoomControls in our xml document. 
1. id: This credit is utilized to extraordinarily distinguish a ZoomControls.
<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!--  id of ZoomControls used to uniquely identify it -->
2. background: This attribute is used to set the background of a ZoomControls. We can set a color or a drawable in the background of a ZoomControls.
Below we set the black color for the background of ZoomControls.
<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000" />
<!-- set black color in the background of a ZoomControls -->
Setting background in ZoomControls In Java class:
ZoomControls simpleZoomControls = (ZoomControls)findViewById(R.id.simpleZoomControl); // initiate a ZoomControls
simpleZoomControls.setBackgroundColor(Color.BLACK); // set black color in the background of ZoomControls
3. padding: This credit is utilized to set the padding from left, right, best or base side of a ZoomControls . 
* paddingRight: set the padding from the correct side of a ZoomControls. 
* paddingLeft: set the padding from the left half of a ZoomControls. 
* paddingTop: set the padding from the best side of a ZoomControls. 
* paddingBottom: set the padding from the base side of a ZoomControls. 
* Padding: set the padding from the all side's of a ZoomControls. 
Beneath we set the 20dp padding from every one of the sides of a ZoomControls.
<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:padding="20dp"/><!-- 20dp padding from all the sides of a ZoomControls -->

Zoom Controls Example In Android Studio

Below is an example of ZoomControls in which we show an ImageView and a ZoomControls. In this first we hide the ZoomControls from the screen and show it on the touch occasion of picture. We additionally perform setOnZoomInClickListener and setOnZoomOutClickListener occasions for executing zoom in and zoom out usefulness. After zoom in and zoom out the ZoomControls is naturally hide from the screen and re-shown if client tap on the picture. A Toast is shown to show the zoom in and zoom out message on the screen. 
Below you can download finish Android Studio venture code, last yield and well ordered clarification of the example:
ZoomControls Example In Android Studio 

Stage 1: Create another undertaking and name it ZoomControlsExample. 
Stage 2: Open res - > layout - >activity_main.xml (or) main.xml and include following code : 
In this progression we include the code for showing an ImageView and ZoomControls.
<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"
android:background="#000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/image" />

<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

</RelativeLayout>
Stage 3: Open src - > package - > MainActivity.java 
In this progression right off the bat we start the ImageView and ZoomControls and after that hide the ZoomControls from the screen and show it on the touch occasion of picture. 
We additionally perform setOnZoomInClickListener and setOnZoomOutClickListener occasions for executing zoom in and zoom out usefulness. After zoom in and zoom out the ZoomControls is naturally hide from the screen and for re-showing it client need to tap on picture. A Toast is shown to show the zoom in and zoom out message on the screen.
package example.abhiandroid.zoomcontrolsexample;

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

public class MainActivity extends AppCompatActivity {

    ImageView image;
    ZoomControls simpleZoomControls;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image); // initiate a ImageView
        simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl); // initiate a ZoomControls
        simpleZoomControls.hide(); // initially hide ZoomControls from the screen
        // perform setOnTouchListener event on ImageView
        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // show Zoom Controls on touch of image
                simpleZoomControls.show();
                return false;
            }
        });
        // perform setOnZoomInClickListener event on ZoomControls
        simpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calculate current scale x and y value of ImageView
                float x = image.getScaleX();
                float y = image.getScaleY();
                // set increased value of scale x and y to perform zoom in functionality
                image.setScaleX((float) (x + 1));
                image.setScaleY((float) (y + 1));
                // display a toast to show Zoom In Message on Screen
                Toast.makeText(getApplicationContext(),"Zoom In",Toast.LENGTH_SHORT).show();
                // hide the ZoomControls from the screen
                simpleZoomControls.hide();
            }
        });
        // perform setOnZoomOutClickListener event on ZoomControls
        simpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calculate current scale x and y value of ImageView
                float x = image.getScaleX();
                float y = image.getScaleY();
                // set decreased value of scale x and y to perform zoom out functionality
                image.setScaleX((float) (x - 1));
                image.setScaleY((float) (y - 1));
                // display a toast to show Zoom Out Message on Screen
                Toast.makeText(getApplicationContext(),"Zoom Out",Toast.LENGTH_SHORT).show();
                // hide the ZoomControls from the screen
                simpleZoomControls.hide();
            }
        });
    }


}
OutPut :-


No comments:

Post a Comment