ZoomControls Android

 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 :
 
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.activity_main, menu);
        return true;
    }
}
 
 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 :
 
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.activity_main, menu);
        return true;
    }
}
 
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s