Created
September 19, 2011 14:53
-
-
Save codeincontext/1226678 to your computer and use it in GitHub Desktop.
Android square layout
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 com.cube.vision; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.LinearLayout; | |
public class SquareLayout extends LinearLayout { | |
public SquareLayout(Context context) { | |
super(context); | |
} | |
public SquareLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = MeasureSpec.getSize(widthMeasureSpec); | |
int height = MeasureSpec.getSize(heightMeasureSpec); | |
int mScale = 1; | |
if (width < (int)(mScale * height + 0.5)) { | |
width = (int)(mScale * height + 0.5); | |
} else { | |
height = (int)(width / mScale + 0.5); | |
} | |
super.onMeasure( | |
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), | |
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!