0

I am trying to add a zoom option for the images, that I had bought to an image view. Actually, the image view is working fine and code is running without any errors or issues. ut during zoom on my image view, it is not zooming. My code is as follows.

ZoomInZoomOut.java

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ZoomInZoomOut extends Activity implements OnTouchListener
{
    private static final String TAG = "Touch";
    @SuppressWarnings("unused")
    private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;

    // These matrices will be used to scale points of the image
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    // The 3 states (events) which the user is trying to perform
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // these PointF objects are used to record the point(s) the user is touching
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen_image);
        ImageView view = (ImageView) findViewById(R.id.imgFullscreen);
        view.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        ImageView view = (ImageView) v;
        view.setScaleType(ImageView.ScaleType.MATRIX);
        float scale;

        dumpEvent(event);
        // Handle touch events here...

        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:   // first finger down only
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG"); // write to LogCat
                mode = DRAG;
                break;

            case MotionEvent.ACTION_UP: // first finger lifted

            case MotionEvent.ACTION_POINTER_UP: // second finger lifted

                mode = NONE;
                Log.d(TAG, "mode=NONE");
                break;

            case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 5f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM");
                }
                break;

            case MotionEvent.ACTION_MOVE:

                if (mode == DRAG)
                {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                }
                else if (mode == ZOOM)
                {
                    // pinch zooming
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 5f)
                    {
                        matrix.set(savedMatrix);
                        scale = newDist / oldDist; // setting the scaling of the
                        // matrix...if scale > 1 means
                        // zoom in...if scale < 1 means
                        // zoom out
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
        }

        view.setImageMatrix(matrix); // display the transformation on screen

        return true; // indicate event was handled
    }

    /*
     * --------------------------------------------------------------------------
     * Method: spacing Parameters: MotionEvent Returns: float Description:
     * checks the spacing between the two fingers on touch
     * ----------------------------------------------------
     */

    private float spacing(MotionEvent event)
    {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    /*
     * --------------------------------------------------------------------------
     * Method: midPoint Parameters: PointF object, MotionEvent Returns: void
     * Description: calculates the midpoint between the two fingers
     * ------------------------------------------------------------
     */

    private void midPoint(PointF point, MotionEvent event)
    {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

    /** Show an event in the LogCat view, for debugging */
    private void dumpEvent(MotionEvent event)
    {
        String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE","POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
        StringBuilder sb = new StringBuilder();
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;
        sb.append("event ACTION_").append(names[actionCode]);

        if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
        {
            sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
            sb.append(")");
        }

        sb.append("[");
        for (int i = 0; i < event.getPointerCount(); i++)
        {
            sb.append("#").append(i);
            sb.append("(pid ").append(event.getPointerId(i));
            sb.append(")=").append((int) event.getX(i));
            sb.append(",").append((int) event.getY(i));
            if (i + 1 < event.getPointerCount())
                sb.append(";");
        }

        sb.append("]");
        Log.d("Touch Events ---------", sb.toString());
    }

Also the activity_fullscreen_image.xml is as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black" >

    <ProgressBar
        android:id="@+id/pbLoader"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
    </ProgressBar>
    <com.google.android.gms.ads.AdView

        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="false"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
    >
    </com.google.android.gms.ads.AdView>


    <!-- Scroll view for fullscreen preview -->

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal" >




        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1">




            <ImageView
                android:id="@+id/imgFullscreen"
                android:layout_width="384dp"
                android:layout_height="288dp"

                android:layout_gravity="center_vertical" />
    <!--  android:scaleType="fitXY" /> -->
</LinearLayout>
</HorizontalScrollView>

    <!-- Set as Set wallpaper button -->

    <!-- Set as Share button -->

    <!-- Download wallpaper button -->

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="28dp">

        <LinearLayout
            android:id="@+id/btShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:background="@drawable/btn_rounded_corner"
            android:orientation="horizontal"
            android:layout_alignWithParentIfMissing="false"


            android:baselineAligned="false"
            android:layout_alignTop="@+id/llSetWallpaper"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left|bottom">

            <ImageView
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:src="@drawable/common_signin_btn_icon_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:text="@string/Share"
                android:textColor="@color/white"
                android:textSize="18dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llSetWallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:background="@drawable/btn_rounded_corner"
            android:orientation="horizontal"
            android:layout_alignTop="@+id/llDownloadWallpaper"
            android:layout_alignRight="@+id/pbLoader"
            android:layout_alignParentBottom="false"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|bottom">

            <ImageView
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:src="@drawable/ico_apply" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:text="@string/set_wallpaper"
                android:textColor="@color/white"
                android:textSize="18dp" />
        </LinearLayout>

    </FrameLayout>

    <LinearLayout
        android:id="@+id/llDownloadWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_rounded_corner"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_gravity="right|bottom"
        android:layout_alignBottom="@+id/frameLayout"
        android:layout_alignParentRight="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="@string/download_wallpaper"
            android:textColor="@color/white"
            android:textSize="18dp" />
    </LinearLayout>

</RelativeLayout>

Any Suggestions?

itwasntme
  • 1,442
  • 4
  • 21
  • 28
Jinson Varghese
  • 11
  • 1
  • 1
  • 8

1 Answers1

0

I have worked for approx two day on this and finally get this solution. Call this method after you set Bitmap on ImageView

public void usingSimpleImage(ImageView imageView) {
    ImageAttacher mAttacher = new ImageAttacher(imageView);
    ImageAttacher.MAX_ZOOM = 2.0f; // Double the current Size
    ImageAttacher.MIN_ZOOM = 0.5f; // Half the current Size
    MatrixChangeListener mMaListener = new MatrixChangeListener();
    mAttacher.setOnMatrixChangeListener(mMaListener);
    PhotoTapListener mPhotoTap = new PhotoTapListener();
    mAttacher.setOnPhotoTapListener(mPhotoTap);
}

private class PhotoTapListener implements OnPhotoTapListener {
    @Override
    public void onPhotoTap(View view, float x, float y) {
    }
}

private class MatrixChangeListener implements OnMatrixChangedListener {
    @Override
    public void onMatrixChanged(RectF rect) {
    }
}

May this help you, this is working for me.

Rahul Patidar
  • 406
  • 3
  • 12