当图像拖动时(标记和移动),标记正在移动

时间:2012-09-24 11:10:35

标签: android imageview android-canvas

我有一个使用SeekBar进行缩放的自定义imageView。在那个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 个答案:

答案 0 :(得分: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中的原始图像。

我希望这可以帮助你=)