Skip to content

Commit

Permalink
计算resize的时候减去padding
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopan committed Jan 13, 2015
1 parent 7dad5d1 commit 85def15
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class DefaultImageSizeCalculator implements ImageSizeCalculator{

@Override
public ImageSize calculateImageMaxsize(ImageView imageView) {
int width = getWidth(imageView, true, true);
int height = getHeight(imageView, true, true);
int width = getWidth(imageView, true, true, false);
int height = getHeight(imageView, true, true, false);
if (width > 0 || height > 0){
return new ImageSize(width, height);
}else{
Expand All @@ -42,8 +42,8 @@ public ImageSize calculateImageMaxsize(ImageView imageView) {

@Override
public ImageSize calculateImageResize(ImageView imageView) {
int width = getWidth(imageView, false, false);
int height = getHeight(imageView, false, false);
int width = getWidth(imageView, false, false, true);
int height = getHeight(imageView, false, false, true);
if (width > 0 && height > 0){
return new ImageSize(width, height);
}else{
Expand Down Expand Up @@ -104,7 +104,7 @@ public int calculateInSampleSize(int outWidth, int outHeight, int targetWidth, i
return inSampleSize;
}

public static int getWidth(ImageView imageView, boolean checkMaxViewSize, boolean acceptWrapContent) {
public static int getWidth(ImageView imageView, boolean checkMaxWidth, boolean acceptWrapContent, boolean subtractPadding) {
if(imageView == null){
return 0;
}
Expand All @@ -113,8 +113,12 @@ public static int getWidth(ImageView imageView, boolean checkMaxViewSize, boolea
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
if (params != null){
width = params.width;
if(subtractPadding && width > 0 && (width - imageView.getPaddingLeft() - imageView.getPaddingRight()) > 0){
width -= imageView.getPaddingLeft()+imageView.getPaddingRight();
return width;
}
}
if(width <= 0 && checkMaxViewSize){
if(width <= 0 && checkMaxWidth){
width = getImageViewFieldValue(imageView, "mMaxWidth");
}
if(width <= 0 && acceptWrapContent && params != null && params.width == ViewGroup.LayoutParams.WRAP_CONTENT){
Expand All @@ -123,7 +127,7 @@ public static int getWidth(ImageView imageView, boolean checkMaxViewSize, boolea
return width;
}

public static int getHeight(ImageView imageView, boolean checkMaxViewSize, boolean acceptWrapContent) {
public static int getHeight(ImageView imageView, boolean checkMaxHeight, boolean acceptWrapContent, boolean subtractPadding) {
if(imageView == null){
return 0;
}
Expand All @@ -132,8 +136,12 @@ public static int getHeight(ImageView imageView, boolean checkMaxViewSize, boole
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
if (params != null){
height = params.height;
if(subtractPadding && height > 0 && (height - imageView.getPaddingTop() - imageView.getPaddingBottom()) > 0){
height -= imageView.getPaddingTop()+imageView.getPaddingBottom();
return height;
}
}
if(height <= 0 && checkMaxViewSize){
if(height <= 0 && checkMaxHeight){
height = getImageViewFieldValue(imageView, "mMaxHeight");
}
if(height <= 0 && acceptWrapContent && params != null && params.height == ViewGroup.LayoutParams.WRAP_CONTENT){
Expand Down

0 comments on commit 85def15

Please sign in to comment.