diff options
| author | Kenny Root <kroot@google.com> | 2010-10-29 14:20:04 -0700 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2010-10-29 14:20:04 -0700 |
| commit | 03034739eb90a41c930201253f5d709c232ce5a9 (patch) | |
| tree | 5e4de6e1968ce4f32b170aaf5108c6335f2f30a3 /core/java/android | |
| parent | 0aa211db16c1d1f86206c534619571cc36ab4a19 (diff) | |
| parent | 55fc850cf992cdcb0993cb109d2f716613c0dbdd (diff) | |
Merge "Add path to get different DPI drawables"
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/content/res/AssetManager.java | 9 | ||||
| -rwxr-xr-x | core/java/android/content/res/Resources.java | 69 |
2 files changed, 73 insertions, 5 deletions
diff --git a/core/java/android/content/res/AssetManager.java b/core/java/android/content/res/AssetManager.java index 73d9458fbec3..e279f64fb7bc 100644 --- a/core/java/android/content/res/AssetManager.java +++ b/core/java/android/content/res/AssetManager.java @@ -149,7 +149,7 @@ public final class AssetManager { /*package*/ final CharSequence getResourceText(int ident) { synchronized (this) { TypedValue tmpValue = mValue; - int block = loadResourceValue(ident, tmpValue, true); + int block = loadResourceValue(ident, (short) 0, tmpValue, true); if (block >= 0) { if (tmpValue.type == TypedValue.TYPE_STRING) { return mStringBlocks[block].get(tmpValue.data); @@ -190,10 +190,11 @@ public final class AssetManager { /*package*/ final boolean getResourceValue(int ident, + int density, TypedValue outValue, boolean resolveRefs) { - int block = loadResourceValue(ident, outValue, resolveRefs); + int block = loadResourceValue(ident, (short) density, outValue, resolveRefs); if (block >= 0) { if (outValue.type != TypedValue.TYPE_STRING) { return true; @@ -681,8 +682,8 @@ public final class AssetManager { /** Returns true if the resource was found, filling in mRetStringBlock and * mRetData. */ - private native final int loadResourceValue(int ident, TypedValue outValue, - boolean resolve); + private native final int loadResourceValue(int ident, short density, TypedValue outValue, + boolean resolve); /** Returns true if the resource was found, filling in mRetStringBlock and * mRetData. */ private native final int loadResourceBagValue(int ident, int bagEntryId, TypedValue outValue, diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index 9b23c1ea8965..e6fd039c839f 100755 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -628,6 +628,50 @@ public class Resources { } /** + * Return a drawable object associated with a particular resource ID for the + * given screen density in DPI. This will set the drawable's density to be + * the device's density multiplied by the ratio of actual drawable density + * to requested density. This allows the drawable to be scaled up to the + * correct size if needed. Various types of objects will be returned + * depending on the underlying resource -- for example, a solid color, PNG + * image, scalable image, etc. The Drawable API hides these implementation + * details. + * + * @param id The desired resource identifier, as generated by the aapt tool. + * This integer encodes the package, type, and resource entry. + * The value 0 is an invalid identifier. + * @param density the desired screen density indicated by the resource as + * found in {@link DisplayMetrics}. + * @throws NotFoundException Throws NotFoundException if the given ID does + * not exist. + * @return Drawable An object that can be used to draw this resource. + * @hide + */ + public Drawable getDrawableForDensity(int id, int density) throws NotFoundException { + synchronized (mTmpValue) { + TypedValue value = mTmpValue; + getValueForDensity(id, density, value, true); + + /* + * Pretend the requested density is actually the display density. If + * the drawable returned is not the requested density, then force it + * to be scaled later by dividing its density by the ratio of + * requested density to actual device density. Drawables that have + * undefined density or no density don't need to be handled here. + */ + if (value.density > 0 && value.density != TypedValue.DENSITY_NONE) { + if (value.density == density) { + value.density = DisplayMetrics.DENSITY_DEVICE; + } else { + value.density = (value.density * DisplayMetrics.DENSITY_DEVICE) / density; + } + } + + return loadDrawable(value, id); + } + } + + /** * Return a movie object associated with the particular resource ID. * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource @@ -930,7 +974,7 @@ public class Resources { */ public void getValue(int id, TypedValue outValue, boolean resolveRefs) throws NotFoundException { - boolean found = mAssets.getResourceValue(id, outValue, resolveRefs); + boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs); if (found) { return; } @@ -939,6 +983,29 @@ public class Resources { } /** + * Get the raw value associated with a resource with associated density. + * + * @param id resource identifier + * @param density density in DPI + * @param resolveRefs If true, a resource that is a reference to another + * resource will be followed so that you receive the actual final + * resource data. If false, the TypedValue will be filled in with + * the reference itself. + * @throws NotFoundException Throws NotFoundException if the given ID does + * not exist. + * @see #getValue(String, TypedValue, boolean) + * @hide + */ + public void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs) + throws NotFoundException { + boolean found = mAssets.getResourceValue(id, density, outValue, resolveRefs); + if (found) { + return; + } + throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id)); + } + + /** * Return the raw data associated with a particular resource ID. * See getIdentifier() for information on how names are mapped to resource * IDs, and getString(int) for information on how string resources are |
