com.android.resources.Density Java Examples

The following examples show how to use com.android.resources.Density. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HardwareConfig.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
public HardwareConfig(
        int screenWidth,
        int screenHeight,
        Density density,
        float xdpi,
        float ydpi,
        ScreenSize screenSize,
        ScreenOrientation orientation,
        ScreenRound screenRoundness,
        boolean softwareButtons) {
    mScreenWidth = screenWidth;
    mScreenHeight = screenHeight;
    mDensity = density;
    mXdpi = xdpi;
    mYdpi = ydpi;
    mScreenSize = screenSize;
    mOrientation = orientation;
    mScreenRound = screenRoundness;
    mSoftwareButtons = softwareButtons;
}
 
Example #2
Source File: AvdManager.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
String getResolution(AvdInfo info) {
    Device device = deviceManager.getDevice(info.getDeviceName(), info.getDeviceManufacturer());
    Dimension res = null;
    Density density = null;
    if (device != null) {
        res = device.getScreenSize(device.getDefaultState().getOrientation());
        density = device.getDefaultHardware().getScreen().getPixelDensity();
    }
    String resolution;
    String densityString = density == null ? "Unknown Density" : density.getResourceValue();
    if (res != null) {
        resolution = String.format(Locale.getDefault(), "%1$d \u00D7 %2$d: %3$s", res.width, res.height, densityString);
    } else {
        resolution = "Unknown Resolution";
    }
    return resolution;
}
 
Example #3
Source File: HardwareConfig.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public HardwareConfig(
        int screenWidth,
        int screenHeight,
        Density density,
        float xdpi,
        float ydpi,
        ScreenSize screenSize,
        ScreenOrientation orientation,
        boolean softwareButtons) {
    mScreenWidth = screenWidth;
    mScreenHeight = screenHeight;
    mDensity = density;
    mXdpi = xdpi;
    mYdpi = ydpi;
    mScreenSize = screenSize;
    mOrientation = orientation;
    mSoftwareButtons = softwareButtons;
}
 
Example #4
Source File: AvdHwProfile.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
public static boolean isRecommended(Density d) {
    switch (d.getDpiValue()) {
        case 213:
        case 260:
        case 280:
        case 300:
        case 340:
        case 360:
        case 400:
        case 420:
        case 560:
            return false;
        default:
            return true;
    }
}
 
Example #5
Source File: AvdHwProfile.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
public static Density getScreenDensity(boolean isTv, double dpi, int screenHeight) {
    Density bucket = Density.MEDIUM;

    if (isTv) {
        // The 'generalized density' of a TV is based on its
        // vertical resolution
        bucket = (screenHeight <= 720) ? Density.TV : Density.XHIGH;
    } else {
        // A hand-held device.
        // Search for the density enum whose value is closest to the density of our device.
        double minDifference = Double.MAX_VALUE;
        for (Density d : Density.values()) {
            if (!d.isValidValueForDevice() || !isRecommended(d)) {
                continue;
            }
            double difference = Math.abs(d.getDpiValue() - dpi);
            if (difference < minDifference) {
                minDifference = difference;
                bucket = d;
            }
        }
    }
    return bucket;
}
 
Example #6
Source File: DensityQualifier.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
    if (compareTo == null) {
        return true;
    }

    DensityQualifier compareQ = (DensityQualifier)compareTo;
    DensityQualifier referenceQ = (DensityQualifier)reference;

    if (compareQ.mValue == referenceQ.mValue || compareQ.mValue == Density.ANYDPI) {
        // what we have is already the best possible match (exact match)
        return false;
    } else if (mValue == referenceQ.mValue || mValue == Density.ANYDPI) {
        // got new exact value, this is the best!
        return true;
    } else {
        // in all case we're going to prefer the higher dpi.
        // if reference is high, we want highest dpi.
        // if reference is medium, we'll prefer to scale down high dpi, than scale up low dpi
        // if reference if low, we'll prefer to scale down high than medium (2:1 over 4:3)
        return mValue.getDpiValue() > compareQ.mValue.getDpiValue();
    }
}
 
