3

I am working on app in which i have to rotate,drag n drop and zoom in/out an image(smaller one) over another image(bigger one). I am done with rotation and drag n drop but not able to implement zoom in/out. I know how to do it on canvas But in my app I am not using Canvas. Any Help would be Grateful..........

Thanks Davender

user698425
  • 31
  • 1
  • 1
  • 3
  • I'm curious as to why you can't use Canvas? – slund Apr 25 '11 at 13:14
  • I'm new to android and dont know working with canvas completely like how to add buttons,seekbars etc to canvas. so i opted for LinearLayout and RelativeLayout to do that job and now my app is almost done n zoom is the only remaining functionality. – user698425 Apr 26 '11 at 04:28
  • I am trying it with the help of matrix.like using matrix.postscale(scaleWidth,scaleHeight) and then calling imageview.setImageMatrix(matrix) and imageView.setScaleType(ScaleTypeMatrix). But not able to make it functional – user698425 Apr 26 '11 at 04:35
  • 1
    Hello Davender, Your Problem is Solved or not? – Dipak Keshariya Sep 28 '11 at 13:30
  • If the answer solves the problem, then kindly accept it, to help others know the solution is available. Thanks – Omar Rehman Jul 25 '12 at 03:17
  • hi,you told that you know how to zoom canvas.please help me i want to zoom canvas. – user1083266 Apr 06 '13 at 10:57

1 Answers1

11

check out the following code and let me know if it helps.

public class Touch extends Activity implements OnTouchListener {

private static final String TAG = "Touch" ;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new  PointF();
public static PointF mid = new PointF();

// We can be in one of these 3 states
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public static int mode = NONE;

float oldDist;

private float[] matrixValues = new float[9];

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView view = (ImageView) findViewById(R.id.imageView);
    view.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    ImageView view = (ImageView) v;

    switch (event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:

        savedMatrix.set(matrix);
        start.set(event.getX(), event.getY());
        Log.d(TAG, "mode=DRAG" );
        mode = DRAG;
        break;

    case MotionEvent.ACTION_POINTER_DOWN:

        oldDist = spacing(event);
        Log.d(TAG, "oldDist=" + oldDist);
        if (oldDist > 10f) {

            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);
        }
        else if (mode == ZOOM) {

            float newDist = spacing(event);
            Log.d(TAG, "newDist=" + newDist);
            if (newDist > 10f) {

                matrix.set(savedMatrix);
                float scale = newDist / oldDist;
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:

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

    // Perform the transformation
    view.setImageMatrix(matrix);

    return true; // indicate event was handled
}

  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);
}

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);
}

}

Following is the main.xml file

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    <ImageView 
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/yourimage"
            android:scaleType="matrix">
    </ImageView>
    </FrameLayout>

replace the value of src property of the imageview (yourimage), to name of the image you have in your project.

Mahdi-bagvand
  • 1,396
  • 19
  • 40
Omar Rehman
  • 2,045
  • 17
  • 17