Last active
December 8, 2015 13:52
-
-
Save easternHong/19f553ffc34614daf7b0 to your computer and use it in GitHub Desktop.
Android View Measure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://github.com/devunwired/custom-view-examples/tree/master/app/src/main/java/com/example/customview/widget | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false)); | |
} | |
private int measure(int measureSpec, boolean isWidth) { | |
int result; | |
int mode = MeasureSpec.getMode(measureSpec); | |
int size = MeasureSpec.getSize(measureSpec); | |
int padding = isWidth ? getPaddingLeft() + getPaddingRight() + foreCircleRadius : getPaddingTop() + getPaddingBottom() + foreCircleRadius; | |
if (mode == MeasureSpec.EXACTLY) { | |
result = size; | |
} else { | |
result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight(); | |
result += padding; | |
//Get the width based on the measure specs | |
result += View.resolveSize(result, measureSpec); | |
if (mode == MeasureSpec.AT_MOST) { | |
if (isWidth) { | |
result = Math.min(result, size); | |
} else { | |
result = Math.min(result, size); | |
} | |
} | |
} | |
return result; | |
//要是ImageView,就需要measureDrawable的宽与高. | |
// Drawable d = getDrawable(); | |
// if (d == null) { | |
// desiredSize = 0; | |
// aspect = 1f; | |
// } else { | |
// desiredSize = d.getIntrinsicWidth(); | |
// aspect = (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight(); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment