drag, zoom imageview without matrix android

2020-06-27 06:34发布

问题:

i need a solution for implement drag and zoom , routate ImageView around screen onTouch event without using matrix. i searched a lot but all answers done it with matrix and because i have multi objects in screen matrix is not suitable for me, i want drag and zoom ImageView object not matrix in the ImageView, after some try i implement a class that drag view fine but in zoom it disturb, thanks if someone can help me and sorry for bad language skills and if i have misspelling.

package com.example.painting1;

import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Drag implements OnTouchListener {

    ImageView img;

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

    private int mode = NONE;

    private PointF mid = new PointF();

    PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
    PointF StartPT = new PointF(); // Record Start Position of 'img'

    float oldDist = 1f;

    public Drag( ImageView img ) {
        // TODO Auto-generated constructor stub
        this.img = img;
        img.setOnTouchListener(this);
    }

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

        /**
         * Calculate the mid point of the first 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);
        }

        @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                int evid = event.getAction();
                int moid = MotionEvent.ACTION_MASK;

                switch (evid & moid)
                {
                    case MotionEvent.ACTION_MOVE :

                       if (mode == DRAG) {                      
                                PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                                img.setX((int)(StartPT.x+mv.x));
                                img.setY((int)(StartPT.y+mv.y));
                                StartPT = new PointF( img.getX(), img.getY() );
                        }

                         else if (mode == ZOOM) {
                                    float newDist = spacing(event);
                                    if (newDist > 10f) {
                                        //matrix.set(savedMatrix);
                                        float scale = (newDist / oldDist);
                                        //matrix.postScale(scale, scale, mid.x, mid.y);

                                        img.layout((int)scale, (int)scale, (int)mid.x, (int)mid.y);

                                        img.setScaleX(scale);
                                        img.setScaleY(scale);
                                    }
                         }

                        break;

                    case MotionEvent.ACTION_DOWN :

                        mode = DRAG;

                        DownPT.x = event.getX();
                        DownPT.y = event.getY();
                        StartPT = new PointF( img.getX(), img.getY() );
                        break;
                    case MotionEvent.ACTION_UP :
                        // Nothing have to do
                    case MotionEvent.ACTION_POINTER_UP :
                            mode = NONE;
                        break;

                    case MotionEvent.ACTION_POINTER_DOWN :

                        oldDist = spacing(event);
                        if (oldDist > 10f) {
                            //savedMatrix.set(matrix);
                            midPoint(mid, event);
                            mode = ZOOM;
                        }
                        break;
                    default :
                        break;
                }
                return true;
        }
}

回答1:

**Here Is the Complete Source Code of Zoom,Drag,Touchable ImageView..

Its Work Smooth While perform Drag and Zoom on Image.**

package com.admin.imagemovementzoom;

import android.annotation.SuppressLint;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;


public class FullmageActivity extends AppCompatActivity implements View.OnTouchListener {

    private ImageView img_fullimage;
    private String picturePath;


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

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

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullmage);

        picturePath = getIntent().getStringExtra("image");

        FindId();

        Log.d("adaa", "file://" + picturePath);

        Picasso.get().load("file://" + picturePath).into(img_fullimage);

        img_fullimage.setOnTouchListener(this);
        img_fullimage.setScaleType(ImageView.ScaleType.MATRIX);

    }


    private void FindId() {
        img_fullimage = findViewById(R.id.img_full);
    }


    /**
     * Called when a touch event is dispatched to a view. This allows listeners to
     * get a chance to respond before the target view.
     *
     * @param v     The view the touch event has been dispatched to.
     * @param event The MotionEvent object containing full information about
     *              the event.
     * @return True if the listener has consumed the event, false otherwise.
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView view = (ImageView) v;

        // Dump touch event to log
        dumpEvent(event);

        // Handle touch events here...
        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_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                Log.d(TAG, "mode=NONE");
                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;
        }

        view.setImageMatrix(matrix);
        return true; // indicate event was handled
    }

    /**
     * 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(TAG, sb.toString());
    }

    /**
     * Determine the space between the first two fingers
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    /**
     * Calculate the mid point of the first 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);
    }
}


回答2:

Try this code

I have a solution for you,I wish it will work for you,

try to use TouchImageView instead of ImageView in Layout.because touchimageview have property that zooming in or out itself

https://github.com/MikeOrtiz/TouchImageView 

from this link you learn how to use touchimageview...

<com.luminous.pick.TouchImageView
                android:id="@+id/img2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

Then After implement onDragListener of TouchImageView so you can get DRAG And Drop of Imageview easily...

@Override
public boolean onDrag(View v, DragEvent event) {

    switch (event.getAction()) {

        case DragEvent.ACTION_DROP:

            TouchImageView target = (TouchImageView) v;
            TouchImageView dragged = (TouchImageView) event.getLocalState();

            Drawable target_draw = target.getDrawable();
            Drawable dragged_draw = dragged.getDrawable();

            dragged.setImageDrawable(target_draw);
            target.setImageDrawable(dragged_draw);

   }
    return true;
}