Example #7
Source File: CompatibleScreensManifest.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@TaskAction
public void generate() throws IOException {
    StringBuilder content = new StringBuilder(
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                    "    package=\"\">\n" + "\n" +
                    "    <compatible-screens>\n");

    String density = getScreenDensity();

    // convert unsupported values to numbers.
    density = convert(density, Density.XXHIGH, Density.XXXHIGH);

    for (String size : getScreenSizes()) {
        content.append(
                "        <screen android:screenSize=\"" + size +
                        "\" android:screenDensity=\"" + density +
                        "\" />\n");
    }

    content.append("    </compatible-screens>\n" +
            "</manifest>");

    Files.write(content.toString(), getManifestFile(), Charsets.UTF_8);
}
 
Example #8
Source File: IconDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds in the resConfig values specified by the given flavor container, assuming
 * it's in one of the relevant variantFlavors, into the given set
 */
private static void addResConfigsFromFlavor(@NonNull Set<String> relevantDensities,
                                            @Nullable List<String> variantFlavors,
                                            @NonNull ProductFlavorContainer container) {
    ProductFlavor flavor = container.getProductFlavor();
    if (variantFlavors == null || variantFlavors.contains(flavor.getName())) {
        if (!flavor.getResourceConfigurations().isEmpty()) {
            for (String densityName : flavor.getResourceConfigurations()) {
                Density density = Density.getEnum(densityName);
                if (density != null && density.isRecommended()
                        && density != Density.NODPI && density != Density.ANYDPI) {
                    relevantDensities.add(densityName);
                }
            }
        }
    }
}
 
Example #9
Source File: Project.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds in the resConfig values specified by the given flavor container, assuming
 * it's in one of the relevant variantFlavors, into the given set
 */
private static void addResConfigsFromFlavor(@NonNull Set<String> relevantDensities,
        @Nullable List<String> variantFlavors,
        @NonNull ProductFlavorContainer container) {
    ProductFlavor flavor = container.getProductFlavor();
    if (variantFlavors == null || variantFlavors.contains(flavor.getName())) {
        if (!flavor.getResourceConfigurations().isEmpty()) {
            for (String densityName : flavor.getResourceConfigurations()) {
                Density density = Density.getEnum(densityName);
                if (density != null && density.isRecommended()
                        && density != Density.NODPI && density != Density.ANYDPI) {
                    relevantDensities.add(densityName);
                }
            }
        }
    }
}
 
Example #10
Source File: MergeAwbResource.java    From atlas with Apache License 2.0 6 votes vote down vote up
@NonNull
private ResourcePreprocessor getPreprocessor() {
    // Only one pre-processor for now. The code will need slight changes when we add more.

    if (isDisableVectorDrawables()) {
        // If the user doesn't want any PNGs, leave the XML file alone as well.
        return NoOpResourcePreprocessor.INSTANCE;
    }

    Collection<Density> densities =
            getGeneratedDensities().stream().map(Density::getEnum).collect(Collectors.toList());

    return new MergeAwbResource.MergeResourcesVectorDrawableRenderer(
            getMinSdk(),
            getGeneratedPngsOutputDir(),
            densities,
            LoggerWrapper.supplierFor(MergeAwbResource.class));
}
 
Example #11
Source File: PreprocessingOptions.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public void setDensities(Set<String> densities) {
    EnumSet<Density> newValue = EnumSet.noneOf(Density.class);
    for (String density : densities) {
        Density typedValue = Density.getEnum(density);
        checkArgument(typedValue != null, "Unrecognized density %s", density);
        newValue.add(typedValue);
    }
    this.densities = newValue;
}
 
Example #12
Source File: CompatibleScreensManifest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static String convert(@NonNull String density, @NonNull Density... densitiesToConvert) {
    for (Density densityToConvert : densitiesToConvert) {
        if (densityToConvert.getResourceValue().equals(density)) {
            return Integer.toString(densityToConvert.getDpiValue());
        }

    }


    return density;
}
 
Example #13
Source File: VectorDrawableRenderer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Collection<File> getFilesToBeGenerated(File inputXmlFile) {
    FolderConfiguration originalConfiguration = getFolderConfiguration(inputXmlFile);

    // Create all the PNG files and duplicate the XML into folders with the version qualifier.
    Collection<File> filesToBeGenerated = Lists.newArrayList();
    for (Density density : mDensities) {
        FolderConfiguration newConfiguration = FolderConfiguration.copyOf(originalConfiguration);
        newConfiguration.setDensityQualifier(new DensityQualifier(density));

        File directory = new File(
                mOutputDir,
                newConfiguration.getFolderName(ResourceFolderType.DRAWABLE));
        File pngFile = new File(
                directory,
                inputXmlFile.getName().replace(".xml", ".png"));

        filesToBeGenerated.add(pngFile);

        newConfiguration.setVersionQualifier(new VersionQualifier(MIN_SDK_WITH_VECTOR_SUPPORT));
        File destination = new File(
                mOutputDir,
                newConfiguration.getFolderName(ResourceFolderType.DRAWABLE));
        File xmlCopy = new File(destination, inputXmlFile.getName());
        filesToBeGenerated.add(xmlCopy);
    }

    return filesToBeGenerated;
}
 
Example #14
Source File: DensitySplitOptions.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Set<String> getDefaultValues() {
    Density[] values = Density.values();
    Set<String> fullList = Sets.newHashSetWithExpectedSize(values.length - 2);
    for (Density value : values) {
        if (value != Density.NODPI && value != Density.ANYDPI && value.isRecommended()) {
            fullList.add(value.getResourceValue());
        }
    }

    return fullList;
}
 
Example #15
Source File: DensitySplitOptions.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ImmutableSet<String> getAllowedValues() {
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();

    for (Density value : Density.values()) {
        if (value != Density.NODPI && value != Density.ANYDPI) {
            builder.add(value.getResourceValue());
        }
    }

    return builder.build();
}
 
Example #16
Source File: ResourceItem.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private Density getFolderDensity() {
    String qualifiers = getQualifiers();
    if (!qualifiers.isEmpty() && qualifiers.contains("dpi")) {
        Iterable<String> segments = Splitter.on('-').split(qualifiers);
        FolderConfiguration config = FolderConfiguration.getConfigFromQualifiers(segments);
        if (config != null) {
            DensityQualifier densityQualifier = config.getDensityQualifier();
            if (densityQualifier != null) {
                return densityQualifier.getValue();
            }
        }
    }
    return null;
}
 
Example #17
Source File: DensityQualifier.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean checkAndSet(String value, FolderConfiguration config) {
    Density density = Density.getEnum(value);
    if (density == null) {

        // attempt to read a legacy value.
        Matcher m = sDensityLegacyPattern.matcher(value);
        if (m.matches()) {
            String v = m.group(1);

            try {
                density = Density.getEnum(Integer.parseInt(v));
            } catch (NumberFormatException e) {
                // looks like the string we extracted wasn't a valid number
                // which really shouldn't happen since the regexp would have failed.
            }
        }
    }

    if (density != null) {
        DensityQualifier qualifier = new DensityQualifier();
        qualifier.mValue = density;
        config.setDensityQualifier(qualifier);
        return true;
    }

    return false;
}
 
Example #18
Source File: PreprocessingOptions.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set of screen densities for which PNG files should be generated based on vector drawable
 * resource files.
 * <p>
 * <p>Default to {@code ["mdpi", "hdpi", "xhdpi", "xxhdpi"]}.
 */
public Set<String> getDensities() {
    Set<String> result = Sets.newHashSet();
    for (Density density : densities) {
        result.add(density.getResourceValue());
    }
    return result;
}
 
Example #19
Source File: PreprocessingOptions.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public PreprocessingOptions() {
    this.enabled = true;

    // TODO: What are the right default values?
    this.densities = EnumSet.of(
            Density.MEDIUM,
            Density.HIGH,
            Density.XHIGH,
            Density.XXHIGH);
}
 
Example #20
Source File: MergeAwbResource.java    From atlas with Apache License 2.0 5 votes vote down vote up
public MergeResourcesVectorDrawableRenderer(
        int minSdk,
        File outputDir,
        Collection<Density> densities,
        Supplier<ILogger> loggerSupplier) {
    super(minSdk, outputDir, densities, loggerSupplier);
}
 
Example #21
Source File: MergeResources.java    From javafxmobile-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MergeResourcesVectorDrawableRenderer(
        int minSdk,
        File outputDir,
        Collection<Density> densities,
        Supplier<ILogger> loggerSupplier) {
    super(minSdk, outputDir, densities, loggerSupplier);
}
 
Example #22
Source File: DensityQualifier.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkAndSet(String value, FolderConfiguration config) {
    Density density = Density.getEnum(value);
    if (density == null) {

        // attempt to read a legacy value.
        Matcher m = sDensityLegacyPattern.matcher(value);
        if (m.matches()) {
            String v = m.group(1);

            try {
                density = Density.getEnum(Integer.parseInt(v));
            } catch (NumberFormatException e) {
                // looks like the string we extracted wasn't a valid number
                // which really shouldn't happen since the regexp would have failed.
            }
        }
    }

    if (density != null) {
        DensityQualifier qualifier = new DensityQualifier();
        qualifier.mValue = density;
        config.setDensityQualifier(qualifier);
        return true;
    }

    return false;
}
 
Example #23
Source File: VectorDrawableRenderer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void generateFile(File toBeGenerated, File original) throws IOException {
        Files.createParentDirs(toBeGenerated);

        if (isXml(toBeGenerated)) {
            Files.copy(original, toBeGenerated);
        } else {
            mLogger.info(
                    "Generating PNG: [%s] from [%s]",
                    toBeGenerated.getAbsolutePath(),
                    original.getAbsolutePath());

            FolderConfiguration folderConfiguration = getFolderConfiguration(toBeGenerated);
            checkState(folderConfiguration.getDensityQualifier() != null);
            Density density = folderConfiguration.getDensityQualifier().getValue();

            String xmlContent = Files.toString(original, Charsets.UTF_8);
            float scaleFactor = density.getDpiValue() / (float) Density.MEDIUM.getDpiValue();
            if (scaleFactor <= 0) {
                scaleFactor = 1.0f;
            }

            final VdPreview.TargetSize imageSize = VdPreview.TargetSize.createSizeFromScale(scaleFactor);
//            BufferedImage image = VdPreview.getPreviewFromVectorXml(imageSize, xmlContent, null);
//            checkState(image != null, "Generating the image failed.");
//            ImageIO.write(image, "png", toBeGenerated);
        }
    }
 
Example #24
Source File: AaptPackageProcessBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static Collection<String> getDensityResConfigs(Collection<String> resourceConfigs) {
    return Collections2.filter(new ArrayList<String>(resourceConfigs),
            new Predicate<String>() {
                @Override
                public boolean apply(@Nullable String input) {
                    return Density.getEnum(input) != null;
                }
            });
}
 
Example #25
Source File: ResourceItem.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Nullable
private Density getFolderDensity() {
    String qualifiers = getQualifiers();
    if (!qualifiers.isEmpty() && qualifiers.contains("dpi")) {
        Iterable<String> segments = Splitter.on('-').split(qualifiers);
        FolderConfiguration config = FolderConfiguration.getConfigFromQualifiers(segments);
        if (config != null) {
            DensityQualifier densityQualifier = config.getDensityQualifier();
            if (densityQualifier != null) {
                return densityQualifier.getValue();
            }
        }
    }
    return null;
}
 
Example #26
Source File: DensityQualifier.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public DensityQualifier(Density value) {
    mValue = value;
}
 
Example #27
Source File: DensityQualifier.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public Density getValue() {
    return mValue;
}
 
Example #28
Source File: ConfigGenerator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
public ConfigGenerator setDensity(Density density) {
    mDensity = density;
    return this;
}
 
Example #29
Source File: VectorDrawableRenderer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public VectorDrawableRenderer(File outputDir, Collection<Density> densities, ILogger logger) {
    mOutputDir = outputDir;
    mDensities = densities;
    mLogger = logger;
}
 
Example #30
Source File: VectorDrawableRenderer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public Collection<File> createPngFiles(
        @NonNull File inputXmlFile,
        @NonNull File outputResDirectory,
        @NonNull Collection<Density> densities) throws IOException {
    throw new RuntimeException("Replaced by the new way to generate PNGs");
}