The imageveiw extension may be the cleanest solution. This works for me:
public class Icon extends ImageView { public Icon(final Context context) { super(context); } public Icon(final Context context, final AttributeSet attrs) { super(context, attrs); } public Icon(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int width, int height) { super.onMeasure(width, height); int measuredWidth = getMeasuredWidth(); int measuredHeight = getMeasuredHeight(); if (measuredWidth > measuredHeight) { setMeasuredDimension(measuredHeight, measuredHeight); } else { setMeasuredDimension(measuredWidth, measuredWidth); } } }
And in xml:
<com.your_package_name.Icon android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" android:src="@drawable/screen" />
Illegal argument
source share