gpt4 book ai didi

android - 缩放扩展 LinearLayout 的自定义小部件

转载 作者:行者123 更新时间:2023-11-29 18:08:08 26 4
gpt4 key购买 nike

您好,我正在尝试在 LinearLayout 中实现捏合缩放功能,我的第一次尝试是扩展线性布局并使用缩放手势检测器来检测捏合缩放手势,遵循 android developers blog 的建议

我试图覆盖 onDraw 方法并使用 Canvas 设置缩放比例,但它根本不起作用, View 保持固定大小

这是我的自定义 View 中的代码:

private static final int INVALID_POINTER_ID = -1;

private static final String TAG = "MyTag";

private float mScaleFactor = 1f;
private ScaleGestureDetector mScaleDetector;
private float mPosX;
private float mPosY;
private float mFocusX;
private float mFocusY;

private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;

private boolean mHandlingGesture = true;

public CrosswordGridLayout(Context context) {
super(context);
init();
}

public CrosswordGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CrosswordGridLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
setWillNotDraw(false);
mScaleDetector = new ScaleGestureDetector(getContext(),
new ScaleListener());
}

@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "onDraw");
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor, mFocusX, mFocusY);
super.onDraw(canvas);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d(TAG, "onTouchEvent");
// Let the ScaleGestureDetector inspect all events.
mHandlingGesture = mScaleDetector.onTouchEvent(ev);

final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();

mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}

case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);

// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;

mPosX += dx;
mPosY += dy;

requestLayout();
}

mLastTouchX = x;
mLastTouchY = y;

break;
}

case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}

case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}

case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}

return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}

private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(TAG, "onScale");
mScaleFactor *= detector.getScaleFactor();

// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

mFocusX = detector.getFocusX();
mFocusY = detector.getFocusY();
Log.d(TAG, "mScaleFactor=" + mScaleFactor);
requestLayout();

return true;
}
}

最佳答案

Canvas 操作是在 dispatchDraw() 而不是 onDraw() 中进行的

protected void dispatchDraw(Canvas canvas) {
Log.d(TAG, "dispatchDraw");
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor, mFocusX, mFocusY);
super.dispatchDraw(canvas);
canvas.restore();
}

该示例与您编写的代码之间的区别在于,在该示例中,缩放的是简单 View ,而在您的示例中,它是 ViewGroup。

并且不要在 ScaleListener.onScale() 中使用 requestLayout(),而是使用 invalidate()

关于android - 缩放扩展 LinearLayout 的自定义小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266256/

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