Merge "Make optical insets actually work"

This commit is contained in:
Alan Viverette
2014-07-04 01:58:48 +00:00
committed by Android (Google) Code Review
5 changed files with 34 additions and 26 deletions

View File

@@ -598,7 +598,7 @@ int register_android_graphics_BitmapFactory(JNIEnv* env) {
jclass bitmap_class = env->FindClass("android/graphics/Bitmap");
SkASSERT(bitmap_class);
gBitmap_nativeBitmapFieldID = getFieldIDCheck(env, bitmap_class, "mNativeBitmap", "J");
gBitmap_layoutBoundsFieldID = getFieldIDCheck(env, bitmap_class, "mLayoutBounds", "[I");
gBitmap_layoutBoundsFieldID = getFieldIDCheck(env, bitmap_class, "mOpticalInsets", "[I");
int ret = AndroidRuntime::registerNativeMethods(env,
"android/graphics/BitmapFactory$Options",
gOptionsMethods,

View File

@@ -16,6 +16,7 @@
package android.graphics;
import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.DisplayMetrics;
@@ -73,7 +74,7 @@ public final class Bitmap implements Parcelable {
private boolean mRequestPremultiplied;
private byte[] mNinePatchChunk; // may be null
private int[] mLayoutBounds; // may be null
private int[] mOpticalInsets; // may be null
private int mWidth;
private int mHeight;
private boolean mRecycled;
@@ -110,7 +111,7 @@ public final class Bitmap implements Parcelable {
@SuppressWarnings({"UnusedDeclaration"}) // called from JNI
Bitmap(long nativeBitmap, byte[] buffer, int width, int height, int density,
boolean isMutable, boolean requestPremultiplied,
byte[] ninePatchChunk, int[] layoutBounds) {
byte[] ninePatchChunk, int[] opticalInsets) {
if (nativeBitmap == 0) {
throw new RuntimeException("internal error: native bitmap is 0");
}
@@ -125,7 +126,7 @@ public final class Bitmap implements Parcelable {
mFinalizer = new BitmapFinalizer(nativeBitmap);
mNinePatchChunk = ninePatchChunk;
mLayoutBounds = layoutBounds;
mOpticalInsets = opticalInsets;
if (density >= 0) {
mDensity = density;
}
@@ -291,16 +292,6 @@ public final class Bitmap implements Parcelable {
mNinePatchChunk = chunk;
}
/**
* Sets the layout bounds as an array of left, top, right, bottom integers
* @param bounds the array containing the padding values
*
* @hide
*/
public void setLayoutBounds(int[] bounds) {
mLayoutBounds = bounds;
}
/**
* Free the native object associated with this bitmap, and clear the
* reference to the pixel data. This will not free the pixel data synchronously;
@@ -949,11 +940,20 @@ public final class Bitmap implements Parcelable {
}
/**
* Populates a rectangle with the bitmap's optical insets.
*
* @param outInsets Rect to populate with optical insets
* @hide
* @return the layout padding [left, right, top, bottom]
*/
public int[] getLayoutBounds() {
return mLayoutBounds;
public void getOpticalInsets(@NonNull Rect outInsets) {
if (mOpticalInsets == null) {
outInsets.setEmpty();
} else {
outInsets.left = mOpticalInsets[0];
outInsets.top = mOpticalInsets[1];
outInsets.right = mOpticalInsets[2];
outInsets.bottom = mOpticalInsets[3];
}
}
/**

View File

@@ -948,13 +948,10 @@ public abstract class Drawable {
np = null;
pad = null;
}
int[] layoutBounds = bm.getLayoutBounds();
Rect layoutBoundsRect = null;
if (layoutBounds != null) {
layoutBoundsRect = new Rect(layoutBounds[0], layoutBounds[1],
layoutBounds[2], layoutBounds[3]);
}
return drawableFromBitmap(res, bm, np, pad, layoutBoundsRect, srcName);
final Rect opticalInsets = new Rect();
bm.getOpticalInsets(opticalInsets);
return drawableFromBitmap(res, bm, np, pad, opticalInsets, srcName);
}
return null;
}

View File

@@ -188,6 +188,16 @@ public class InsetDrawable extends Drawable implements Drawable.Callback {
}
}
/** @hide */
@Override
public Insets getOpticalInsets() {
final Insets contentInsets = super.getOpticalInsets();
return Insets.of(contentInsets.left + mInsetState.mInsetLeft,
contentInsets.top + mInsetState.mInsetTop,
contentInsets.right + mInsetState.mInsetRight,
contentInsets.bottom + mInsetState.mInsetBottom);
}
@Override
public void setHotspot(float x, float y) {
mInsetState.mDrawable.setHotspot(x, y);

View File

@@ -23,12 +23,10 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Insets;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
@@ -419,6 +417,9 @@ public class NinePatchDrawable extends Drawable {
": <nine-patch> requires a valid 9-patch source image");
}
// Hey, now might be a good time to actually load optical bounds!
bitmap.getOpticalInsets(opticalInsets);
state.mNinePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk());
state.mPadding = padding;
state.mOpticalInsets = Insets.of(opticalInsets);