Created
March 16, 2016 15:00
-
-
Save ayvazj/1b12829a7f883f170358 to your computer and use it in GitHub Desktop.
BtfyAnimationView
This file contains hidden or 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
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