How to maintain widgets aspect ratio in Qt?

You don’t have to implement your own layout manager. You can do with inheriting QWidget and reimplementing int QWidget::heightForWidth( int w ) { return w; } to stay square. However, heightForWidth() doesn’t work on toplevel windows on X11, since apparently the X11 protocol doesn’t support that. As for centering, you can pass Qt::AlignCenter as the … Read more

Android Camera Preview Stretched

I’m using this method -> based on API Demos to get my Preview Size: private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 0.1; double targetRatio=(double)h / w; if (sizes == null) return null; Camera.Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; for (Camera.Size size : sizes) … Read more

Maintain the aspect ratio of a div with CSS

Just create a wrapper <div> with a percentage value for padding-bottom, like this: .demoWrapper { padding: 10px; background: white; box-sizing: border-box; resize: horizontal; border: 1px dashed; overflow: auto; max-width: 100%; height: calc(100vh – 16px); } div { width: 100%; padding-bottom: 75%; background: gold; /** <– For the demo **/ } <div class=”demoWrapper”> <div></div> </div> It … Read more

CSS force image resize and keep aspect ratio

img { display: block; max-width:230px; max-height:95px; width: auto; height: auto; } <p>This image is originally 400×400 pixels, but should get resized by the CSS:</p> <img width=”400″ height=”400″ src=”http://i.stack.imgur.com/aEEkn.png”> This will make image shrink if it’s too big for specified area (as downside, it will not enlarge image).

Java: maintaining aspect ratio of JPanel background image

Well, the quickest and easiest solution is to use Image.getScaledInstance g.drawImage(img.getScaledInstance(newWidth, -1, Image. SCALE_SMOOTH), x, y, this); If your wondering about the negative number, the java docs say: If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width … Read more