2

Hello Im using a function for fit to screen on this topic Android image view matrix scale + translate

float imageWidth = imageDetail.getDrawable().getIntrinsicWidth();
    float imageHeight = imageDetail.getDrawable().getIntrinsicHeight();
    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, imageDetail.getWidth(),
            imageDetail.getHeight());
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    imageDetail.setImageMatrix(matrix);
    imageDetail.invalidate();

It works effectively but not the first time. When I start the application It doesnt work. Image doesnt show. When I m tap It works. What Im do wrong? Thanks for any advice

Community
  • 1
  • 1
user3770295
  • 33
  • 1
  • 4

1 Answers1

4

Hi you should place your code in onWindowFocusChanged method.

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    float imageWidth = image.getDrawable().getIntrinsicWidth();
    float imageHeight = image.getDrawable().getIntrinsicHeight();
    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, image.getWidth(),
            image.getHeight());
    Matrix matrix = new Matrix();
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    image.setImageMatrix(matrix);
    image.invalidate();
}
sravanalakshmi.sunkara
  • 1,161
  • 2
  • 13
  • 35