java - Is this a good method to make ImageView square? -


parent layout has specified width value (50dp), , height set match_parent. in layout adding few imageview widgets using java, , because parent layout has specified width, can set width , height of each imageview match_parent , using java can make imageview square setting height width.

this code:

public class icon extends imageview {      public appicon(final context context)     {         super(context);     }      public appicon(final context context, final attributeset attrs)     {         super(context, attrs);     }      public appicon(final context context, final attributeset attrs, final int defstyle)     {         super(context, attrs, defstyle);     }      @override     protected void onmeasure(final int width, final int height)     {         setmeasureddimension(width, width);     }  } 

is method make each imageview square? think it's simple, missing.

p.s.: method works, need sure, fine.

your code working 1 can enhanced. see code: updated code.

the image scaled based on measured width , height , whichever smaller.

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);      }  }  } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -