Create Density based resource values when needed.

This will let the layoutlib know when to scale bitmaps
that are not in the native density of the rendering.

Change-Id: I08c99666460b5b5a3ed8d0aac7fa1b7c0136fd6b
This commit is contained in:
Xavier Ducrohet
2009-09-22 18:32:17 -07:00
parent 038300d95f
commit 350187ab93
4 changed files with 89 additions and 33 deletions

View File

@@ -17,6 +17,7 @@
package com.android.ide.eclipse.adt.internal.resources.configurations;
import com.android.ide.eclipse.adt.internal.editors.IconFactory;
import com.android.layoutlib.api.IDensityBasedResourceValue;
import com.android.sdklib.AndroidVersion;
import com.android.sdklib.IAndroidTarget;
@@ -39,19 +40,22 @@ public final class PixelDensityQualifier extends ResourceQualifier {
* Screen Orientation enum.
*/
public static enum Density {
HIGH("hdpi", 240, "High Density"), //$NON-NLS-1$
MEDIUM("mdpi", 160, "Medium Density"), //$NON-NLS-1$
LOW("ldpi", 120, "Low Density"), //$NON-NLS-1$
NODPI("nodpi", -1, "No Density"); //$NON-NLS-1$
HIGH("hdpi", 240, "High Density", IDensityBasedResourceValue.Density.HIGH), //$NON-NLS-1$
MEDIUM("mdpi", 160, "Medium Density", IDensityBasedResourceValue.Density.MEDIUM), //$NON-NLS-1$
LOW("ldpi", 120, "Low Density", IDensityBasedResourceValue.Density.LOW), //$NON-NLS-1$
NODPI("nodpi", -1, "No Density", IDensityBasedResourceValue.Density.NODPI); //$NON-NLS-1$
private final String mValue;
private final String mDisplayValue;
private final int mDpiValue;
private final IDensityBasedResourceValue.Density mDensity;
private Density(String value, int dpiValue, String displayValue) {
private Density(String value, int dpiValue, String displayValue,
IDensityBasedResourceValue.Density density) {
mValue = value;
mDpiValue = dpiValue;
mDisplayValue = displayValue;
mDensity = density;
}
/**
@@ -109,6 +113,14 @@ public final class PixelDensityQualifier extends ResourceQualifier {
return mDisplayValue;
}
/**
* Returns the {@link com.android.layoutlib.api.IDensityBasedResourceValue.Density} value
* associated to this {@link Density}.
*/
public IDensityBasedResourceValue.Density getDensity() {
return mDensity;
}
public static int getIndex(Density value) {
int i = 0;
for (Density input : values()) {

View File

@@ -45,7 +45,7 @@ import javax.xml.parsers.SAXParserFactory;
public final class MultiResourceFile extends ResourceFile implements IValueResourceRepository {
private final static SAXParserFactory sParserFactory = SAXParserFactory.newInstance();
private final HashMap<ResourceType, HashMap<String, ResourceValue>> mResourceItems =
new HashMap<ResourceType, HashMap<String, ResourceValue>>();
@@ -58,7 +58,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
update();
Set<ResourceType> keys = mResourceItems.keySet();
return keys.toArray(new ResourceType[keys.size()]);
}
@@ -69,21 +69,21 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
HashMap<String, ResourceValue> list = mResourceItems.get(type);
return (list != null && list.size() > 0);
}
@Override
public Collection<ProjectResourceItem> getResources(ResourceType type,
ProjectResources projectResources) {
update();
HashMap<String, ResourceValue> list = mResourceItems.get(type);
ArrayList<ProjectResourceItem> items = new ArrayList<ProjectResourceItem>();
if (list != null) {
Collection<ResourceValue> values = list.values();
for (ResourceValue res : values) {
ProjectResourceItem item = projectResources.findResourceItem(type, res.getName());
if (item == null) {
if (type == ResourceType.ID) {
item = new IdResourceItem(res.getName(), false /* isDeclaredInline */);
@@ -99,7 +99,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
return items;
}
/**
* Updates the Resource items if necessary.
*/
@@ -110,7 +110,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
// need to parse the file and find the content.
parseFile();
resetTouch();
}
}
@@ -128,7 +128,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
} catch (CoreException e) {
}
}
/**
* Adds a resource item to the list
* @param resType The type of the resource
@@ -138,7 +138,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
ResourceType type = ResourceType.getEnum(resType);
if (type != null) {
HashMap<String, ResourceValue> list = mResourceItems.get(type);
// if the list does not exist, create it.
if (list == null) {
list = new HashMap<String, ResourceValue>();
@@ -146,13 +146,13 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
} else {
// look for a possible value already existing.
ResourceValue oldValue = list.get(value.getName());
if (oldValue != null) {
oldValue.replaceWith(value);
return;
}
}
// empty list or no match found? add the given resource
list.put(value.getName(), value);
}
@@ -168,7 +168,7 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou
if (list != null) {
return list.get(name);
}
return null;
}
}

View File

@@ -17,8 +17,10 @@
package com.android.ide.eclipse.adt.internal.resources.manager;
import com.android.ide.eclipse.adt.internal.resources.ResourceType;
import com.android.ide.eclipse.adt.internal.resources.configurations.PixelDensityQualifier;
import com.android.ide.eclipse.adt.internal.resources.manager.files.IAbstractFile;
import com.android.layoutlib.api.IResourceValue;
import com.android.layoutlib.utils.DensityBasedResourceValue;
import com.android.layoutlib.utils.ResourceValue;
import java.util.ArrayList;
@@ -40,24 +42,24 @@ public class SingleResourceFile extends ResourceFile {
static {
sParserFactory.setNamespaceAware(true);
}
private final static Pattern sXmlPattern = Pattern.compile("^(.+)\\.xml", //$NON-NLS-1$
Pattern.CASE_INSENSITIVE);
private final static Pattern[] sDrawablePattern = new Pattern[] {
Pattern.compile("^(.+)\\.9\\.png", Pattern.CASE_INSENSITIVE), //$NON-NLS-1$
Pattern.compile("^(.+)\\.png", Pattern.CASE_INSENSITIVE), //$NON-NLS-1$
Pattern.compile("^(.+)\\.jpg", Pattern.CASE_INSENSITIVE), //$NON-NLS-1$
Pattern.compile("^(.+)\\.gif", Pattern.CASE_INSENSITIVE), //$NON-NLS-1$
};
private String mResourceName;
private ResourceType mType;
private IResourceValue mValue;
public SingleResourceFile(IAbstractFile file, ResourceFolder folder) {
super(file, folder);
// we need to infer the type of the resource from the folder type.
// This is easy since this is a single Resource file.
ResourceType[] types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType());
@@ -65,9 +67,17 @@ public class SingleResourceFile extends ResourceFile {
// compute the resource name
mResourceName = getResourceName(mType);
mValue = new ResourceValue(mType.getName(), getResourceName(mType), file.getOsLocation(),
isFramework());
// test if there's a density qualifier associated with the resource
PixelDensityQualifier qualifier = folder.getConfiguration().getPixelDensityQualifier();
if (qualifier == null) {
mValue = new ResourceValue(mType.getName(), getResourceName(mType),
file.getOsLocation(), isFramework());
} else {
mValue = new DensityBasedResourceValue(mType.getName(), getResourceName(mType),
file.getOsLocation(), qualifier.getValue().getDensity(), isFramework());
}
}
@Override
@@ -83,27 +93,27 @@ public class SingleResourceFile extends ResourceFile {
@Override
public Collection<ProjectResourceItem> getResources(ResourceType type,
ProjectResources projectResources) {
// looking for an existing ResourceItem with this name and type
ProjectResourceItem item = projectResources.findResourceItem(type, mResourceName);
ArrayList<ProjectResourceItem> items = new ArrayList<ProjectResourceItem>();
if (item == null) {
item = new ConfigurableResourceItem(mResourceName);
items.add(item);
}
// add this ResourceFile to the ResourceItem
item.add(this);
return items;
}
/*
* (non-Javadoc)
* @see com.android.ide.eclipse.editors.resources.manager.ResourceFile#getValue(com.android.ide.eclipse.common.resources.ResourceType, java.lang.String)
*
*
* This particular implementation does not care about the type or name since a
* SingleResourceFile represents a file generating only one resource.
* The value returned is the full absolute path of the file in OS form.
@@ -112,14 +122,14 @@ public class SingleResourceFile extends ResourceFile {
public IResourceValue getValue(ResourceType type, String name) {
return mValue;
}
/**
* Returns the name of the resources.
*/
private String getResourceName(ResourceType type) {
// get the name from the filename.
String name = getFile().getName();
if (type == ResourceType.ANIM || type == ResourceType.LAYOUT || type == ResourceType.MENU ||
type == ResourceType.COLOR || type == ResourceType.XML) {
Matcher m = sXmlPattern.matcher(name);
@@ -133,7 +143,7 @@ public class SingleResourceFile extends ResourceFile {
return m.group(1);
}
}
// also try the Xml pattern for selector/shape based drawable.
Matcher m = sXmlPattern.matcher(name);
if (m.matches()) {