2013-10-25 93 views
0

我正在開發一個與圖像視圖旋轉,移動和縮放有關的應用程序。在這個圖像視圖中,旋轉和縮放效果最好。當移動圖像視圖時,它正在移動,但不是控制,而是走出屏幕。我怎樣才能控制這個圖像視圖的屏幕只?Android如何控制圖像只能在屏幕上移動?

在這我使用手勢探測器圖像視圖移動。

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

} 

謝謝...

+0

看到這個鏈接http://www.javacodegeeks.com/2011/07/android-game-development-moving-images .html – BBdev

+0

嘿,你解決了這個問題嗎? – ryderz8

回答

1

你需要確保符合以下條件時,你移動你的觀點:

  1. 新的X> 0
  2. 新Y> 0
  3. 新X < screenWidth - viewWidth
  4. 的新的Y < screenHeight - viewHeight

希望這有助於:)

+0

這有一點幫助..bit ..但是,謝謝你對我的迴應.....並解決了我的問題... –

+0

嘿,我有同樣的概率...任何代碼片段? – ryderz8

相關問題