diff options
author | Kenny Root <kroot@google.com> | 2010-10-28 14:47:01 -0700 |
---|---|---|
committer | Kenny Root <kroot@google.com> | 2010-10-29 12:47:07 -0700 |
commit | 55fc850cf992cdcb0993cb109d2f716613c0dbdd (patch) | |
tree | 2277f806e14fb8a6b422dde687ac9779176ae83b /core | |
parent | 490d7c5deda8d602f916942d7002757082274b9b (diff) | |
download | frameworks_base-55fc850cf992cdcb0993cb109d2f716613c0dbdd.zip frameworks_base-55fc850cf992cdcb0993cb109d2f716613c0dbdd.tar.gz frameworks_base-55fc850cf992cdcb0993cb109d2f716613c0dbdd.tar.bz2 |
Add path to get different DPI drawables
Allow a caller to request a different density than their current display
allows. This can mean a device displaying mdpi can get a resource that's
in hdpi and have it pretend to be in mdpi resolution. If a drawable
that's returned is not in the requested density, it will set it at the
appropriate density to be scaled up later on.
The API for this is hidden currently.
Bug: 3134688
Change-Id: I6c3908cbdef4907b8d3f1576df9e3b0e7af1755a
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/content/res/AssetManager.java | 9 | ||||
-rwxr-xr-x | core/java/android/content/res/Resources.java | 69 | ||||
-rw-r--r-- | core/jni/android_util_AssetManager.cpp | 5 |
3 files changed, 76 insertions, 7 deletions
diff --git a/core/java/android/content/res/AssetManager.java b/core/java/android/content/res/AssetManager.java index 73d9458..e279f64 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 9b23c1e..e6fd039 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 diff --git a/core/jni/android_util_AssetManager.cpp b/core/jni/android_util_AssetManager.cpp index f3b9357..c4056a4 100644 --- a/core/jni/android_util_AssetManager.cpp +++ b/core/jni/android_util_AssetManager.cpp @@ -701,6 +701,7 @@ static jstring android_content_AssetManager_getResourceEntryName(JNIEnv* env, jo static jint android_content_AssetManager_loadResourceValue(JNIEnv* env, jobject clazz, jint ident, + jshort density, jobject outValue, jboolean resolve) { @@ -713,7 +714,7 @@ static jint android_content_AssetManager_loadResourceValue(JNIEnv* env, jobject Res_value value; ResTable_config config; uint32_t typeSpecFlags; - ssize_t block = res.getResource(ident, &value, false, &typeSpecFlags, &config); + ssize_t block = res.getResource(ident, &value, false, density, &typeSpecFlags, &config); #if THROW_ON_BAD_ID if (block == BAD_INDEX) { jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); @@ -1703,7 +1704,7 @@ static JNINativeMethod gAssetManagerMethods[] = { (void*) android_content_AssetManager_getResourceTypeName }, { "getResourceEntryName","(I)Ljava/lang/String;", (void*) android_content_AssetManager_getResourceEntryName }, - { "loadResourceValue","(ILandroid/util/TypedValue;Z)I", + { "loadResourceValue","(ISLandroid/util/TypedValue;Z)I", (void*) android_content_AssetManager_loadResourceValue }, { "loadResourceBagValue","(IILandroid/util/TypedValue;Z)I", (void*) android_content_AssetManager_loadResourceBagValue }, |