Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Android Zoom Controls in Android studio

Step 1: Create a new project and name it ZoomControlsApp.

Step 2: Open res -> layout ->content_main and add following code 
In this step we add the code for displaying a ImageView and ZoomControls.




Add image into drawable folder

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.mukesh.zoombuttondemo.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="Zoom Control Button"
android:id="@+id/textView2"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Zoom Control Button"
android:id="@+id/textView2"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="300dp"     
android:layout_height="400dp"       
android:id="@+id/imageView2"        
android:layout_centerHorizontal="true"      
android:layout_marginTop="10dp"       
android:src="@drawable/image"/>

<ZoomControls       
  android:layout_width="wrap_content"     
  android:layout_height="wrap_content"     
  android:id="@+id/zoomControls1"      
  android:layout_below="@id/imageView2"      
  android:layout_centerVertical="true"      
  android:layout_centerHorizontal="true" />

</RelativeLayout>




Step 3: Open Src/Java  -> package -> MainActivity.java



package com.example.mukesh.zoombuttondemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

public class Main1Activity extends AppCompatActivity {
    ZoomControls zoom1;
    ImageView img1;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        zoom1=(ZoomControls)findViewById(R.id.zoomControls1);
        img1=(ImageView)findViewById(R.id.imageView2);

        zoom1.setOnZoomInClickListener(new View.OnClickListener() {
            @Override          
              public void onClick(View v) {
                float x=img1.getScaleX();
                float y=img1.getScaleY();

                img1.setScaleY(x+1);
                img1.setScaleY(y+1);
            }
        });

        zoom1.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override           
              public void onClick(View v) {
                float x=img1.getScaleX();
                float y=img1.getScaleY();
                img1.setScaleY(x-1);
                img1.setScaleY(y-1);
            }
        });

    }

}

No comments:

Post a Comment