gpt4 book ai didi

Android - 捏缩放触摸事件坐标

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:06 27 4
gpt4 key购买 nike

我正在尝试获取我正在创建的 Android 应用程序的 Canvas 坐标。在我添加代码以使用比例焦点(以下两行)之前,它工作得很好:

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

这是我的 View 类的源代码:

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


}

有什么想法可以让这个工作正常进行吗?

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

再次更新:缩放后出现问题。缩放前,坐标是正确的,但缩放后,坐标太偏右下方了。看起来你缩小得越多,他们得到的错误就越多。

最佳答案

哈哈!成功!这几乎花了我一整天的时间,但我通过一堆猜测和检查弄明白了。

这是我需要的代码:

case MotionEvent.ACTION_DOWN: {
final float x = (ev.getX() - scalePointX)/mScaleFactor;
final float y = (ev.getY() - scalePointY)/mScaleFactor;
cX = x - mPosX + scalePointX; // canvas X
cY = y - mPosY + scalePointY; // canvas Y
[snip]
}

还有一段类似的 ACTION_MOVE 代码

关于Android - 捏缩放触摸事件坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com