现在的位置: 首页 > 综合 > 正文

android.grapthics.Bitmap类的详解

2018年05月01日 ⁄ 综合 ⁄ 共 6398字 ⁄ 字号 评论关闭

具体的API翻译见http://www.cnblogs.com/over140/archive/2011/11/21/2256727.html

public final class

Bitmap

extends Object
implements Parcelable

java.lang.Object
   ↳ android.graphics.Bitmap

内部类

  enum Bitmap.CompressFormat

  详细说明位图的压缩格式,支持JPEG
,PNG ,WEBP

 

  enum Bitmap.Config

  位图的结构,ALPHA_8 ,ARGB_4444,ARGB_8888
RGB_565


Public Methods
boolean compress(Bitmap.CompressFormat format,
int quality, OutputStream stream)
Write a compressed version of the bitmap to the specified outputstream.

把位图的压缩信息写入到一个指定的输出流中。如果返回true,可以通过传递一个相应的输出流到BitmapFactory.decodeStream()来重构该位图。

注意:并非所有的格式都直接支持位图结构,所以通过BitmapFactory返回的位图很可能有不同的位深度,或许会丢失每个象素的alpha值

(例如,JPEG 只支持不透明像素)。

(译者注:色深(color depth),也称色位深度(bitdePth),是指在一定分辨率下一个像素能够接受的颜色数量范围。通常,色深用2的n次方来表示。

例如,8 bit的色深包含2的8次方)

参数

                            format  图像的压缩格式;

                            quality 图像压缩比的值,0-100。 0 意味着小尺寸压缩,100意味着高质量压缩。对于有些格式,比如无损压缩的PNG,

它就会忽视quality这个参数设置。

                            stream  写入压缩数据的输出流

    返回值

如果成功地把压缩数据写入输出流,则返回true

该方法可以实现Bitmap保存为图片,图片格式为压缩格式

Bitmap copy(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.
void copyPixelsFromBuffer(Buffer src)
Copy the pixels from the buffer, beginning at the current position, overwriting the bitmap's pixels.
void copyPixelsToBuffer(Buffer dst)
Copy the bitmap's pixels into the specified buffer (allocated by the caller).
static Bitmap createBitmap(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.
从原始位图剪切图像并根据可选的矩阵进行转换。它被初始化为跟源位图有同样的密度
方法是最终的实现,下面的几个方法都是该方法的封装。
参数说明:
  Bitmap source:要从中截图的原始位图
  int x:起始x坐标
  int y:起始y坐标
int width:要截的图的宽度
int height:要截的图的宽度
Matrix(矩阵)来实现旋转等高级方式截图
filter   如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效
返回值:返回一个剪切好的Bitmap
static Bitmap createBitmap(int
width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height.
static Bitmap createBitmap(Bitmap source,
int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.
从源Bitmap中指定区域(x,y,
width, height)中挖出一块来实现
剪切
static Bitmap createBitmap(int[]
colors, int offset, int stride, int width, int height, 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.

返回一个指定宽度和高度的不可变位图,该位图每个像素值等于颜色数组中对应的值。它初始化的密度由getDensity()来设定。

         参数

                      colors 用来初始化像素值得颜色数组

                      offset 在像素数组的第一个颜色值之前要忽略的像素个数

                      stride 行之间像素个数

                      width 位图的宽度

                      height 位图的高度

                      config 位图的结构。如果这个结构不支持每个像素的alpha通道(比如,RGB_565), 那么colors数组中的alpha位将被忽略

(被假定为FF值)。

         异常 

IllegalArgumentException  如果宽度值或高度值小于等于零,或者像素数组的长度小于像素个数,该异常被抛出。

static Bitmap createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap.
从原位图src复制出一个新的位图,和原始位图相同
static Bitmap createBitmap(int[]
colors, int width, int height, 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.
这个函数根据颜色数组来创建位图,注意:颜色数组的长度>=width*height
此函数创建位图的过程可以简单概括为:以width和height创建空位图,然后用指定的颜色数组colors来从左到右从上至下一次填充颜色。
config是一个枚举,可以用它来指定位图“质量”。
static Bitmap createScaledBitmap(Bitmap src,
int dstWidth, int dstHeight, boolean filter)
Creates a new bitmap, scaled from an existing bitmap.
把源Bitmap缩放为宽和高(dstWidth
 dstHeight)的Bitmap
int describeContents()
No special parcel contents.
void eraseColor(int
c)
Fills the bitmap's pixels with the specified Color.
Bitmap extractAlpha()
Returns a new bitmap that captures the alpha values of the original.
Bitmap extractAlpha(Paint paint,
int[] offsetXY)
Returns a new bitmap that captures the alpha values of the original.
final int getByteCount()
Returns the number of bytes used to store this bitmap's pixels.
final Bitmap.Config getConfig()获取bitmap是配置
If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.
int getDensity()获取bitmap的密度

Returns the density for this bitmap.

int getGenerationId()
Returns the generation ID of this bitmap.
final int getHeight()
Returns the bitmap's height
byte[] getNinePatchChunk()
Returns an optional array of private data, used by the UI system for some bitmaps.
int getPixel(int
x, int y)
Returns the Color at
the specified location.
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.
final int getRowBytes()
Return the number of bytes between rows in the bitmap's pixels.
int getScaledHeight(int
targetDensity)
Convenience method that returns the height of this bitmap divided by the density scale factor.
int getScaledHeight(DisplayMetrics metrics)
Convenience for calling getScaledHeight(int) with
the target density of the given DisplayMetrics.
int getScaledHeight(Canvas canvas)
Convenience for calling getScaledHeight(int) with
the target density of the given Canvas.
int getScaledWidth(DisplayMetrics metrics)
Convenience for calling getScaledWidth(int) with
the target density of the given DisplayMetrics.
int getScaledWidth(int
targetDensity)
Convenience method that returns the width of this bitmap divided by the density scale factor.
int getScaledWidth(Canvas canvas)
Convenience for calling getScaledWidth(int) with
the target density of the given Canvas.
final int getWidth()
Returns the bitmap's width
final boolean hasAlpha()是否有透明度
Returns true if the bitmap's config supports per-pixel alpha, and if the pixels may contain non-opaque alpha values.
final boolean isMutable()
Returns true if the bitmap is marked as mutable (i.e.
final boolean isRecycled()是否释放资源
Returns true if this bitmap has been recycled.
void prepareToDraw()
Rebuilds any caches associated with the bitmap that are used for drawing it.
void recycle()释放bitmap占用的内存资源
Free the native object associated with this bitmap, and clear the reference to the pixel data.
释放和位图相关的本地对象,且清除对像素数据的引用。该方法不会同步释放像素数据。它仅仅允许在没有其它引用的情况
下被系统垃圾回收。这个位图被标记为“死的”,意味着如果getPixels()setPixels()被调用,一个异常会抛出,
且它不画任何东西。这个操作不可翻转,所以只有当你确信这个位图没有更多的用处时,它才能被调用。这是一个
高级的调用, 且一般来说它不需要被调用,因为通常的垃圾回收过程会在该位图没有其它引用的时候,释放这些内存。
boolean sameAs(Bitmap other)
Given another bitmap, return true if it has the same dimensions, config, and pixel data as this bitmap.
void setDensity(int
density)

Specifies the density for this bitmap.

void setHasAlpha(boolean
hasAlpha)
Tell the bitmap if all of the pixels are known to be opaque (false) or if some of the pixels may contain non-opaque alpha values (true).
void setPixel(int
x, int y, int color)
Write the specified Color into
the bitmap (assuming it is mutable) at the x,y coordinate.
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.
void writeToParcel(Parcel p,
int flags)
Write the bitmap and its pixels to the parcel.

抱歉!评论已关闭.