How do I keep the aspect ratio on image buttons in android?

<LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:id=”@+id/layoutButtons”> <com.package.SquareButton android:layout_height=”fill_parent” android:layout_width=”0dip” android:layout_weight=”1″ <ImageView android:id=”@+id/box1″ android:layout_gravity=”center” android:adjustViewBounds=”true” android:scaleType=”centerInside” android:layout_height=”wrap_content” android:layout_width=”0dp” android:layout_weight=”1″ android:layout_marginLeft=”5dp” android:layout_marginRight=”5dp”/> </com.package.SquareButton> <com.package.SquareButton android:layout_height=”fill_parent” android:layout_width=”0dip” android:layout_weight=”1″ <ImageView android:id=”@+id/box2″ android:layout_gravity=”center” android:adjustViewBounds=”true” android:scaleType=”centerInside” android:layout_height=”fill_parent” android:layout_width=”fill_parent” android:layout_marginLeft=”5dp” android:layout_marginRight=”5dp”/> </com.package.SquareButton> ……… </LinearLayout> And then add this custom button class: public class SquareButton extends LinearLayout { public SquareButton(Context context) { super(context); } public … Read more

How to auto-resize an image while maintaining aspect ratio

Do not apply an explicit width or height to the image tag. Instead, give it: max-width:100%; max-height:100%; Also, height: auto; if you want to specify a width only. Example: http://jsfiddle.net/xwrvxser/1/ img { max-width: 100%; max-height: 100%; } .portrait { height: 80px; width: 30px; } .landscape { height: 30px; width: 80px; } .square { height: 75px; … Read more

VideoView to match parent height and keep aspect ratio

You should extends from the built-in video view. Call setVideoSize before video view is shown, you can get video size from thumbnail extracted from video. So that, when video view’s onMeasure is called, both mVideoWidth & mVideoHeight are > 0. If you want to account the height of controllers, you can do it yourself in … Read more

Padding-bottom/top in flexbox layout

Update September 2020 Firefox and edge have implemented the behaviour from the specs and margin + padding for flex elements are both calculated according to the width of the containing block. Just like block elements. Update February 2018 Firefox and edge have agreed to change their behaviour on top, bottom margin and padding for flex … Read more

How to scale SVG image to fill browser window?

How about: html, body { margin:0; padding:0; overflow:hidden } svg { position:fixed; top:0; bottom:0; left:0; right:0 } Or: html, body { margin:0; padding:0; overflow:hidden } svg { position:fixed; top:0; left:0; height:100%; width:100% } I have an example on my site using (roughly) this technique, albeit with 5% padding all around, and using position:absolute instead of … Read more

Maintain aspect ratio according to width and height

The aspect-ratio property (2022) To maintain the aspect ratio of a div according to width and height, you can use the aspect-ratio property (MDN reference). This allows you to maintain any aspect ratio according to the viewport size or to the size of the parent element. Maintaining aspect-ratio according to the viewport size (width and … Read more