Skip to content

Instantly share code, notes, and snippets.

@easternHong
Last active December 8, 2015 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easternHong/19f553ffc34614daf7b0 to your computer and use it in GitHub Desktop.
Save easternHong/19f553ffc34614daf7b0 to your computer and use it in GitHub Desktop.
Android View Measure
//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