Search in sources :

Example 1 with ScreenRatio

use of com.android.resources.ScreenRatio in project android by JetBrains.

the class DeviceDefinitionPreview method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    GraphicsUtil.setupAntialiasing(g);
    GraphicsUtil.setupAAPainting(g);
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(JBColor.background());
    g2d.fillRect(0, 0, getWidth(), getHeight());
    g2d.setColor(JBColor.foreground());
    g2d.setFont(STANDARD_FONT);
    if (myDeviceData.name().get().equals(DO_NOT_DISPLAY)) {
        FontMetrics metrics = g2d.getFontMetrics();
        g2d.drawString(NO_DEVICE_SELECTED, (getWidth() - metrics.stringWidth(NO_DEVICE_SELECTED)) / 2, (getHeight() - metrics.getHeight()) / 2);
        return;
    }
    boolean isCircular = myDeviceData.isWear().get() && myDeviceData.isScreenRound().get();
    // Paint our icon
    Icon icon = getIcon(myDeviceData);
    icon.paintIcon(this, g, PADDING / 2, PADDING / 2);
    // Paint the device name
    g2d.setFont(TITLE_FONT);
    FontMetrics metrics = g.getFontMetrics(TITLE_FONT);
    g2d.drawString(myDeviceData.name().get(), JBUI.scale(50), PADDING + metrics.getHeight() / 2);
    g2d.drawLine(0, JBUI.scale(50), getWidth(), JBUI.scale(50));
    // Paint the device outline with dimensions labelled
    Dimension screenSize = getScaledDimension();
    Dimension pixelScreenSize = myDeviceData.getDeviceScreenDimension();
    if (screenSize != null) {
        if (screenSize.getHeight() <= 0) {
            screenSize.height = 1;
        }
        if (screenSize.getWidth() <= 0) {
            screenSize.width = 1;
        }
        RoundRectangle2D roundRect = new RoundRectangle2D.Double(PADDING, JBUI.scale(100), screenSize.width, screenSize.height, JBUI.scale(10), JBUI.scale(10));
        g2d.setStroke(new BasicStroke(DIMENSION_LINE_WIDTH));
        g2d.setColor(OUR_GRAY);
        g2d.setFont(FIGURE_FONT);
        metrics = g2d.getFontMetrics(FIGURE_FONT);
        int stringHeight = metrics.getHeight() - metrics.getDescent();
        // Paint the width dimension
        String widthString = Integer.toString(pixelScreenSize.width) + "px";
        int widthLineY = JBUI.scale(95) - (metrics.getHeight() - metrics.getDescent()) / 2;
        g2d.drawLine(PADDING, widthLineY, round(PADDING + screenSize.width), widthLineY);
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        int widthStringWidth = metrics.stringWidth(widthString);
        int widthTextX = round(PADDING + (screenSize.width - widthStringWidth) / 2);
        g2d.drawLine(widthTextX - FIGURE_PADDING, widthLineY, widthTextX + widthStringWidth + FIGURE_PADDING, widthLineY);
        // Paint the width text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(widthString, widthTextX, JBUI.scale(95));
        // Paint the height dimension
        g2d.setColor(OUR_GRAY);
        String heightString = Integer.toString(pixelScreenSize.height) + "px";
        int heightLineX = round(PADDING + screenSize.width + JBUI.scale(15));
        g2d.drawLine(heightLineX, JBUI.scale(100), heightLineX, round(JBUI.scale(100) + screenSize.height));
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        int heightTextY = round(JBUI.scale(100) + (screenSize.height + stringHeight) / 2);
        g2d.drawLine(heightLineX, heightTextY + FIGURE_PADDING, heightLineX, heightTextY - stringHeight - FIGURE_PADDING);
        // Paint the height text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(heightString, heightLineX - JBUI.scale(10), heightTextY);
        // Paint the diagonal dimension
        g2d.setColor(OUR_GRAY);
        String diagString = FORMAT.format(myDeviceData.diagonalScreenSize().get());
        int diagTextX = round(PADDING + (screenSize.width - metrics.stringWidth(diagString)) / 2);
        int diagTextY = round(JBUI.scale(100) + (screenSize.height + stringHeight) / 2);
        double chin = (double) myDeviceData.screenChinSize().get();
        chin *= screenSize.getWidth() / myDeviceData.getDeviceScreenDimension().getWidth();
        Line2D diagLine = new Line2D.Double(PADDING, JBUI.scale(100) + screenSize.height + chin, PADDING + screenSize.width, JBUI.scale(100));
        if (isCircular) {
            // Move the endpoints of the line to within the circle. Each endpoint must move towards the center axis of the circle by
            // 0.5 * (l - l/sqrt(2)) where l is the diameter of the circle.
            double dist = 0.5 * (screenSize.width - screenSize.width / Math.sqrt(2));
            diagLine.setLine(diagLine.getX1() + dist, diagLine.getY1() - dist, diagLine.getX2() - dist, diagLine.getY2() + dist);
        }
        g2d.draw(diagLine);
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        Rectangle erasureRect = new Rectangle(diagTextX - FIGURE_PADDING, diagTextY - stringHeight - FIGURE_PADDING, metrics.stringWidth(diagString) + FIGURE_PADDING * 2, stringHeight + FIGURE_PADDING * 2);
        g2d.fill(erasureRect);
        // Paint the diagonal text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(diagString, diagTextX, diagTextY);
        // Finally, paint the outline
        g2d.setStroke(new BasicStroke(OUTLINE_LINE_WIDTH));
        g2d.setColor(JBColor.foreground());
        if (isCircular) {
            double x = roundRect.getX();
            double y = roundRect.getY();
            Ellipse2D circle = new Ellipse2D.Double(x, y, screenSize.width, screenSize.height + chin);
            g2d.draw(circle);
            if (chin > 0) {
                erasureRect = new Rectangle((int) x, (int) (y + screenSize.height + OUTLINE_LINE_WIDTH / 2 + 1), screenSize.width, (int) chin + OUTLINE_LINE_WIDTH / 2 + 1);
                g2d.setColor(JBColor.background());
                g2d.fill(erasureRect);
                g2d.setColor(JBColor.foreground());
                double halfChinWidth = Math.sqrt(chin * (screenSize.width - chin)) - OUTLINE_LINE_WIDTH / 2;
                int chinX = (int) (x + screenSize.width / 2 - halfChinWidth);
                g2d.drawLine(chinX, (int) (y + screenSize.height), (int) (chinX + halfChinWidth * 2), (int) (y + screenSize.height));
            }
        } else {
            g2d.draw(roundRect);
        }
        // Paint the details. If it's a portrait phone, then paint to the right of the rect.
        // If it's a landscape tablet/tv, paint below.
        g2d.setFont(STANDARD_FONT);
        metrics = g2d.getFontMetrics(STANDARD_FONT);
        stringHeight = metrics.getHeight();
        int infoSegmentX;
        int infoSegmentY;
        if (myDeviceData.getDefaultDeviceOrientation().equals(ScreenOrientation.PORTRAIT)) {
            infoSegmentX = round(PADDING + screenSize.width + metrics.stringWidth(heightString) + PADDING);
            infoSegmentY = JBUI.scale(100);
        } else {
            infoSegmentX = PADDING;
            infoSegmentY = round(JBUI.scale(100) + screenSize.height + PADDING);
        }
        infoSegmentY += stringHeight;
        ScreenSize size = AvdScreenData.getScreenSize(myDeviceData.diagonalScreenSize().get());
        g2d.drawString("Size:      " + size.getResourceValue(), infoSegmentX, infoSegmentY);
        infoSegmentY += stringHeight;
        ScreenRatio ratio = AvdScreenData.getScreenRatio(myDeviceData.screenResolutionWidth().get(), myDeviceData.screenResolutionHeight().get());
        g2d.drawString("Ratio:    " + ratio.getResourceValue(), infoSegmentX, infoSegmentY);
        infoSegmentY += stringHeight;
        Density pixelDensity = myDeviceData.density().get();
        if (pixelDensity == Density.NODPI) {
            // We need to calculate the density
            pixelDensity = AvdScreenData.getScreenDensity(myDeviceData.isTv().get(), myDeviceData.screenDpi().get(), myDeviceData.screenResolutionHeight().get());
        }
        g2d.drawString("Density: " + pixelDensity.getResourceValue(), infoSegmentX, infoSegmentY);
    }
}
Also used : RoundRectangle2D(java.awt.geom.RoundRectangle2D) ScreenRatio(com.android.resources.ScreenRatio) Line2D(java.awt.geom.Line2D) Density(com.android.resources.Density) Ellipse2D(java.awt.geom.Ellipse2D) ScreenSize(com.android.resources.ScreenSize)

Example 2 with ScreenRatio

use of com.android.resources.ScreenRatio in project android by JetBrains.

the class CanvasResizeInteraction method coveredAreaForConfig.

/**
   * Returns the {@link Area} of the {@link ScreenView} that is covered by the given {@link FolderConfiguration}
   */
@SuppressWarnings("SuspiciousNameCombination")
@NotNull
private Area coveredAreaForConfig(@NotNull FolderConfiguration config, @NotNull ScreenView screenView) {
    int x0 = screenView.getX();
    int y0 = screenView.getY();
    JComponent layeredPane = myDesignSurface.getLayeredPane();
    int width = layeredPane.getWidth();
    int height = layeredPane.getHeight();
    int maxDim = Math.max(width, height);
    int minX = 0;
    int maxX = -1;
    int minY = 0;
    int maxY = -1;
    int dpi = screenView.getConfiguration().getDensity().getDpiValue();
    SmallestScreenWidthQualifier smallestWidthQualifier = config.getSmallestScreenWidthQualifier();
    if (smallestWidthQualifier != null) {
        // Restrict the area due to a sw<N>dp qualifier
        minX = smallestWidthQualifier.getValue() * dpi / 160;
        minY = smallestWidthQualifier.getValue() * dpi / 160;
    }
    ScreenWidthQualifier widthQualifier = config.getScreenWidthQualifier();
    if (widthQualifier != null) {
        // Restrict the area due to a w<N>dp qualifier
        minX = Math.max(minX, widthQualifier.getValue() * dpi / 160);
    }
    ScreenHeightQualifier heightQualifier = config.getScreenHeightQualifier();
    if (heightQualifier != null) {
        // Restrict the area due to a h<N>dp qualifier
        minY = Math.max(minY, heightQualifier.getValue() * dpi / 160);
    }
    ScreenSizeQualifier sizeQualifier = config.getScreenSizeQualifier();
    if (sizeQualifier != null && sizeQualifier.getValue() != null) {
        // Restrict the area due to a screen size qualifier (SMALL, NORMAL, LARGE, XLARGE)
        switch(sizeQualifier.getValue()) {
            case SMALL:
                maxX = 320 * dpi / 160;
                maxY = 470 * dpi / 160;
                break;
            case NORMAL:
                break;
            case LARGE:
                minX = 480 * dpi / 160;
                minY = 640 * dpi / 160;
                break;
            case XLARGE:
                minX = 720 * dpi / 160;
                minY = 960 * dpi / 160;
                break;
        }
    }
    ScreenRatioQualifier ratioQualifier = config.getScreenRatioQualifier();
    ScreenRatio ratio = ratioQualifier != null ? ratioQualifier.getValue() : null;
    ScreenOrientationQualifier orientationQualifier = config.getScreenOrientationQualifier();
    ScreenOrientation orientation = orientationQualifier != null ? orientationQualifier.getValue() : null;
    Polygon portrait = new Polygon();
    Polygon landscape = new Polygon();
    if (orientation == null || orientation.equals(ScreenOrientation.PORTRAIT)) {
        constructPolygon(portrait, ratio, maxDim, true);
        portrait.translate(x0, y0);
    }
    if (orientation == null || orientation.equals(ScreenOrientation.LANDSCAPE)) {
        constructPolygon(landscape, ratio, maxDim, false);
        landscape.translate(x0, y0);
    }
    Area portraitArea = new Area(portrait);
    Area landscapeArea = new Area(landscape);
    Area portraitBounds = new Area(new Rectangle(Coordinates.getSwingX(screenView, minX), Coordinates.getSwingY(screenView, minY), maxX >= 0 ? Coordinates.getSwingDimension(screenView, maxX - minX) : width, maxY >= 0 ? Coordinates.getSwingDimension(screenView, maxY - minY) : height));
    Area landscapeBounds = new Area(new Rectangle(Coordinates.getSwingX(screenView, minY), Coordinates.getSwingY(screenView, minX), maxY >= 0 ? Coordinates.getSwingDimension(screenView, maxY - minY) : width, maxX >= 0 ? Coordinates.getSwingDimension(screenView, maxX - minX) : height));
    portraitArea.intersect(portraitBounds);
    landscapeArea.intersect(landscapeBounds);
    portraitArea.add(landscapeArea);
    return portraitArea;
}
Also used : ScreenRatio(com.android.resources.ScreenRatio) ScreenOrientation(com.android.resources.ScreenOrientation) Area(java.awt.geom.Area) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ScreenRatio (com.android.resources.ScreenRatio)2 Density (com.android.resources.Density)1 ScreenOrientation (com.android.resources.ScreenOrientation)1 ScreenSize (com.android.resources.ScreenSize)1 Area (java.awt.geom.Area)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Line2D (java.awt.geom.Line2D)1 RoundRectangle2D (java.awt.geom.RoundRectangle2D)1 NotNull (org.jetbrains.annotations.NotNull)1