1

我想通过手指手势移动视图。我搜索但添加了此代码,但仍然无法正常工作。

请帮我检查问题出在哪里。

public boolean onTouchEvent(MotionEvent event) {
    curX = event.getX();
    curY = event.getY();
    if(event.getPointerCount() == 2) {
        previousFingerDistance = currentFingerDistance;
        currentFingerDistance = fingerDist(event);

        if(previousFingerDistance == currentFingerDistance) {
            prevX = curX;
            prevY = curY;
            invalidate();
        }
    }
    return true;
}
4

2 回答 2

2

试试这个代码。我正在使用它在屏幕上移动 ImageView,但我认为它适合其他类型的视图。

imView.setOnTouchListener(new OnTouchListener() {
                private static final String TAG = "Touch";
                // These matrices will be used to move and zoom image

                Matrix savedMatrix = new Matrix();
                PointF start = new PointF();
                public 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 int mode = NONE;

                float oldDist;

                public boolean onTouch(View v, MotionEvent event) {

                    ImageView view = (ImageView) v;
                    view.setScaleType(ImageView.ScaleType.MATRIX);
                    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
                    Log.d("point",start.x +" "+start.y);
                    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);
                }

            });

更一般的观点

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            float dx =  motionEvent.getX();
            float dy =  motionEvent.getY();
            break;

        case MotionEvent.ACTION_MOVE:
            float x =  motionEvent.getX();
            fload y =  motionEvent.getY();
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
            float left = lp.leftMargin + (x - dx);
            float top = lp.topMargin + (y - dy);
            lp.leftMargin = (int)left;
            lp.topMargin = (int)top;
            view.setLayoutParams(lp);
            break;
    }
    return true;
}
于 2013-07-17T09:38:20.933 回答
0

试试这个,假设你有一个带有 draw() 方法的视图

private var Cx = 0f ; private var Cy = 0f
override fun onDraw(canvas: Canvas?) {
    canvas?.drawCircle(Cx,Cy,_radius.toFloat(), paint)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
    event?:return false
    val action = event.actionMasked
    return when(action){
        MotionEvent.ACTION_MOVE -> {
            Cx = event.x
            Cy = event.y
            invalidate()
            true
        }

        MotionEvent.ACTION_DOWN -> {
            true
        }

        MotionEvent.ACTION_UP -> {
            true
        }
        else -> false
    }
}
于 2018-11-05T20:09:05.773 回答