Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Created September 19, 2011 14:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeincontext/1226678 to your computer and use it in GitHub Desktop.
Save codeincontext/1226678 to your computer and use it in GitHub Desktop.
Android square layout
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)
);
}
}
@RockyLin
Copy link

RockyLin commented Jun 5, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment