Search in sources :

Example 11 with ImageView

use of org.apache.pivot.wtk.ImageView in project pivot by apache.

the class ImageViewSkin method layout.

@Override
public void layout() {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    if (image != null) {
        int width = getWidth();
        int height = getHeight();
        Dimensions imageSize = image.getSize();
        if (fill) {
            // Scale to fit
            if (preserveAspectRatio) {
                float aspectRatio = (float) width / (float) height;
                float imageAspectRatio = (float) imageSize.width / (float) imageSize.height;
                if (aspectRatio > imageAspectRatio) {
                    imageY = 0;
                    scaleY = (float) height / (float) imageSize.height;
                    imageX = (int) (width - imageSize.width * scaleY) / 2;
                    scaleX = scaleY;
                } else {
                    imageX = 0;
                    scaleX = (float) width / (float) imageSize.width;
                    imageY = (int) (height - imageSize.height * scaleX) / 2;
                    scaleY = scaleX;
                }
            } else {
                imageX = 0;
                scaleX = (float) width / (float) imageSize.width;
                imageY = 0;
                scaleY = (float) height / (float) imageSize.height;
            }
        } else {
            if (horizontalAlignment == HorizontalAlignment.CENTER) {
                imageX = (width - imageSize.width) / 2;
            } else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
                imageX = width - imageSize.width;
            } else {
                imageX = 0;
            }
            scaleX = 1.0f;
            if (verticalAlignment == VerticalAlignment.CENTER) {
                imageY = (height - imageSize.height) / 2;
            } else if (verticalAlignment == VerticalAlignment.BOTTOM) {
                imageY = height - imageSize.height;
            } else {
                imageY = 0;
            }
            scaleY = 1.0f;
        }
    }
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image)

Aggregations

ImageView (org.apache.pivot.wtk.ImageView)11 Image (org.apache.pivot.wtk.media.Image)8 Dimensions (org.apache.pivot.wtk.Dimensions)3 Component (org.apache.pivot.wtk.Component)2 Label (org.apache.pivot.wtk.Label)2 PushButton (org.apache.pivot.wtk.PushButton)2 TablePane (org.apache.pivot.wtk.TablePane)2 SVGElementException (com.kitfox.svg.SVGElementException)1 SVGException (com.kitfox.svg.SVGException)1 AlphaComposite (java.awt.AlphaComposite)1 Composite (java.awt.Composite)1 Graphics2D (java.awt.Graphics2D)1 IOException (java.io.IOException)1 URL (java.net.URL)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BoxPane (org.apache.pivot.wtk.BoxPane)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 Checkbox (org.apache.pivot.wtk.Checkbox)1 DragSource (org.apache.pivot.wtk.DragSource)1