Methods Summary |
---|
private void | checkPixelAccess(int x, int y)Shared code to check for illegal arguments passed to getPixel()
or setPixel()
checkXYSign(x, y);
if (x >= getWidth()) {
throw new IllegalArgumentException("x must be < bitmap.width()");
}
if (y >= getHeight()) {
throw new IllegalArgumentException("y must be < bitmap.height()");
}
|
private void | checkPixelsAccess(int x, int y, int width, int height, int offset, int stride, int[] pixels)Shared code to check for illegal arguments passed to getPixels()
or setPixels()
checkXYSign(x, y);
if (width < 0) {
throw new IllegalArgumentException("width must be >= 0");
}
if (height < 0) {
throw new IllegalArgumentException("height must be >= 0");
}
if (x + width > getWidth()) {
throw new IllegalArgumentException(
"x + width must be <= bitmap.width()");
}
if (y + height > getHeight()) {
throw new IllegalArgumentException(
"y + height must be <= bitmap.height()");
}
if (Math.abs(stride) < width) {
throw new IllegalArgumentException("abs(stride) must be >= width");
}
int lastScanline = offset + (height - 1) * stride;
int length = pixels.length;
if (offset < 0 || (offset + width > length)
|| lastScanline < 0
|| (lastScanline + width > length)) {
throw new ArrayIndexOutOfBoundsException();
}
|
private void | checkRecycled(java.lang.String errorMessage)This is called by methods that want to throw an exception if the bitmap
has already been recycled.
if (mRecycled) {
throw new IllegalStateException(errorMessage);
}
|
private static void | checkWidthHeight(int width, int height)Common code for checking that width and height are > 0
if (width <= 0) {
throw new IllegalArgumentException("width must be > 0");
}
if (height <= 0) {
throw new IllegalArgumentException("height must be > 0");
}
|
private static void | checkXYSign(int x, int y)Common code for checking that x and y are >= 0
if (x < 0) {
throw new IllegalArgumentException("x must be >= 0");
}
if (y < 0) {
throw new IllegalArgumentException("y must be >= 0");
}
|
public boolean | compress(android.graphics.Bitmap$CompressFormat format, int quality, java.io.OutputStream stream)Write a compressed version of the bitmap to the specified outputstream.
If this returns true, the bitmap can be reconstructed by passing a
corresponding inputstream to BitmapFactory.decodeStream(). Note: not
all Formats support all bitmap configs directly, so it is possible that
the returned bitmap from BitmapFactory could be in a different bitdepth,
and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
pixels).
checkRecycled("Can't compress a recycled bitmap");
// do explicit check before calling the native method
if (stream == null) {
throw new NullPointerException();
}
if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("quality must be 0..100");
}
return nativeCompress(mNativeBitmap, format.nativeInt, quality,
stream, new byte[WORKING_COMPRESS_STORAGE]);
|
public android.graphics.Bitmap | copy(android.graphics.Bitmap$Config config, boolean isMutable)Tries to make a new bitmap based on the dimensions of this bitmap,
setting the new bitmap's config to the one specified, and then copying
this bitmap's pixels into the new bitmap. If the conversion is not
supported, or the allocator fails, then this returns NULL.
checkRecycled("Can't copy a recycled bitmap");
return nativeCopy(mNativeBitmap, config.nativeInt, isMutable);
|
public void | copyPixelsFromBuffer(java.nio.Buffer src)Copy the pixels from the buffer, beginning at the current position,
overwriting the bitmap's pixels. The data in the buffer is not changed
in any way (unlike setPixels(), which converts from unpremultipled 32bit
to whatever the bitmap's native format is.
checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
int elements = src.remaining();
int shift;
if (src instanceof ByteBuffer) {
shift = 0;
} else if (src instanceof ShortBuffer) {
shift = 1;
} else if (src instanceof IntBuffer) {
shift = 2;
} else {
throw new RuntimeException("unsupported Buffer subclass");
}
long bufferBytes = (long)elements << shift;
long bitmapBytes = (long)getRowBytes() * getHeight();
if (bufferBytes < bitmapBytes) {
throw new RuntimeException("Buffer not large enough for pixels");
}
nativeCopyPixelsFromBuffer(mNativeBitmap, src);
|
public void | copyPixelsToBuffer(java.nio.Buffer dst)Copy the bitmap's pixels into the specified buffer (allocated by the
caller). An exception is thrown if the buffer is not large enough to
hold all of the pixels (taking into account the number of bytes per
pixel) or if the Buffer subclass is not one of the support types
(ByteBuffer, ShortBuffer, IntBuffer).
int elements = dst.remaining();
int shift;
if (dst instanceof ByteBuffer) {
shift = 0;
} else if (dst instanceof ShortBuffer) {
shift = 1;
} else if (dst instanceof IntBuffer) {
shift = 2;
} else {
throw new RuntimeException("unsupported Buffer subclass");
}
long bufferSize = (long)elements << shift;
long pixelSize = (long)getRowBytes() * getHeight();
if (bufferSize < pixelSize) {
throw new RuntimeException("Buffer not large enough for pixels");
}
nativeCopyPixelsToBuffer(mNativeBitmap, dst);
// now update the buffer's position
int position = dst.position();
position += pixelSize >> shift;
dst.position(position);
|
public static android.graphics.Bitmap | createBitmap(android.graphics.Bitmap src)Returns an immutable bitmap from the source bitmap. The new bitmap may
be the same object as source, or a copy may have been made.
return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
|
public static android.graphics.Bitmap | createBitmap(android.graphics.Bitmap source, int x, int y, int width, int height)Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made.
return createBitmap(source, x, y, width, height, null, false);
|
public static android.graphics.Bitmap | createBitmap(android.graphics.Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)Returns an immutable bitmap from subset of the source bitmap,
transformed by the optional matrix.
checkXYSign(x, y);
checkWidthHeight(width, height);
if (x + width > source.getWidth()) {
throw new IllegalArgumentException("x + width must be <= bitmap.width()");
}
if (y + height > source.getHeight()) {
throw new IllegalArgumentException("y + height must be <= bitmap.height()");
}
// check if we can just return our argument unchanged
if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
height == source.getHeight() && (m == null || m.isIdentity())) {
return source;
}
int neww = width;
int newh = height;
Canvas canvas = new Canvas();
Bitmap bitmap;
Paint paint;
Rect srcR = new Rect(x, y, x + width, y + height);
RectF dstR = new RectF(0, 0, width, height);
if (m == null || m.isIdentity()) {
bitmap = createBitmap(neww, newh,
source.hasAlpha() ? Config.ARGB_8888 : Config.RGB_565);
paint = null; // not needed
} else {
/* the dst should have alpha if the src does, or if our matrix
doesn't preserve rectness
*/
boolean hasAlpha = source.hasAlpha() || !m.rectStaysRect();
RectF deviceR = new RectF();
m.mapRect(deviceR, dstR);
neww = Math.round(deviceR.width());
newh = Math.round(deviceR.height());
bitmap = createBitmap(neww, newh, hasAlpha ? Config.ARGB_8888 : Config.RGB_565);
if (hasAlpha) {
bitmap.eraseColor(0);
}
canvas.translate(-deviceR.left, -deviceR.top);
canvas.concat(m);
paint = new Paint();
paint.setFilterBitmap(filter);
if (!m.rectStaysRect()) {
paint.setAntiAlias(true);
}
}
canvas.setBitmap(bitmap);
canvas.drawBitmap(source, srcR, dstR, paint);
// The new bitmap was created from a known bitmap source so assume that
// they use the same density scale
bitmap.setDensityScale(source.getDensityScale());
bitmap.setAutoScalingEnabled(source.isAutoScalingEnabled());
return bitmap;
|
public static android.graphics.Bitmap | createBitmap(int width, int height, android.graphics.Bitmap$Config config)Returns a mutable bitmap with the specified width and height.
Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
bm.eraseColor(0); // start with black/transparent pixels
return bm;
|
public static android.graphics.Bitmap | createBitmap(int[] colors, int offset, int stride, int width, int height, android.graphics.Bitmap$Config config)Returns a immutable bitmap with the specified width and height, with each
pixel value set to the corresponding value in the colors array.
checkWidthHeight(width, height);
if (Math.abs(stride) < width) {
throw new IllegalArgumentException("abs(stride) must be >= width");
}
int lastScanline = offset + (height - 1) * stride;
int length = colors.length;
if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
(lastScanline + width > length)) {
throw new ArrayIndexOutOfBoundsException();
}
return nativeCreate(colors, offset, stride, width, height,
config.nativeInt, false);
|
public static android.graphics.Bitmap | createBitmap(int[] colors, int width, int height, android.graphics.Bitmap$Config config)Returns a immutable bitmap with the specified width and height, with each
pixel value set to the corresponding value in the colors array.
return createBitmap(colors, 0, width, width, height, config);
|
public static android.graphics.Bitmap | createScaledBitmap(android.graphics.Bitmap src, int dstWidth, int dstHeight, boolean filter)
Matrix m;
synchronized (Bitmap.class) {
// small pool of just 1 matrix
m = sScaleMatrix;
sScaleMatrix = null;
}
if (m == null) {
m = new Matrix();
}
final int width = src.getWidth();
final int height = src.getHeight();
final float sx = dstWidth / (float)width;
final float sy = dstHeight / (float)height;
m.setScale(sx, sy);
Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
synchronized (Bitmap.class) {
// do we need to check for null? why not just assign everytime?
if (sScaleMatrix == null) {
sScaleMatrix = m;
}
}
return b;
|
public int | describeContents()No special parcel contents.
return 0;
|
public void | eraseColor(int c)Fills the bitmap's pixels with the specified {@link Color}.
checkRecycled("Can't erase a recycled bitmap");
if (!isMutable()) {
throw new IllegalStateException("cannot erase immutable bitmaps");
}
nativeErase(mNativeBitmap, c);
|
public android.graphics.Bitmap | extractAlpha()Returns a new bitmap that captures the alpha values of the original.
This may be drawn with Canvas.drawBitmap(), where the color(s) will be
taken from the paint that is passed to the draw call.
return extractAlpha(null, null);
|
public android.graphics.Bitmap | extractAlpha(Paint paint, int[] offsetXY)Returns a new bitmap that captures the alpha values of the original.
These values may be affected by the optional Paint parameter, which
can contain its own alpha, and may also contain a MaskFilter which
could change the actual dimensions of the resulting bitmap (e.g.
a blur maskfilter might enlarge the resulting bitmap). If offsetXY
is not null, it returns the amount to offset the returned bitmap so
that it will logically align with the original. For example, if the
paint contains a blur of radius 2, then offsetXY[] would contains
-2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then
drawing the original would result in the blur visually aligning with
the original.
checkRecycled("Can't extractAlpha on a recycled bitmap");
int nativePaint = paint != null ? paint.mNativePaint : 0;
Bitmap bm = nativeExtractAlpha(mNativeBitmap, nativePaint, offsetXY);
if (bm == null) {
throw new RuntimeException("Failed to extractAlpha on Bitmap");
}
return bm;
|
protected void | finalize()
try {
nativeDestructor(mNativeBitmap);
} finally {
super.finalize();
}
|
public final android.graphics.Bitmap$Config | getConfig()If the bitmap's internal config is in one of the public formats, return
that config, otherwise return null.
return Config.nativeToConfig(nativeConfig(mNativeBitmap));
|
public float | getDensityScale()Returns the density scale for this bitmap, expressed as a factor of
the default density (160.) For instance, a bitmap designed for
displays with a density of 240 will have a density scale of 1.5 whereas a bitmap
designed for a density of 160 will have a density scale of 1.0.
The default density scale is {@link #DENSITY_SCALE_UNKNOWN}.
return mDensityScale;
|
public final int | getHeight()Returns the bitmap's height
return mHeight == -1 ? mHeight = nativeHeight(mNativeBitmap) : mHeight;
|
public byte[] | getNinePatchChunk()Returns an optional array of private data, used by the UI system for
some bitmaps. Not intended to be called by applications.
return mNinePatchChunk;
|
public int | getPixel(int x, int y)Returns the {@link Color} at the specified location. Throws an exception
if x or y are out of bounds (negative or >= to the width or height
respectively).
checkRecycled("Can't call getPixel() on a recycled bitmap");
checkPixelAccess(x, y);
return nativeGetPixel(mNativeBitmap, x, y);
|
public void | getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)Returns in pixels[] a copy of the data in the bitmap. Each value is
a packed int representing a {@link Color}. The stride parameter allows
the caller to allow for gaps in the returned pixels array between
rows. For normal packed results, just pass width for the stride value.
checkRecycled("Can't call getPixels() on a recycled bitmap");
if (width == 0 || height == 0) {
return; // nothing to do
}
checkPixelsAccess(x, y, width, height, offset, stride, pixels);
nativeGetPixels(mNativeBitmap, pixels, offset, stride,
x, y, width, height);
|
public final int | getRowBytes()Return the number of bytes between rows in the bitmap's pixels. Note that
this refers to the pixels as stored natively by the bitmap. If you call
getPixels() or setPixels(), then the pixels are uniformly treated as
32bit values, packed according to the Color class.
return nativeRowBytes(mNativeBitmap);
|
public int | getScaledHeight()Convenience method that returns the height of this bitmap divided
by the density scale factor.
final float scale = getDensityScale();
return scale == DENSITY_SCALE_UNKNOWN ? getWidth() : (int) (getHeight() / scale);
|
public int | getScaledWidth()Convenience method that returns the width of this bitmap divided
by the density scale factor.
final float scale = getDensityScale();
return scale == DENSITY_SCALE_UNKNOWN ? getWidth() : (int) (getWidth() / scale);
|
public final int | getWidth()Returns the bitmap's width
return mWidth == -1 ? mWidth = nativeWidth(mNativeBitmap) : mWidth;
|
public final boolean | hasAlpha()Returns true if the bitmap's pixels support levels of alpha
return nativeHasAlpha(mNativeBitmap);
|
public boolean | isAutoScalingEnabled()Indicates whether this bitmap will be automatically be scaled at the
target's density at drawing time. If auto scaling is enabled, this bitmap
will be drawn with the following scale factor:
scale = (bitmap density scale factor) / (target density scale factor)
Auto scaling is turned off by default. If auto scaling is enabled but the
bitmap has an unknown density scale, then the bitmap will never be automatically
scaled at drawing time.
return mAutoScaling;
|
public final boolean | isMutable()Returns true if the bitmap is marked as mutable (i.e. can be drawn into)
return mIsMutable;
|
public final boolean | isRecycled()Returns true if this bitmap has been recycled. If so, then it is an error
to try to access its pixels, and the bitmap will not draw.
return mRecycled;
|
private static native boolean | nativeCompress(int nativeBitmap, int format, int quality, java.io.OutputStream stream, byte[] tempStorage)
|
private static native int | nativeConfig(int nativeBitmap)
|
private static native android.graphics.Bitmap | nativeCopy(int srcBitmap, int nativeConfig, boolean isMutable)
|
private static native void | nativeCopyPixelsFromBuffer(int nb, java.nio.Buffer src)
|
private static native void | nativeCopyPixelsToBuffer(int nativeBitmap, java.nio.Buffer dst)
|
private static native android.graphics.Bitmap | nativeCreate(int[] colors, int offset, int stride, int width, int height, int nativeConfig, boolean mutable)
|
private static native android.graphics.Bitmap | nativeCreateFromParcel(android.os.Parcel p)
|
private static native void | nativeDestructor(int nativeBitmap)
|
private static native void | nativeErase(int nativeBitmap, int color)
|
private static native android.graphics.Bitmap | nativeExtractAlpha(int nativeBitmap, int nativePaint, int[] offsetXY)
|
private static native int | nativeGetPixel(int nativeBitmap, int x, int y)
|
private static native void | nativeGetPixels(int nativeBitmap, int[] pixels, int offset, int stride, int x, int y, int width, int height)
|
private static native boolean | nativeHasAlpha(int nativeBitmap)
|
private static native int | nativeHeight(int nativeBitmap)
|
private static native void | nativeRecycle(int nativeBitmap)
|
private static native int | nativeRowBytes(int nativeBitmap)
|
private static native void | nativeSetPixel(int nativeBitmap, int x, int y, int color)
|
private static native void | nativeSetPixels(int nativeBitmap, int[] colors, int offset, int stride, int x, int y, int width, int height)
|
private static native int | nativeWidth(int nativeBitmap)
|
private static native boolean | nativeWriteToParcel(int nativeBitmap, boolean isMutable, android.os.Parcel p)
|
final int | ni()
return mNativeBitmap;
|
public void | recycle()Free up the memory associated with this bitmap's pixels, and mark the
bitmap as "dead", meaning it will throw an exception if getPixels() or
setPixels() is called, and will draw nothing. This operation cannot be
reversed, so it should only be called if you are sure there are no
further uses for the bitmap. This is an advanced call, and normally need
not be called, since the normal GC process will free up this memory when
there are no more references to this bitmap.
if (!mRecycled) {
nativeRecycle(mNativeBitmap);
mNinePatchChunk = null;
mRecycled = true;
}
|
public void | setAutoScalingEnabled(boolean autoScalingEnabled)Enables or disables auto scaling for this bitmap. When auto scaling is enabled,
the bitmap will be scaled at drawing time to accomodate the drawing target's pixel
density. The final scale factor for this bitmap is thus defined:
scale = (bitmap density scale factor) / (target density scale factor)
If auto scaling is enabled but the bitmap has an unknown density scale, then
the bitmap will never be automatically scaled at drawing time.
mAutoScaling = autoScalingEnabled;
|
public void | setDensityScale(float densityScale)Specifies the density scale for this bitmap, expressed as a factor of
the default density (160.) For instance, a bitmap designed for
displays with a density of 240 will have a density scale of 1.5 whereas a bitmap
designed for a density of 160 will have a density scale of 1.0.
mDensityScale = densityScale;
|
public void | setNinePatchChunk(byte[] chunk)Sets the nine patch chunk.
mNinePatchChunk = chunk;
|
public void | setPixel(int x, int y, int color)Write the specified {@link Color} into the bitmap (assuming it is
mutable) at the x,y coordinate.
checkRecycled("Can't call setPixel() on a recycled bitmap");
if (!isMutable()) {
throw new IllegalStateException();
}
checkPixelAccess(x, y);
nativeSetPixel(mNativeBitmap, x, y, color);
|
public void | setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)Replace pixels in the bitmap with the colors in the array. Each element
in the array is a packed int prepresenting a {@link Color}
checkRecycled("Can't call setPixels() on a recycled bitmap");
if (!isMutable()) {
throw new IllegalStateException();
}
if (width == 0 || height == 0) {
return; // nothing to do
}
checkPixelsAccess(x, y, width, height, offset, stride, pixels);
nativeSetPixels(mNativeBitmap, pixels, offset, stride,
x, y, width, height);
|
public void | writeToParcel(android.os.Parcel p, int flags)Write the bitmap and its pixels to the parcel. The bitmap can be
rebuilt from the parcel by calling CREATOR.createFromParcel().
checkRecycled("Can't parcel a recycled bitmap");
if (!nativeWriteToParcel(mNativeBitmap, mIsMutable, p)) {
throw new RuntimeException("native writeToParcel failed");
}
|