I am trying to manually get an image inside an imageview centered and fitting the screen. I need to do it with a matrix (I will later dynamically change the matrix transform
Here is how I solved my problem using matrices (requested by joakime in the other answer):
private void setImageTransformation(float tx, float ty, float scale) {
savedMatrix.reset();
savedMatrix.postTranslate(-imageWidth / 2f, -imageHeight / 2f);
savedMatrix.postScale(scale, scale);
savedMatrix.postTranslate(tx, ty);
imageView.setImageMatrix(savedMatrix);
}
public void resetImageMatrix() {
if (!isImageLoaded()) return;
imageWidth = imageView.getDrawable().getIntrinsicWidth();
imageHeight = imageView.getDrawable().getIntrinsicHeight();
float scaleX = (float) displayWidth / (float) imageWidth;
float scaleY = (float) displayHeight / (float) imageHeight;
minScale = Math.min(scaleX, scaleY);
maxScale = 2.5f * minScale;
initialTranslation.set(
Math.max(0,
minScale * imageWidth / 2f
+ 0.5f * (displayWidth - (minScale * imageWidth))),
Math.max(0,
minScale * imageHeight / 2f
+ 0.5f * (displayHeight - (minScale * imageHeight))));
currentScale = minScale;
currentTranslation.set(initialTranslation);
initialImageRect.set(0, 0, imageWidth, imageHeight);
setImageTransformation(initialTranslation.x, initialTranslation.y,
minScale);
}
I am cheating here a bit because the pinch is not really centered between the user fingers, which is acceptable in my case.