am 3b805d57: Merge change 26616 into eclair

Merge commit '3b805d570716749966b909be16513f882522c58e' into eclair-plus-aosp

* commit '3b805d570716749966b909be16513f882522c58e':
  Add density support to layoutlib so that bitmap are scaled if needed.
This commit is contained in:
Xavier Ducrohet
2009-09-23 19:24:00 -07:00
committed by Android Git Automerger
4 changed files with 78 additions and 47 deletions

View File

@@ -359,6 +359,7 @@ public final class Bridge implements ILayoutBridge {
try { try {
// setup the display Metrics. // setup the display Metrics.
DisplayMetrics metrics = new DisplayMetrics(); DisplayMetrics metrics = new DisplayMetrics();
metrics.densityDpi = density;
metrics.density = density / (float) DisplayMetrics.DENSITY_DEFAULT; metrics.density = density / (float) DisplayMetrics.DENSITY_DEFAULT;
metrics.scaledDensity = metrics.density; metrics.scaledDensity = metrics.density;
metrics.widthPixels = screenWidth; metrics.widthPixels = screenWidth;
@@ -406,7 +407,7 @@ public final class Bridge implements ILayoutBridge {
// get the background drawable // get the background drawable
if (windowBackground != null) { if (windowBackground != null) {
Drawable d = ResourceHelper.getDrawable(windowBackground.getValue(), Drawable d = ResourceHelper.getDrawable(windowBackground,
context, true /* isFramework */); context, true /* isFramework */);
root.setBackgroundDrawable(d); root.setBackgroundDrawable(d);
} }

View File

@@ -124,7 +124,7 @@ public final class BridgeResources extends Resources {
IResourceValue value = getResourceValue(id, mPlatformResourceFlag); IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
if (value != null) { if (value != null) {
return ResourceHelper.getDrawable(value.getValue(), mContext, value.isFramework()); return ResourceHelper.getDrawable(value, mContext, value.isFramework());
} }
// id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException.

View File

@@ -659,8 +659,9 @@ public final class BridgeTypedArray extends TypedArray {
return null; return null;
} }
String value = mData[index].getValue(); IResourceValue value = mData[index];
if (value == null || BridgeConstants.REFERENCE_NULL.equals(value)) { String stringValue = value.getValue();
if (stringValue == null || BridgeConstants.REFERENCE_NULL.equals(stringValue)) {
return null; return null;
} }

View File

@@ -16,6 +16,9 @@
package com.android.layoutlib.bridge; package com.android.layoutlib.bridge;
import com.android.layoutlib.api.IDensityBasedResourceValue;
import com.android.layoutlib.api.IResourceValue;
import com.android.layoutlib.api.IDensityBasedResourceValue.Density;
import com.android.ninepatch.NinePatch; import com.android.ninepatch.NinePatch;
import org.kxml2.io.KXmlParser; import org.kxml2.io.KXmlParser;
@@ -96,28 +99,30 @@ public final class ResourceHelper {
/** /**
* Returns a drawable from the given value. * Returns a drawable from the given value.
* @param value The value. A path to a 9 patch, a bitmap or a xml based drawable, * @param value The value that contains a path to a 9 patch, a bitmap or a xml based drawable,
* or an hexadecimal color * or an hexadecimal color
* @param context * @param context
* @param isFramework indicates whether the resource is a framework resources. * @param isFramework indicates whether the resource is a framework resources.
* Framework resources are cached, and loaded only once. * Framework resources are cached, and loaded only once.
*/ */
public static Drawable getDrawable(String value, BridgeContext context, boolean isFramework) { public static Drawable getDrawable(IResourceValue value, BridgeContext context, boolean isFramework) {
Drawable d = null; Drawable d = null;
String lowerCaseValue = value.toLowerCase(); String stringValue = value.getValue();
String lowerCaseValue = stringValue.toLowerCase();
if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) { if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) {
File f = new File(value); File file = new File(stringValue);
if (f.isFile()) { if (file.isFile()) {
NinePatch ninePatch = Bridge.getCached9Patch(value, NinePatch ninePatch = Bridge.getCached9Patch(stringValue,
isFramework ? null : context.getProjectKey()); isFramework ? null : context.getProjectKey());
if (ninePatch == null) { if (ninePatch == null) {
try { try {
ninePatch = NinePatch.load(new File(value).toURL(), false /* convert */); ninePatch = NinePatch.load(file.toURL(), false /* convert */);
Bridge.setCached9Patch(value, ninePatch, Bridge.setCached9Patch(stringValue, ninePatch,
isFramework ? null : context.getProjectKey()); isFramework ? null : context.getProjectKey());
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
// URL is wrong, we'll return null below // URL is wrong, we'll return null below
@@ -134,7 +139,7 @@ public final class ResourceHelper {
return null; return null;
} else if (lowerCaseValue.endsWith(".xml")) { } else if (lowerCaseValue.endsWith(".xml")) {
// create a blockparser for the file // create a blockparser for the file
File f = new File(value); File f = new File(stringValue);
if (f.isFile()) { if (f.isFile()) {
try { try {
// let the framework inflate the Drawable from the XML file. // let the framework inflate the Drawable from the XML file.
@@ -157,19 +162,43 @@ public final class ResourceHelper {
return null; return null;
} else { } else {
File bmpFile = new File(value); File bmpFile = new File(stringValue);
if (bmpFile.isFile()) { if (bmpFile.isFile()) {
try { try {
Bitmap bitmap = Bridge.getCachedBitmap(value, Bitmap bitmap = Bridge.getCachedBitmap(stringValue,
isFramework ? null : context.getProjectKey()); isFramework ? null : context.getProjectKey());
if (bitmap == null) { if (bitmap == null) {
bitmap = new Bitmap(bmpFile); bitmap = new Bitmap(bmpFile);
Bridge.setCachedBitmap(value, bitmap, try {
bitmap.setDensity(Density.MEDIUM.getValue());
} catch (NoClassDefFoundError error) {
// look like we're running in an older version of ADT that doesn't
// include the new layoutlib_api. Let's just ignore this, the drawing
// will just be wrong.
}
Bridge.setCachedBitmap(stringValue, bitmap,
isFramework ? null : context.getProjectKey()); isFramework ? null : context.getProjectKey());
} }
return new BitmapDrawable(bitmap); try {
if (value instanceof IDensityBasedResourceValue) {
Density density = ((IDensityBasedResourceValue)value).getDensity();
if (density != Density.MEDIUM) {
// create a copy of the bitmap
bitmap = Bitmap.createBitmap(bitmap);
// apply the density
bitmap.setDensity(density.getValue());
}
}
} catch (NoClassDefFoundError error) {
// look like we're running in an older version of ADT that doesn't include
// the new layoutlib_api. Let's just ignore this, the drawing will just be
// wrong.
}
return new BitmapDrawable(context.getResources(), bitmap);
} catch (IOException e) { } catch (IOException e) {
// we'll return null below // we'll return null below
// TODO: log the error. // TODO: log the error.
@@ -177,7 +206,7 @@ public final class ResourceHelper {
} else { } else {
// attempt to get a color from the value // attempt to get a color from the value
try { try {
int color = getColor(value); int color = getColor(stringValue);
return new ColorDrawable(color); return new ColorDrawable(color);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// we'll return null below. // we'll return null below.