Android PhotoView - zoom to previous position (x y coordinates and scale) - android

I'm using https://github.com/chrisbanes/PhotoView so user can zoom the image and then draw some point.
There is also a function to delete this point. In this case I redraw whole bitmap:
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
setImageBitmap(bmp);
But now I want to zoom image to previous position/coordinates.
This should work:
setScale(scale, focalX, focalY, false);
I know the scale but I need focalX, focalY.
How can I get it? Maybe I should somehow calculate it from the
getSuppMatrix(matrix);
But I'm not sure if it's possible and how.
Do you have some tips? Help?

Solved by getting matrix
getSuppMatrix(matrix);
before setting new bitmap
setImageBitmap(bmp);
And then setting previous matrix
setSuppMatrix(matrix);

Related

How to find center position of a bitmap that rotated and scaled with a matrix?

I want to calculate center of a bitmap that is drawn on a canvas with a matrix, it can be rotated, scaled or translated with a arbitrary value. What is the easiest way to find center of the this bitmap on canvas?
You need to apply the matrix to the coordinates of the center of bitmap.
If you use a canvas that has a transformation matrix, you can get the final matrix through Canvas.getMatrix()
If you draw the Bitmap on the Canvas with a Matrix : drawBitmap(bitmap, matrix, paint), then you you need to use that Matrix, or concatenate it to that of the Canvas (in the case it has one).
Then you can finally apply that matrix to the center of the matrix using Matrix.mapPoints.
Something like :
canvas.drawBitmap(bitmap, bitmap_transform, paint);
Matrix full_transform = new Matrix(canvas.getMatrix());
full_transform.postConcat(bitmap_transform);
float[] center = new float[] {bitmap.getHeight()/2.0, bitmap.getWidth()/2.0};
full_transform.mapPoints(center);
Alternatively, if you apply transformations to your bitmap without a matrix, you can use the full_transform.postRotate, full_transform.postScale, etc with the same values. In particular, if you draw your bitmap with drawBitmap(bitmap, left, top, paint) then you need to do a full_transform.postTranslate(left, top).
If you're looking for the "easiest" way then just sticking with the translate rotate and scale functions would solve the problem. The reason that those were developed was so developers wouldn't have to do vector calculus for simple animations. Also the only value you would have to actually calculate in that sense it is the translate value after you take into account the original coordinates.

How do I rotate bitmap on canvas around center x y in android

I am drawing a number of shapes on a canvas. One such shape is a bitmap. Say the center of the bitmap on the canvas is at center xCenter, yCenter such that to draw I call
xLeft = xCenter+mBitmap.getWidth()/2;
yTop = yCenter+mBitmap.getHeight()/2;
canvas.drawBitmap(mBitmap, x,y, mFilledPaint);
So far everything works fine. The next step is to rotate the bitmap around the center xCenter, yCenter. My code below is not doing it. It moves the bitmap all over the place, whereas all I want is for the image to rotate in place around its own center. How can I fix the code below? I already looked at Android: How to rotate a bitmap on a center point and at best I don't understand the responses there.
My code
xLeft = xCenter+mBitmap.getWidth()/2;
yTop = yCenter+mBitmap.getHeight()/2;
matrix.postRotate(30f, xLeft, yTop);
canvas.drawBitmap(mBitmap, matrix, mFilledPaint);
also replacing xLeft and yTop with 0 does not seem to work.. any idea ??
Edit:
xCenter, yCenter is not the center of the canvas. It's just the point where the center of the bitmap lands.
i guess you mean, you can not understand following code:
this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());
I think i could give some explanation about this, since i done this task in my project before:
the first line to rest it to identity, which means do thing, because any thing times identity matrix get itself.
the second line, move the original point to the target position,this is actually specify xLeft and yTop to which the bitmap will draw in canvas.
the thrid line, rotate around center point (xLeft+bitmapWidth/2,yTop+Height/2)
To understand this, you need to know how the matrix is used.basically, you bitmap (the points in bitmap is a matrix actually) times the matrix you created here, then it get a new matrix, canvas just draw that new matrix.

How do I position my Bitmap on the canvas using a Matrix?

I'm implementing a pinching and dragging thing and I'm confused as to how to get the image that I'm affecting to move to it's dragged to location (the scaling is working).
So, in the past I would position the image thusly...
canvas.drawBitmap(img, x,y, paint);
but now that I'm using a matrix I don't see what function in it gets the image over to where I want it to be.
Could someone post a short code snippet of what one needs to do to get the image to move away from 0,0?
TIA
Use postTranslate on your scaling matrix:
scaleMatrix.postTranslate(x, y);
canvas.drawBitmap(img, scaleMatrix, paint);

how to get pixel Color value on a zoomed bitmap?

I am using zooming and panning with sony ericssion tutorial with this link
http://blogs.sonyericsson.com/developerworld/2010/06/09/android-one-finger-zoom-tutorial-part-3/
and when I am using Bitmap.getPixel(x,y) in ontouchevent of image.Its giving different different RGB values of color.After zooming bitmap same problem occurs.I have tried x y coordinates diviing by zooming factor also.it doesnt work for me.
can plz someone help me to solve this problem.how to get the color value on a zoomed Bitmap?
getPixel is working on your unzoomed Bitmap.
I assume X and Y come from a touch event and are relative to the View.
I see in your link that the example is doing the resize using two Rect objects.
#Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null && mState != null) {
canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
}
}
I assume that is is also how you did it.
Simply dividing your coordinates with the zoom ratio will not work because X and Y are relative to your view and not to the RectDst.
I generally use Matrix objects to do zooming because it is so easy to reproduce, invert, etc...
For exemple if your zoom had been done using Matrix you'll be able to invert it, give it your X,Y and get your original coordinates relative to your bitmap back.
But you've got a way to do it anyway, by calculating the Matrix using the 2 Rects:
//Recreate the Matrix using the 2 Rects
Matrix m = new Matrix()
m.setRectToRect(mRectDst, mRectSrc, false);
//Transform the coordinates back to the original plan
float[] src = {x,y};
float[] dst = new float[2];
m.mapPoints(src, dst);
myBitmap.getPixel(dst[0], dst[1]);
I did not test it but it should work or at least help you find your solution.
Bitmap.getPixel(x,y) get damm slow result .
prefer
int [] byteArry= new int [Bitmap.getWidth()*Bitmap.getHeight()] ;
Bitmap.getPixels(byteArry, 0, Bitmap.getWidth(), 0, 0, slotBitmap_1.getWidth(), Bitmap.getHeight());
the byteArry will returns the pixels color of image.

rotate image in around its center point in canvas

rotate image on canvas around its center . i tried some example from net but i am failed
if i am using bitmap.creatbitmap with matrix i am getting error
Please suggest me which is better
Thanks in advance
Aswan
You can use a matrix to rotate. First you set the position (I'm using the coordinates of the bitmap's centre). Then apply a rotation. Then draw using your matrix.
Matrix transform = new Matrix();
transform.setTranslate(xOfCentre, yOfCentre);
transform.preRotate(turnDegrees, width/2, height/2);
canvas.drawBitmap(bitmap, transform, null);
If you want your turning to be animated, then see my answer to "animating and rotating an image...".

Categories

Resources