一、Bitmap摘要
Bitmap实现了Parceable接口,所以bitmap也能够通过intent进行传递。
public final class Bitmap extends Object implements Parcelable
它有两个内部类,都是枚举类,如下所示。
Nested classes | |
---|---|
enum | Bitmap.CompressFormat Bitmap.CompressFormat规定了几种种压缩格式,分别是JPEG、PNG、WEBP(这个在API30被废弃,转换成更为精确的后面两个)、WEBP_LOSSLESS、WEBP_LOSSY |
enum | Bitmap.Config 表示一个图片存储了多少像素点,这直接影响了图片的颜色深度和透明度,有 |
1、Bitmap.CompressFormat
主要和quality有关
- JPEG 压缩成JPEG格式。quality0代表压缩最小,100代表最好的视觉效果 。
- PNG 压缩成JPEG格式 。更加无损,忽略quality属性。
- WEBP 压缩成WEBP格式。同JPEG
- WEBP_LOSSLESS 压缩为WEBP lossless格式。这种格式下quality表示压缩程度,0代表快速压缩,得到一个较大体积的文件,100代表高强度的压缩,获得体积较小的文件
- WEBP_LOSSY 压缩成WEBP_LOSSY
2、Bitmap.Config
Bitmap.Config有以下像素类型。
Enum values | |
---|---|
Bitmap.Config | ALPHA_8 只有A通道,只能改变透明度 |
Bitmap.Config | ARGB_4444 有四个通道,分别是A( |
Bitmap.Config | ARGB_8888 和上面的基本类似,唯一的区别在于每个通道都是8位,所以每个像素占(8+8+8+8)/8=4字节。显示效果好,也是被推荐的一种格式。 |
Bitmap.Config | HARDWARE 8.0加入的一种图片存储方式,一般的存储方式会在GPU和heap上分别存储一份,而HARDWARE只存储在GPU,节省了一半的内存空间。 |
Bitmap.Config | RGBA_F16 每个像素点占据8字节,图片效果比ARGB_8888还好,但是需要大量的内存空间,比如1920*1080的图片,ARGB_8888格式下存储需要1920*1080*4字节,大概是8M左右的内存,而 |
Bitmap.Config | RGB_565 这种格式只有三原色通道,red有5位(32种),green有6位(64种),red有5位(32种)。 |
除此以外还有一个int类型的常量。
Constants | |
---|---|
int | DENSITY_NONE 表示位图由未定的像素创造 |
二、方法
boolean | compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 此方法用于对bitmap进行压缩,第一个参数 制定了压缩图片的类型,第二个参数指定了quality属性,第三个属性是压缩后输出的流。 返回值是boolean类型,如果方法返回true,可以通过 传递BitmapFactory.decodeStream()一个输入流进行重建位图。 |
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. 以原本的位图作为基础,创建一个新的位图。 第一个参数是新位图的像素存储类型,第二个参数决定是否可以改变新位图的像素值。 返回值是bitmap类型,即返回新位图。 |
void | copyPixelsFromBuffer(Buffer src) 从当前位置开始从缓冲区复制像素,覆盖位图的像素。 参数代表数据源。 |
void | copyPixelsToBuffer(Buffer dst) 将位图的像素复制到指定缓冲区内。
|
static Bitmap |
第一个参数是原位图,第二和第三个参数代表原位图中第一对xy坐标,第四个参数代表每一行有多少像素点,第五个参数代表行数。 返回值是bitmap,返回新建的位图。
|
static Bitmap |
创建指定宽高的位图,其中每一个像素都由colors数组决定。 第一个参数是颜色数组,用于初始化每一个像素,数组大小不能小于width*height 第二、三个参数是宽高。第四个参数是位图的像素类型,如果指定的像素类型不支持a通道,那么颜色数组中的a值会被忽略。 返回值是bitmap,返回新建的位图。 |
static Bitmap | createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 根据原位图创建新位图,可以由矩形指定。如果原位图不可变,且新位图大小尺寸和原位图相同,则直接返回原位图。新位图都是可变的,除非两种情况(1)直接返回的原位图,且原位图不可变(2)它的Bitmap.Config是
|
static Bitmap |
返回一个可变的指定宽高的位图。 第一个参数 根据
|
static Bitmap | createBitmap(Bitmap src) 很简单不说了。 |
static Bitmap |
|
static Bitmap |
|
static Bitmap |
效果同上。 |
static Bitmap |
|
static Bitmap |
|
static Bitmap | createBitmap(Picture source, int width, int height, Bitmap.Config config) 根据给定绘图命令的原图片创建新位图。 |
static Bitmap | createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha, ColorSpace colorSpace)
|
static Bitmap | createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha)
|
static Bitmap | createBitmap(int width, int height, Bitmap.Config config)
|
static Bitmap | createBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha)
|
static Bitmap |
根据一个原位图进行放大缩小,获得一个新的位图。 |
int |
|
void | eraseColor(int c) 用指定颜色填充位图的元素。 第一个参数 c代表了所指定的颜色。 |
void | eraseColor(long color) 效果同上。 第一个参数 color代表来自Color class. |
Bitmap | 返回一个新位图,保存原位图的a值 Returns a new bitmap that captures the alpha values of the original. |
Bitmap |
效果同上。 第一个参数 paint用于修改a值,传null或者默认。 第二个参数 |
int |
|
int |
|
Color | getColor(int x, int y) 返回指定地方的颜色。 |
ColorSpace | 返回此位图的colorspace |
Bitmap.Config | 如果位图的内在配置是之前的几种公开格式,则返回,否则就返回null。 |
int | getDensity() 返回此位图的密度。 |
int | getGenerationId() 返回此位图的生成id。 |
int | getHeight() 返回位图的高度。 |
byte[] |
|
int |
|
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. |
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(Canvas canvas) Convenience for calling |
int | getScaledHeight(DisplayMetrics metrics) Convenience for calling |
int | getScaledWidth(int targetDensity) Convenience method that returns the width of this bitmap divided by the density scale factor. |
int | getScaledWidth(DisplayMetrics metrics) Convenience for calling |
int | getScaledWidth(Canvas canvas) Convenience for calling |
int | getWidth() Returns the bitmap's width |
boolean | hasAlpha() Returns true if the bitmap's config supports per-pixel alpha, and if the pixels may contain non-opaque alpha values. |
boolean | hasMipMap() Indicates whether the renderer responsible for drawing this bitmap should attempt to use mipmaps when this bitmap is drawn scaled down. |
boolean | isMutable() Returns true if the bitmap is marked as mutable (i.e. can be drawn into) |
boolean | isPremultiplied()
Indicates whether pixels stored in this bitmaps are stored pre-multiplied. |
boolean | isRecycled() Returns true if this bitmap has been recycled. |
void | prepareToDraw() Builds caches associated with the bitmap that are used for drawing it. |
void | reconfigure(int width, int height, Bitmap.Config config)
Modifies the bitmap to have a specified width, height, and |
void | recycle() Free the native object associated with this bitmap, and clear the reference to the pixel data. |
boolean | sameAs(Bitmap other) Given another bitmap, return true if it has the same dimensions, config, and pixel data as this bitmap. |
void | setColorSpace(ColorSpace colorSpace)
Modifies the bitmap to have the specified |
void | setConfig(Bitmap.Config config)
Convenience method for calling |
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 | setHasMipMap(boolean hasMipMap) Set a hint for the renderer responsible for drawing this bitmap indicating that it should attempt to use mipmaps when this bitmap is drawn scaled down. |
void | setHeight(int height)
Convenience method for calling |
void | setPixel(int x, int y, int color)
Write the specified |
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 | setPremultiplied(boolean premultiplied) Sets whether the bitmap should treat its data as pre-multiplied. |
void | setWidth(int width)
Convenience method for calling |
static Bitmap | wrapHardwareBuffer(HardwareBuffer hardwareBuffer, ColorSpace colorSpace) Create a hardware bitmap backed by a |
void | writeToParcel(Parcel p, int flags) Write the bitmap and its pixels to the parcel. |
三、使用