Skip to content

Instantly share code, notes, and snippets.

@DSdatsme
Created March 29, 2018 10:15
Show Gist options
  • Save DSdatsme/0d97bbbc45fca0975a70dadeb5138c80 to your computer and use it in GitHub Desktop.
Save DSdatsme/0d97bbbc45fca0975a70dadeb5138c80 to your computer and use it in GitHub Desktop.
this is for generating square images which always takes width form parent and sets height equal to width
public class MySquareImages extends android.support.v7.widget.AppCompatImageView {
public MySquareImages(Context context) {
super(context);
}
public MySquareImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySquareImages(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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.dsdatsme.instaclone.Utils.MySquareImages
android:src="@drawable/img5"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_weight="1"/>
<com.example.dsdatsme.instaclone.Utils.MySquareImages
android:src="@drawable/img5"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_weight="1"/>
<com.example.dsdatsme.instaclone.Utils.MySquareImages
android:src="@drawable/img5"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_weight="1"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment