This article shows how you can do simple image scaling in Android.

Here I have a simple class that Scales the image.

The Utility class

package pinch_zoom_demo.coderzheaven.com.pinchtozoomdemo;

import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageScaler
        extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    TextView scaleDataMsg;
    ImageView myImage;

    float factor;

    public ImageScaler(TextView v, ImageView iv) {
        super();
        scaleDataMsg = v;
        myImage = iv;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        factor = 1.0f;
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {

        float scaleFactor = detector.getScaleFactor() - 1;
        factor += scaleFactor;
        scaleDataMsg.setText(String.valueOf(scaleFactor)
                + "\n" + String.valueOf(factor));
        myImage.setScaleX(factor);
        myImage.setScaleY(factor);
        return true;
    }
}

Layout

The layout which shows the image for scaling.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/scaleDataMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" />
</LinearLayout>

Set the above xml as layout for the Activity.

Activity

package pinch_zoom_demo.coderzheaven.com.pinchtozoomdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView scaleDataMsg;
    private ImageView myImage;

    private ScaleGestureDetector scaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scaleDataMsg = (TextView) findViewById(R.id.scaleDataMsg);
        myImage = (ImageView) findViewById(R.id.myImage);

        scaleGestureDetector = new ScaleGestureDetector(
                this, new ImageScaler(scaleDataMsg, myImage));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }

}

Please leave your comments at the end of the post.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.