android touch事件坐标原点,Android - 捏缩放ontouch事件坐标

我正在尝试获取我正在创建的Android应用程序的画布坐标.它很有用,直到我添加代码以使用缩放焦点(以下两行):

scalePoint.setX((int) detector.getFocusX());

scalePoint.setY((int) detector.getFocusY());

这是我的视图类的源代码:

package us.kniffin.Jigsaw;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.view.GestureDetector.SimpleOnGestureListener;

import android.view.MotionEvent;

import android.view.ScaleGestureDetector;

import android.view.View;

public class TestView extends View {

private float mLastTouchX;

private float mLastTouchY;

private float mPosX;

private float mPosY;

private Rect rect;

private float cX, cY; // circle coords

// Scaling objects

private ScaleGestureDetector mScaleDetector;

private float mScaleFactor = 1.f;

// The focus point for the scaling

private float scalePointX;

private float scalePointY;

public TestView(Context context) {

super(context);

mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint p = new Paint();

p.setColor(Color.RED);

rect = canvas.getClipBounds();

canvas.save();

canvas.scale(mScaleFactor, mScaleFactor, scalePointX, scalePointY);

canvas.translate(mPosX, mPosY);

canvas.drawCircle(cX, cY, 10, p);

canvas.restore();

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

// Let the ScaleGestureDetector inspect all events.

mScaleDetector.onTouchEvent(ev);

final int action = ev.getAction();

switch(action & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN: {

final float x = ev.getX()/mScaleFactor;// screen X position

final float y = ev.getY()/mScaleFactor;// screen Y position

cX = x - (rect.left * mScaleFactor) - mPosX; // canvas X

cY = y - (rect.top * mScaleFactor) - mPosY; // canvas Y

// Remember where we started

mLastTouchX = x;

mLastTouchY = y;

break;

}

case MotionEvent.ACTION_MOVE: {

final float x = ev.getX()/mScaleFactor;

final float y = ev.getY()/mScaleFactor;

cX = x - (rect.left * mScaleFactor) - mPosX; // canvas X

cY = y - (rect.top * mScaleFactor) - mPosY; // canvas Y

// Only move if the ScaleGestureDetector isn't processing a gesture.

if (!mScaleDetector.isInProgress()) {

final float dx = x - mLastTouchX; // change in X

final float dy = y - mLastTouchY; // change in Y

mPosX += dx;

mPosY += dy;

invalidate();

}

mLastTouchX = x;

mLastTouchY = y;

break;

}

case MotionEvent.ACTION_UP: {

mLastTouchX = 0;

mLastTouchY = 0;

invalidate();

}

}

return true;

}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

@Override

public boolean onScale(ScaleGestureDetector detector) {

mScaleFactor *= detector.getScaleFactor();

scalePointX = detector.getFocusX();

scalePointY = detector.getFocusY();

// Don't let the object get too small or too large.

mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));

invalidate();

return true;

}

}

}

任何想法我需要做什么才能使这个工作?

更新:我将代码示例替换为具有相同问题的另一个代码示例,但简化为基本要素

再次更新:缩放后出现问题.在缩放之前,坐标是正确的,但之后,坐标在您单击的右侧和下方太远.看起来缩小的越多,他们得到的错误就越多.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值