2012-09-24 103 views
0

我有一個自定義的圖像查看與縮放使用SeekBar。在該imageView中,我正在標記圖像的一部分,但是當我移動標記不隨圖像移動的圖像時,它保持不變。所以請幫助我,如何在選定的部分製作該標記。標記正在移動圖像拖動(平移&移動)

下面的代碼我已經習慣了放大:

public class ImageZoomView extends View implements Observer { 

    /** Paint object used when drawing bitmap. */ 
    private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); 

    /** Rectangle used (and re-used) for cropping source image. */ 
    private final Rect mRectSrc = new Rect(); 

    /** Rectangle used (and re-used) for specifying drawing area on canvas. */ 
    private final Rect mRectDst = new Rect(); 

    /** The bitmap that we're zooming in, and drawing on the screen. */ 
    private Bitmap mBitmap; 
    private Bitmap mBitmap2; 

    private ShapeDrawable one; 
    private ShapeDrawable two; 


    /** Pre-calculated aspect quotient. */ 
    private float mAspectQuotient; 

    /** State of the zoom. */ 
    private ZoomState mState; 

    // Public methods 

    /** 
    * Constructor 
    */ 
    public ImageZoomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    /** 
    * Set image bitmap 
    * 
    * @param bitmap 
    *   The bitmap to view and zoom into 
    */ 
    public void setImage(Bitmap bitmap) { 
     mBitmap = bitmap; 

     calculateAspectQuotient(); 

     invalidate(); 
    } 

    /** 
    * Set image bitmap 
    * 
    * @param bitmap 
    *   The bitmap to view and zoom into 
    */ 
    public void setImage2(Bitmap bitmap2) { 
     mBitmap2 = bitmap2; 

     calculateAspectQuotient(); 

     invalidate(); 
    } 

    /** 
    * Set object holding the zoom state that should be used 
    * 
    * @param state 
    *   The zoom state 
    */ 
    public void setZoomState(ZoomState state) { 
     if (mState != null) { 
      mState.deleteObserver(this); 
     } 

     mState = state; 
     mState.addObserver(this); 

     invalidate(); 
    } 

    // Private methods 

    private void calculateAspectQuotient() { 
     if (mBitmap != null) { 
      mAspectQuotient = (((float) mBitmap.getWidth())/mBitmap.getHeight())/(((float) getWidth())/getHeight()); 
      Log.i("Aspect",""+mAspectQuotient); 
     } 
    } 

    // Superclass overrides 

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (mBitmap != null && mState != null) { 
      final int viewWidth = getWidth(); 
      final int viewHeight = getHeight(); 
      final int bitmapWidth = mBitmap.getWidth(); 
      final int bitmapHeight = mBitmap.getHeight(); 

      final float panX = mState.getPanX(); 
      final float panY = mState.getPanY(); 
      final float zoomX = mState.getZoomX(mAspectQuotient*0.5f) * viewWidth/bitmapWidth; 
      final float zoomY = mState.getZoomY(mAspectQuotient*0.5f) * viewHeight/bitmapHeight; 

      Log.i("onDraw", "onDraw" + SimpleSeekBarListener.f); 
      // Setup source and destination rectangles 
      mRectSrc.left = (int) (panX * bitmapWidth - viewWidth/(zoomX * 2)); 
      mRectSrc.top = (int) (panY * bitmapHeight - viewHeight/(zoomY * 2)); 
      mRectSrc.right = (int) (mRectSrc.left + viewWidth/zoomX); 
      mRectSrc.bottom = (int) (mRectSrc.top + viewHeight/zoomY); 
      mRectDst.left = getLeft(); 
      mRectDst.top = getTop(); 
      mRectDst.right = getRight(); 
      mRectDst.bottom = getBottom(); 

      // Adjust source rectangle so that it fits within the source image. 
      if (mRectSrc.left < 0) { 
       mRectDst.left += -mRectSrc.left * zoomX; 
       mRectSrc.left = 0; 
      } 
      if (mRectSrc.right > bitmapWidth) { 
       mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX; 
       mRectSrc.right = bitmapWidth; 
      } 
      if (mRectSrc.top < 0) { 
       mRectDst.top += -mRectSrc.top * zoomY; 
       mRectSrc.top = 0; 
      } 
      if (mRectSrc.bottom > bitmapHeight) { 
       mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY; 
       mRectSrc.bottom = bitmapHeight; 
      } 
      // int h1= mBitmap.getHeight(); 
      // Log.d("Height Befor",""+h1); 
      canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint); 



     } 
    } 



    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     int left1 = left; 
     calculateAspectQuotient(); 
    } 

    // implements Observer 
    public void update(Observable observable, Object data) { 
     invalidate(); 
    } 

} 

回答

1

我覺得這是解決方案,您可以合併圖像,以便試試這個方法:

private Bitmap annotateTheImage(Bitmap thisBitmap) { 

    Bitmap constructThisImage = Bitmap.createBitmap(thisBitmap.getWidth(),thisBitmap.getHeight(),Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(constructThisImage); 

    canvas.drawBitmap(thisBitmap, 0, 0, null); 

    for (MyAnnotations point : annotationsPoints) { 
     canvas.drawBitmap(point.getBitmap(), point.x, point.y, null); 
    } 

    return constructThisImage; 
} 

,並在你的畫布這樣調用:

canvas.drawBitmap(annotateTheImage(mBitmap), mRectSrc, mRectDst, mPaint); 

mBitmap是您的自定義imageView中的原始圖像。

我希望這能幫到你=)