Eine squareImageView mit angegebener Breite:
public class SquareImageViewByWidth extends AppCompatImageView {
public SquareImageViewByWidth(Context context) {
super(context);
}
public SquareImageViewByWidth(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageViewByWidth(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width= getMeasuredWidth();
setMeasuredDimension(width, width);
}
...
}
Eine squareImageView nach angegebener Höhe:
public class SquareImageViewByHeight extends AppCompatImageView {
public SquareImageViewByHeight(Context context) {
super(context);
}
public SquareImageViewByHeight(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageViewByHeight(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = getMeasuredHeight();
setMeasuredDimension(height, height);
}
...
}
Eine squareImageView mit einem Minimum an Dimensionen:
public class SquareImageViewByMin extends AppCompatImageView {
public SquareImageViewByHeight(Context context) {
super(context);
}
public SquareImageViewByHeight(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageViewByHeight(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int minSize = Math.min(width, height);
setMeasuredDimension(minSize, minSize);
}
...
}