Android image view matrix scale + translate

后端 未结 4 1987
执笔经年
执笔经年 2020-11-28 04:52

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

4条回答
  •  感动是毒
    2020-11-28 05:32

    There's a convenient method called Matrix.setRectToRect(RectF, RectF, ScaleToFit) to help you here.

    Matrix m = imageView.getImageMatrix();
    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight());
    m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    imageView.setImageMatrix(m);
    

    That should set the matrix m to have combo of scaling and translate values that is needed to show the drawable centered and fit within the ImageView widget.

提交回复
热议问题