Android Zoom Controls



Android Zoom Controls



  • Static Zoom Controls Creation in Android.

  • Dynamic Zoom Controls Creation in Android.

  • Zoom Controls for ImageView in Low Level Version.



Static Zoom Controls Creation in Android.

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<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" >
   
   
    <TextView
        android:id="@+id/textView1"
        android:textSize="18dp"
        android:textColor="#4169E1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Static ZoomControls" />

    <ZoomControls
        android:id="@+id/zoomControls1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ZoomControls;

public class MainActivity extends Activity {
   
    ZoomControls zoom;
    ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        zoom = (ZoomControls) findViewById(R.id.zoomControls1);
        img = (ImageView) findViewById(R.id.imageView1);
       
        zoom.setOnZoomInClickListener(new OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            float x = img.getScaleX();
            float y = img.getScaleY();
           
            img.setScaleX((float) (x+1));
            img.setScaleY((float) (y+1));
        }
    });

        zoom.setOnZoomOutClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           

            float x = img.getScaleX();
            float y = img.getScaleY();
           
            img.setScaleX((float) (x-1));
            img.setScaleY((float) (y-1));
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 5 : Our output will be like this :





Dynamic Zoom Controls Creation in Android.

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
   
    <TextView
        android:id="@+id/textView1"
        android:textSize="18dp"
        android:textColor="#4169E1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Dynamic ZoomControls" />
   

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>



Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
   
    RelativeLayout rl;
    ZoomControls zoom;
    ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        rl = (RelativeLayout) findViewById(R.id.rl);
        img = (ImageView) findViewById(R.id.imageView1);
       
        zoom = new ZoomControls(MainActivity.this);
       
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
       
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.bottomMargin = 40;
       
        zoom.setLayoutParams(params);
       
        rl.addView(zoom);
       
        zoom.setOnZoomInClickListener(new OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            float x = img.getScaleX();
            float y = img.getScaleY();
           
            img.setScaleX((float) (x+1));
            img.setScaleY((float) (y+1));
        }
    });
       
        zoom.setOnZoomOutClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            float x = img.getScaleX();
            float y = img.getScaleY();
           
            img.setScaleX((float) (x-1));
            img.setScaleY((float) (y-1));
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 4 : Open AndroidManifest.xml and add following code :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



Step 5 : Our output will be like this :





Zoom Controls for ImageView in Low Level Version.

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

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

</RelativeLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

public class MainActivity extends Activity {
   
    ImageView img;
    ZoomControls zoom;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        img = (ImageView) findViewById(R.id.imageView1);
        zoom = (ZoomControls) findViewById(R.id.zoomControls1);
       
        zoom.setOnZoomInClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            int w = img.getWidth();
            int h = img.getHeight();
           
            RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(w + 10, h +10);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
           
            img.setLayoutParams(params);
        }
    });
       
        zoom.setOnZoomOutClickListener(new View.OnClickListener() {
           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
            int w = img.getWidth();
            int h = img.getHeight();
           
            RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(w - 10, h -10);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
           
            img.setLayoutParams(params);
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :