Android如何控制图像只在屏幕上移动?

时间:2013-10-25 08:49:31

标签: android imageview gestures

我正在开发一个与图像视图旋转,移动和缩放相关的应用程序。在此图像视图中,旋转和缩放效果最佳。当移动图像视图时,它正在移动但不是控制而是移出屏幕。如何仅为屏幕控制此图像视图?

在此我正在使用手势探测器移动图像视图。

Mycode是::

public class MoveGestureDetector extends BaseGestureDetector {

/**
 * Listener which must be implemented which is used by MoveGestureDetector
 * to perform callbacks to any implementing class which is registered to a
 * MoveGestureDetector via the constructor.
 * 
 * @see MoveGestureDetector.SimpleOnMoveGestureListener
 */
public interface OnMoveGestureListener {
    public boolean onMove(MoveGestureDetector detector);
    public boolean onMoveBegin(MoveGestureDetector detector);
    public void onMoveEnd(MoveGestureDetector detector);
}

/**
 * Helper class which may be extended and where the methods may be
 * implemented. This way it is not necessary to implement all methods
 * of OnMoveGestureListener.
 */
public static class SimpleOnMoveGestureListener implements OnMoveGestureListener {
    public boolean onMove(MoveGestureDetector detector) {
        return false;
    }

    public boolean onMoveBegin(MoveGestureDetector detector) {
        return true;
    }

    public void onMoveEnd(MoveGestureDetector detector) {
        // Do nothing, overridden implementation may be used
    }
}

private static final PointF FOCUS_DELTA_ZERO = new PointF();

private final OnMoveGestureListener mListener;

private PointF mCurrFocusInternal;
private PointF mPrevFocusInternal;  
private PointF mFocusExternal = new PointF();
private PointF mFocusDeltaExternal = new PointF();


public MoveGestureDetector(Context context, OnMoveGestureListener listener) {
    super(context);
    mListener = listener;

}

@Override
protected void handleStartProgressEvent(int actionCode, MotionEvent event){
    switch (actionCode) { 
        case MotionEvent.ACTION_DOWN: 
            resetState(); // In case we missed an UP/CANCEL event

            mPrevEvent = MotionEvent.obtain(event);
            mTimeDelta = 0;

            updateStateByEvent(event);
            break;

        case MotionEvent.ACTION_MOVE:
            mGestureInProgress = mListener.onMoveBegin(this);
            break;
    }
}

@Override
protected void handleInProgressEvent(int actionCode, MotionEvent event){    
    switch (actionCode) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mListener.onMoveEnd(this);
            resetState();
            break;

        case MotionEvent.ACTION_MOVE:
            updateStateByEvent(event);

            if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) {
                final boolean updatePrevious = mListener.onMove(this);
                if (updatePrevious) {
                    mPrevEvent.recycle();
                    mPrevEvent = MotionEvent.obtain(event);
                }
            }
            break;
     }
}

protected void updateStateByEvent(MotionEvent curr) {
    super.updateStateByEvent(curr);

    final MotionEvent prev = mPrevEvent;

    // Focus intenal
    mCurrFocusInternal = determineFocalPoint(curr);
    mPrevFocusInternal = determineFocalPoint(prev);

    boolean mSkipNextMoveEvent = prev.getPointerCount() != curr.getPointerCount();
    mFocusDeltaExternal = mSkipNextMoveEvent ? FOCUS_DELTA_ZERO : new PointF(mCurrFocusInternal.x - mPrevFocusInternal.x,  mCurrFocusInternal.y - mPrevFocusInternal.y);

    mFocusExternal.x += mFocusDeltaExternal.x;
    mFocusExternal.y += mFocusDeltaExternal.y;     

}

/**
 * Determine (multi)finger focal point (a.k.a. center point between all
 * fingers)
 * @param MotionEvent e
 * @return PointF focal point
 */
private PointF determineFocalPoint(MotionEvent e){
    final int pCount = e.getPointerCount(); 
    float x = 0f;
    float y = 0f;

    for(int i = 0; i < pCount; i++){
        x += e.getX(i);
        y += e.getY(i);
    }
     return new PointF(x/pCount, y/pCount);
}

public float getFocusX() {
    return mFocusExternal.x;
}

public float getFocusY() {
    return mFocusExternal.y;
}

public PointF getFocusDelta() {
return mFocusDeltaExternal;
}

}

...谢谢

1 个答案:

答案 0 :(得分:1)

移动视图时,您需要确保以下条件适用:

  1. 新X&gt; 0
  2. 新Y&gt; 0
  3. 新X&lt; screenWidth - viewWidth
  4. 新Y&lt; screenHeight - viewHeight
  5. 希望这会有所帮助:)