Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:28+00:00 2026-06-14T21:29:28+00:00

I need to drag and zoom an image. Here is my code … //instance

  • 0

I need to drag and zoom an image. Here is my code …

//instance variables

Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

//OnTouchListener for imageview

OnTouchListener touchAction = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView)v;

        dragAndZoom(i, event);
        return true; 

    }
};

//performing drag&zoom operations

private void dragAndZoom(View v, MotionEvent event) 
{
    ImageView view = (ImageView) v;
    view.setScaleType(ImageView.ScaleType.MATRIX);
    float scale;

    // 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());


         mode = DRAG;
        break;

    case MotionEvent.ACTION_UP: // first finger lifted

    case MotionEvent.ACTION_POINTER_UP: // second finger lifted

        mode = NONE;   

        break;

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

        oldDist = spacing(event);

        if (oldDist > 5f)
        {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            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) 
        { 
            // pinch zooming
            float newDist = spacing(event);

            if (newDist > 5f) 
            {
                matrix.set(savedMatrix);
                scale = newDist / oldDist; 
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;  
    }

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


}

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

It works okay. but sometimes the image is going out of screen boundaries on touch of it. How can i keep the image in UI all the times? Please help me. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:29:29+00:00Added an answer on June 14, 2026 at 9:29 pm
    private void limitDrag(Matrix m, ImageView view) {
    
    
        float[] values = new float[9];
        m.getValues(values);
        float transX = values[Matrix.MTRANS_X];
        float transY = values[Matrix.MTRANS_Y];
        float scaleX = values[Matrix.MSCALE_X];
        float scaleY = values[Matrix.MSCALE_Y];
    
        Rect bounds = view.getDrawable().getBounds();
        int viewWidth = getResources().getDisplayMetrics().widthPixels;
        int viewHeight = getResources().getDisplayMetrics().heightPixels;
    
    
    
        if(viewHeight<=480)
        {
    
            _y_up=0;
        }
        if(viewHeight>480&&viewHeight<980)
        {
    
            _y_up=140;
        }
    
    
        int width = bounds.right - bounds.left;
        int height = bounds.bottom - bounds.top;
        int __width=width;
        int __height=height;
        width = viewWidth / 2;
        height = viewHeight / 2;
    
    
        //height = 200 ;
        float minX = (-width) ;//* scaleX;
        float minY = (-height) ;//* scaleY;
    
    
    
        if ((transX) > (viewWidth)) {
    
            //_x_left
    
            transX = viewWidth;
        } else if (transX < minX) {
    
    
            transX = minX;
        }
    
    
        if ((-transX) > (viewWidth)) {
                 // _x_right
            transX = -(viewWidth);
        } else if (-transX < minX) {
    
            transX = -(minX+30);
        }
    
    
    
        if ((transY) > (viewHeight)) {
        //  _y_up
            transY =( viewHeight);
    
    
        } else if (transY < minY) {
    
            transY = (minY+_y_up);
        }
    
        if ((-transY) > (viewHeight)) {
        //  _y_down
            transY = -(viewHeight);
    
        } else if (-transY < minY) {
    
            transY = -(minY+170);
        }
    
        values[Matrix.MTRANS_X] = transX;
        values[Matrix.MTRANS_Y] = transY;
        m.setValues(values);
    }
    

    Call this function limitDrag(matrix,view) just above your view.setImageMatrix(matrix) to restrict the movement inside the screen
    Note:- IM doing some ugly hardcoded calculation inside it. you can change it according to your need.Just run this and check, remember its not the best solution still something is better than nothing.Happy coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container that contains an image that I need to drag from
I'm using Flash builder 4.6 for mobile applications. I need to drag an image
I need to create a drag and drop system in swing where an image
i need to be able to drag and drop my picturebox with an image
I need to upload an image to my site, using either a drag and
I need to Drag and Drop multiple items inside my Tlistbox . The code
I need to drag an image that is currently under another png image. I
I am working on a image processing project, where I need to drag image
i have a image view which is drag able and zoom able but now
I am using a custom TreeView class because i need drag-and-drop capabilities. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.