且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

马克移动时图像拖动(平移和移动)

更新时间:2023-11-13 13:23:28

我觉得这是解决方案,您可以合并图像,以便试试这个方法:

 私人位图annotateTheImage(位图thisBitmap){    位图constructThisImage = Bitmap.createBitmap(thisBitmap.getWidth(),thisBitmap.getHeight(),Bitmap.Config.ARGB_8888);
    帆布帆布=新的Canvas(constructThisImage);    canvas.drawBitmap(thisBitmap,0,0,NULL);    对于(MyAnnotations点:annotationsPoints){
        canvas.drawBitmap(point.getBitmap(),point.x,point.y,NULL);
    }    返回constructThisImage;
}

和你通话的帆布这样的:

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

该mBitmap是在自定义的ImageView原始图像。

我希望这可以帮助您=)

I have a custom imageView with zoom using SeekBar. In that imageView I am Marking a portion of the image but when i move the image that mark is not moving with the image it remains same. So please help me , how to make that mark at the selected portion.

Following code I have Used To zoom:

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

}

I find this solution, you can merge images so try this method:

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

and in you canvas call like this:

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

The mBitmap is the original image in your custom imageView.

I hope this help you =)