Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

positioning an matrix image at the center of imageview

I've created a gallery of images and everything works fine. To zoom in and move an image is required

ImgView.setScaleType (ImageView.ScaleType.MATRIX)

and when I do it, the image I got is small, so I called the method

scaleFactor = view.getWidth()/(float)view.getDrawable().getIntrinsicWidth();
 matrix.setScale(scaleFactor, scaleFactor);

so do not miss the image size but as you can see, the image rises above everything and need to stay in the center of the screen.

I tried this

matrix.postTranslate((screen_width-image_width)/2, (screen_height-image_height)/2);

but don't work.

Any idea? Sorry for not inserting images, but I can not because my reputation. Thank you very much in advance

like image 502
sergio Avatar asked Jan 21 '13 19:01

sergio


2 Answers

Ok, I've corrected the error, just had to put the following lines of code:

RectF drawableRect = new RectF(0, 0, image_width, image_height);
RectF viewRect = new RectF(0, 0, screen_width, screen_height);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

Hopefully someone help, thank you very much for answering.

like image 74
sergio Avatar answered Sep 28 '22 00:09

sergio


@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    screenWidth = layout.getWidth();
    screenHeight = layout.getHeight();

    Log.e("", "Image Width : " + imageWidth + " > " + imageHeight);
    Log.e("", "screen Width : " + screenWidth + " > " + screenHeight);

    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, screenWidth, screenHeight);
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    img.setImageMatrix(matrix);
}
like image 38
Chirag Avatar answered Sep 28 '22 02:09

Chirag