拖动,缩放没有矩阵android的imageview

drag, zoom imageview without matrix android(拖动,缩放没有矩阵android的imageview)

本文介绍了拖动,缩放没有矩阵android的imageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个解决方案来实现拖动和缩放,在不使用矩阵的情况下围绕屏幕 onTouch 事件路由 ImageView.我搜索了很多,但所有答案都是用矩阵完成的,因为我在屏幕矩阵中有多个对象不适合我,我想在 ImageView 中拖动和缩放 ImageView 对象而不是矩阵,经过一些尝试后我实现了一个可以拖动视图的类但是在放大时它会打扰,感谢有人可以帮助我,并为糟糕的语言技能以及我的拼写错误感到抱歉.

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

推荐答案

**这里是Zoom,Drag,Touchable ImageView的完整源码..

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

这篇关于拖动,缩放没有矩阵android的imageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:拖动,缩放没有矩阵android的imageview

基础教程推荐