0
\$\begingroup\$

This is good practice, to make square image? Side of the square container is equal to its width.

public class SquareImageView extends ImageView{
 public SquareImageView(Context context) {
 super(context);
 }
 public SquareImageView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, widthMeasureSpec);
 }
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 17, 2016 at 20:16
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If your project needs it - it's good. I did the same thing some time ago and you must remember that sometimes widthMeasureSpec or heightMeasureSpec can be very small, so I suggest that you check those values, choose the bigger one and then call super.onMeasure().

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 if (widthMeasureSpec < 1) {
 super.onMeasure(heightMeasureSpec, heightMeasureSpec);
 } else {
 super.onMeasure(widthMeasureSpec, widthMeasureSpec);
 }
 }
mdfst13
22.4k6 gold badges34 silver badges70 bronze badges
answered Jun 18, 2016 at 11:50
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.