Android开发获取ImageView显示的图片尺寸

时间:2025-01-31 08:15:47
    private ImageView imageView;
    private int realImgShowWidth, realImgShowHeight;

    private void getImgDisplaySize() {
        Drawable imgDrawable = ();
        if (imgDrawable != null) {
            //获得ImageView中Image的真实宽高,
            int dw = ().getBounds().width();
            int dh = ().getBounds().height();

            //获得ImageView中Image的变换矩阵
            Matrix m = ();
            float[] values = new float[10];
            (values);

            //Image在绘制过程中的变换矩阵,从中获得x和y方向的缩放系数
            float sx = values[0];
            float sy = values[4];

            //计算Image在屏幕上实际绘制的宽高
            realImgShowWidth = (int) (dw * sx);
            realImgShowHeight = (int) (dh * sy);
        }
    }