@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private static ImageSize getImageViewSize(ImageView imageView) { ImageSize imageSize = new ImageSize(); DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics(); ViewGroup.LayoutParams lp = imageView.getLayoutParams(); /* * 获取图片压缩的宽和高 * */ int width = imageView.getWidth(); // 获取imageView的实际宽度 if (width <= 0) { width = lp.width; // 获取imageView在布局中设置的宽度 } if (width <= 0) { width = imageView.getMaxWidth(); } if (width <= 0) { width = displayMetrics.widthPixels; } imageSize.width = width; int height = imageView.getHeight(); // 获取imageView的实际宽度 if (height <= 0) { height = lp.height; // 获取imageView在布局中设置的宽度 } if (height <= 0) { height = imageView.getMaxHeight(); } if (height <= 0) { height = displayMetrics.heightPixels; } imageSize.height = height; return imageSize; }
/** * 根据ImageView获得适当的压缩的宽和高 * * @param imageView * @return */ private ImageSize getImageViewWidth(ImageView imageView) { ImageSize imageSize = new ImageSize(); final DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics(); final LayoutParams params = imageView.getLayoutParams(); int width = params.width == LayoutParams.WRAP_CONTENT ? 0 : imageView.getWidth(); // Get actual image width if (width <= 0) width = params.width; // Get layout width parameter if (width <= 0) width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check // maxWidth // parameter if (width <= 0) width = displayMetrics.widthPixels; int height = params.height == LayoutParams.WRAP_CONTENT ? 0 : imageView.getHeight(); // Get actual image height if (height <= 0) height = params.height; // Get layout height parameter if (height <= 0) height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check // maxHeight // parameter if (height <= 0) height = displayMetrics.heightPixels; imageSize.width = width; imageSize.height = height; return imageSize; }
/** * 根据ImageView获适当的压缩的宽和高 * * @param view * @return */ public static ImageSize getImageViewSize(View view) { ImageSize imageSize = new ImageSize(); imageSize.width = getExpectWidth(view); imageSize.height = getExpectHeight(view); return imageSize; }