将图片转换成一个ImageView大小,如何获取这个ImageView的大小

从开源项目ImageLoader中摘出:

/**
     * Defines image size for loading at memory (for memory economy) by {@link ImageView} parameters.<br />
     * Size computing algorithm:<br />
     * 1) Get <b>maxWidth</b> and <b>maxHeight</b>. If both of them are not set then go to step #2.<br />
     * 2) Get <b>layout_width</b> and <b>layout_height</b>. If both of them haven't exact value then go to step #3.</br>
     * 3) Get device screen dimensions.
     */
    private ImageSize getImageSizeScaleTo(ImageView imageView) {
        int width = -1;
        int height = -1;

        // Check maxWidth and maxHeight parameters
        try {
            Field maxWidthField = ImageView.class.getDeclaredField("mMaxWidth");
            Field maxHeightField = ImageView.class.getDeclaredField("mMaxHeight");
            maxWidthField.setAccessible(true);
            maxHeightField.setAccessible(true);
            int maxWidth = (Integer) maxWidthField.get(imageView);
            int maxHeight = (Integer) maxHeightField.get(imageView);

            if (maxWidth >= 0 && maxWidth < Integer.MAX_VALUE) {
                width = maxWidth;
            }
            if (maxHeight >= 0 && maxHeight < Integer.MAX_VALUE) {
                height = maxHeight;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }

        if (width < 0 && height < 0) {
            // Get layout width and height parameters
            LayoutParams params = imageView.getLayoutParams();
            width = params.width;
            height = params.height;
        }

        if (width < 0 && height < 0) {
            // Get device screen dimensions
            width = configuration.maxImageWidthForMemoryCache;
            height = configuration.maxImageHeightForMemoryCache;

            // Consider device screen orientation
            int screenOrientation = imageView.getContext().getResources().getConfiguration().orientation;
            if ((screenOrientation == Configuration.ORIENTATION_PORTRAIT && width > height)
                    || (screenOrientation == Configuration.ORIENTATION_LANDSCAPE && width < height)) {
                int tmp = width;
                width = height;
                height = tmp;
            }
        }
        return new ImageSize(width, height);
    }

你可能感兴趣的:(将图片转换成一个ImageView大小,如何获取这个ImageView的大小)