Created
May 15, 2014 04:03
-
-
Save ypresto/5d3e2f49ff2b09a3b48b to your computer and use it in GitHub Desktop.
Keep aspect ratio to 1:1. Not tested.
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
| package net.ypresto.utils.squareview; | |
| import android.content.Context; | |
| import android.util.AttributeSet; | |
| import android.widget.FrameLayout; | |
| /** | |
| * FrameLayout enforces its aspect ratio to 1:1. | |
| * NOTE: wrap_content is not supported. | |
| */ | |
| public class SqaureFrameLayout extends FrameLayout { | |
| public SqaureFrameLayout(Context context) { | |
| super(context); | |
| } | |
| public SqaureFrameLayout(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| public SqaureFrameLayout(Context context, AttributeSet attrs, int defStyle) { | |
| super(context, attrs, defStyle); | |
| } | |
| @Override | |
| protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| boolean isWidthUnspecified = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED; | |
| boolean isHeightUnspecified = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED; | |
| if (isWidthUnspecified && isHeightUnspecified) { | |
| // Unsupported | |
| super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
| return; | |
| } | |
| int width = isWidthUnspecified ? Integer.MAX_VALUE : MeasureSpec.getSize(widthMeasureSpec); | |
| int height = isHeightUnspecified ? Integer.MAX_VALUE : MeasureSpec.getSize(heightMeasureSpec); | |
| int measureSpec = MeasureSpec.makeMeasureSpec(Math.min(width, height), MeasureSpec.EXACTLY); | |
| super.onMeasure(measureSpec, measureSpec); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment