1

こんにちは、LinearLayout 内にピンチ ツー ズーム機能を実装しようとしています。私の最初の試みは、linearlayout を拡張し、スケール ジェスチャ検出器を使用して、Android 開発者ブログからの提案に従って、ピンチ ツー ズーム ジェスチャを検出することでした。

onDraw メソッドをオーバーライドし、キャンバスを使用してスケーリングを設定しようとしましたが、まったく機能せず、ビューは固定サイズのままです

これは私のカスタムビュー内のコードです:

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

1 に答える 1

3

内部で操作をキャンバスします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です。

代わりにrequestLayout()in ScaleListener.onScale()useを使用しないでください。invalidate()

于 2012-09-04T15:22:33.970 に答える