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 8421735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:13:47+00:00 2026-06-10T03:13:47+00:00

I programming an android app that can drag image. Top and left of image

  • 0

I programming an android app that can drag image. Top and left of image in default mode is 0, 0. My question is how can I get top and left of visible image after drag.

Touch class:

public class Touch implements OnTouchListener {

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

    private static final float MIN_ZOOM = 1f;
    private static final float MAX_ZOOM = 5f;

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

    private PointF start = new PointF();
    private PointF mid = new PointF();

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

    private int mode = NONE;
    private float dx;
    private float dy;
    private float matrixX = 0;
    private float matrixY = 0;
    private float width = 0;
    private float height = 0;
    private float oldDistance = 1f;

    public boolean onTouch(View view, MotionEvent event) {
         ImageView imageView = (ImageView)view;

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

             case MotionEvent.ACTION_DOWN:
                 savedMatrix.set(matrix);
                 start.set(event.getX(), event.getY());
                 mode = DRAG;
                 break;

             case MotionEvent.ACTION_POINTER_DOWN:
                 oldDistance = spacing(event);
                 if(oldDistance > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                 }
                 break;

             case MotionEvent.ACTION_UP:
             case MotionEvent.ACTION_POINTER_UP:
                 mode = NONE;
                 break;

             case MotionEvent.ACTION_MOVE:
                if(mode == DRAG) {
                    matrix.set(savedMatrix);
                    matrix.getValues(values);
                    matrixX = values[Matrix.MTRANS_X];
                    matrixY = values[Matrix.MTRANS_Y];
                    width = values[Matrix.MSCALE_X] * imageView.getDrawable().getIntrinsicWidth();
                    height = values[Matrix.MSCALE_Y] * imageView.getDrawable().getIntrinsicHeight();
                    dx = event.getX() - start.x;
                    dy = event.getY() - start.y;
                    if(matrixY + dy > 0)
                        while(matrixY + dy > 0)
                            dy--;
                    if(matrixX + dx + width < imageView.getWidth())
                        while(matrixX + dx + width < imageView.getWidth())
                            dx++;
                    if(matrixY + dy + height < imageView.getHeight())
                         while(matrixY + dy + height < imageView.getHeight())
                             dy++;
                    if(matrixX + dx > 0)
                         while(matrixX + dx > 0)
                             dx--;
                    matrix.postTranslate(dx, dy);
                 }
                 else if(mode == ZOOM) {
                    float newDistance = spacing(event);
                    if(newDistance > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDistance / oldDistance;
                        matrix.getValues(values);
                        float currentScale = values[Matrix.MSCALE_X];
                        if(scale * currentScale > MAX_ZOOM) 
                            scale = MAX_ZOOM / currentScale;
                        else if (scale * currentScale < MIN_ZOOM)
                            scale = MIN_ZOOM / currentScale;
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                 }
                 break;
        }
        imageView.setImageMatrix(matrix);
        return true;
    }

    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) {
        point.set((event.getX(0) + event.getX(1)) / 2, (event.getY(0) + event.getY(1)) / 2);
    }

}

Activity class:

public class RepositionTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reposition_test_layout);
        imageView = (ImageView)findViewById(R.id.android_image);
        imageView.setOnTouchListener(new Touch());
    }

}

Layout xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reposition_test_layout"
    android:paddingLeft="20dp"
    android:paddingTop="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
         android:id="@+id/android_image"
         android:src="@drawable/android"
         android:layout_width="300dp"
         android:layout_height="300dp"
         android:scaleType="matrix"
         android:contentDescription="@string/android_image_description" >
    </ImageView>

</LinearLayout>

Thanks for your help.

  • 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-10T03:13:49+00:00Added an answer on June 10, 2026 at 3:13 am

    I finally solve the problem. When the user reposition the image, then we can save the matrix information in a file or db.

    Activity class:

    public class RepositionTestActivity extends Activity {
    
        private ImageView imageView;
        private Button button;
        private SQLiteDatabase db;
        private Cursor cursor;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.reposition_test_layout);
            imageView = (ImageView)findViewById(R.id.android_image);
            button = (Button)findViewById(R.id.reposition_button);
            ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
            if(viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    public void onGlobalLayout() {
                        db = openOrCreateDatabase("DB_DATING", MODE_PRIVATE, null);
                        db.execSQL("" +
                            "CREATE TABLE IF NOT EXISTS tbl_images (" +
                            "   id INTEGER PRIMARY KEY AUTOINCREMENT," +
                            "   name VARCHAR(50) UNIQUE NOT NULL," +
                            "   scale_x FLOAT NOT NULL," +
                            "   scale_y FLOAT NOT NULL," +
                            "   trans_x FLOAT NOT NULL," +
                            "   trans_y FLOAT NOT NULL" +
                            ");");
                        cursor = db.query("tbl_images", new String[] {"name", "scale_x", "scale_y", "trans_x", "trans_y"}, "name = \"android\"", null, null, null, null);
                        imageView.setScaleType(ScaleType.MATRIX);
                        cursor.moveToNext();
                        if(cursor.getCount() > 0) {
                            Matrix matrix = new Matrix();
                            matrix.postScale(cursor.getFloat(1), cursor.getFloat(2));
                            matrix.postTranslate(cursor.getFloat(3), cursor.getFloat(4));
                            imageView.setImageMatrix(matrix);
                        }
                        imageView.setOnTouchListener(new Touch(imageView.getImageMatrix()));
                        button.setOnClickListener(new OnClickListener() {
                            public void onClick(View view) {
                                float[] values = new float[9];
                                imageView.getImageMatrix().getValues(values);
                                ContentValues contentValues = new ContentValues();
                                contentValues.put("scale_x", values[Matrix.MSCALE_X]);
                                contentValues.put("scale_y", values[Matrix.MSCALE_Y]);
                                contentValues.put("trans_x", values[Matrix.MTRANS_X]);
                                contentValues.put("trans_y", values[Matrix.MTRANS_Y]);
                                if(cursor.getCount() > 0) {
                                    long count = db.update("tbl_images", contentValues, "name = \"android\"", null);
                                    Log.d("Update Count", "===========> " + count);
                                }
                                else {
                                    contentValues.put("name", "android");
                                    long count = db.insert("tbl_images", null, contentValues);
                                    Log.d("Insert Count", "===========> " + count);
                                }
                            }
                        });
                    }
                });
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in Android Programming. I'm trying to write an app that uses USB
Possible Duplicate: Android App high cpu-usage I am programming a app that has to
It seems that the Android SDK doesn't compile my android app correctly. After building
I'm programming an Android app, where I have to store pictures to some online
i am new to programming and hence android programming. I'm making an android app
i am new to android programming and i am trying to develop an app
I am new to android programming and i am trying to develop this app
I am programming an Android 2d game using opengl es 2.0. After I draw
i'm programming an android app, a Tasker, and i really don't find the way
I'm really new in Android programming, so I have a simple question getting a

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.