2011-08-02 73 views
0

我對捏(縮放)手勢實現了一個簡單的自定義視圖和縮放手勢檢測,就像這樣(發現它在計算器上太):的Android - 縮放視圖翻譯限制

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.ScaleGestureDetector; 
import android.view.View; 

public class MyImageView extends View { 

private static final int INVALID_POINTER_ID = -1; 

    private Drawable mImage; 
    private float mPosX; 
    private float mPosY; 

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

    private ScaleGestureDetector mScaleDetector; 
    private float mScaleFactor = 1.f; 

    public MyImageView(Context context) { 
     this(context, null, 0); 
    mImage=act.getResources().getDrawable(context.getResources().getIdentifier("imag­ename", "drawable", "packagename")); 

     mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight()); 
    } 

    public MyImageView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public MyImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
    } 

    @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(); 
      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; 

       invalidate(); 
      } 

      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 void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.save(); 
     Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY); 
     canvas.translate(mPosX, mPosY); 
     canvas.scale(mScaleFactor, mScaleFactor); 
     mImage.draw(canvas); 
     canvas.restore(); 
    } 

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

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

      invalidate(); 
      return true; 
     } 
    } 

} 

然後,當視圖是縮放的,我想限制翻譯僅限於在onDraw()中繪製的圖像的邊界。 我想我會用Canvas.getClipBounds()來控制它,看看縮放後的圖像偏移了視口,但是我無法弄清楚如何控制它。

更確切地說,我希望我的視圖不會將圖像翻譯爲視圖的0左和0頂點以上。

謝謝!

回答

0

變化

canvas.translate(mPosX, mPosY); 

if((mPosX<0)&&(mImage.getIntrinsicWidth()<canvas.getWidth())) 
     mPosX = 0; 
if((mPosY<0)&&(mImage.getIntrinsicHeight()<canvas.getHeight())) 
     mPosY = 0; 
canvas.translate(mPosX, mPosY); 
+0

mPosX時視野放大是0,所以它只能在翻譯後改變其值(如果我將它移動到合適的IT變得積極,如果我移動它離開它變得消極)... –

+0

正是!你不想讓它保持正面嗎?似乎我還沒有完全弄清楚問題):..請說明 –

+0

不,如果右邊有偏移,我也要翻譯右邊。所以我想翻譯圖像,只要有一些應該看到的部分。類似於模仿ScrollView但沒有ScrollView的東西:)所以我ZoomIn - 左邊有偏移量,右邊也是,我想翻譯到左邊,這樣我可以看到右邊的隱藏部分,我想翻譯到正確的位置,以便我看到左側的隱藏部分..希望我很清楚:/ –