Skip to content

Instantly share code, notes, and snippets.

@ayvazj
Created March 16, 2016 15:00
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 ayvazj/1b12829a7f883f170358 to your computer and use it in GitHub Desktop.
Save ayvazj/1b12829a7f883f170358 to your computer and use it in GitHub Desktop.
BtfyAnimationView
public class BtfyAnimationView extends ViewGroup {
// ... load .btfy into a BtfyStage object at property this.btfyStage
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (this.btfyStage == null) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
return;
}
BtfySize size = this.btfyStage.sizeInfo;
int resolveSizeWidth = resolveSize(size.width.intValue(), widthMeasureSpec);
int resolveSizeHeight = resolveSize(size.height.intValue(), heightMeasureSpec);
float sizeRatio;
if (!(resolveSizeWidth == size.width && resolveSizeHeight == size.height)) {
sizeRatio = size.width / size.height;
float resolvedRatio = ((float) resolveSizeWidth) / ((float) resolveSizeHeight);
if (resolvedRatio > sizeRatio) {
resolveSizeWidth = Math.round(((float) resolveSizeHeight) * sizeRatio);
} else if (resolvedRatio < sizeRatio) {
resolveSizeHeight = Math.round(((float) resolveSizeWidth) / sizeRatio);
}
}
int measuredWidth;
if (resolveSizeWidth < MeasureSpec.getSize(widthMeasureSpec)
|| resolveSizeHeight < MeasureSpec.getSize(heightMeasureSpec)) {
sizeRatio = (float) Math.min(MeasureSpec.getSize(widthMeasureSpec) / resolveSizeWidth,
MeasureSpec.getSize(heightMeasureSpec) / resolveSizeHeight);
measuredWidth = (int) (((float) resolveSizeWidth) * sizeRatio);
resolveSizeWidth = (int) (((float) resolveSizeHeight) * sizeRatio);
} else {
measuredWidth = resolveSizeWidth;
resolveSizeWidth = resolveSizeHeight;
}
setMeasuredDimension(measuredWidth, resolveSizeWidth);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment