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

View File

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

View File

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

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.layoutlib.utils;
import com.android.layoutlib.api.IDensityBasedResourceValue;
public class DensityBasedResourceValue extends ResourceValue implements IDensityBasedResourceValue {
private Density mDensity;
public DensityBasedResourceValue(String type, String name, String value, Density density,
boolean isFramework) {
super(type, name, value, isFramework);
mDensity = density;
}
public Density getDensity() {
return mDensity;
}
}