Bitmap 官网学习笔记

一、Bitmap摘要

Bitmap实现了Parceable接口,所以bitmap也能够通过intent进行传递。

public final class Bitmap extends Object implements Parcelable

它有两个内部类,都是枚举类,如下所示。

Nested classes

enumBitmap.CompressFormat

Bitmap.CompressFormat规定了几种种压缩格式,分别是JPEG、PNG、WEBP(这个在API30被废弃,转换成更为精确的后面两个)、WEBP_LOSSLESS、WEBP_LOSSY

enumBitmap.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(ALPHA)、R(RED)、G(GREEN)、B(BLUE),也就是通常的三原色加上透明度。每个通道都是四位,由于只能提供较低的图片质量,失真较为严重,所以更推荐使用下面的ARGB_8888。

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左右的内存,而RGBA_F16需要16M左右,更加高清。

Bitmap.Config RGB_565

这种格式只有三原色通道,red有5位(32种),green有6位(64种),red有5位(32种)。

除此以外还有一个int类型的常量。

Constants

intDENSITY_NONE

表示位图由未定的像素创造

二、方法 

booleancompress(Bitmap.CompressFormat format, int quality, OutputStream stream)

此方法用于对bitmap进行压缩,第一个参数 制定了压缩图片的类型,第二个参数指定了quality属性,第三个属性是压缩后输出的流。

返回值是boolean类型,如果方法返回true,可以通过 传递BitmapFactory.decodeStream()一个输入流进行重建位图。

Bitmapcopy(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类型,即返回新位图。

voidcopyPixelsFromBuffer(Buffer src)

从当前位置开始从缓冲区复制像素,覆盖位图的像素。

参数代表数据源。

voidcopyPixelsToBuffer(Buffer dst)

将位图的像素复制到指定缓冲区内。

 

static Bitmap

createBitmap(Bitmap source, int x, int y, int width, int height)

根据原位图以及对应的尺寸参数,创建新的位图。

第一个参数是原位图,第二和第三个参数代表原位图中第一对xy坐标,第四个参数代表每一行有多少像素点,第五个参数代表行数。

返回值是bitmap,返回新建的位图。

 

static Bitmap

createBitmap(int[] colors, int width, int height, Bitmap.Config config)

创建指定宽高的位图,其中每一个像素都由colors数组决定。

第一个参数是颜色数组,用于初始化每一个像素,数组大小不能小于width*height

第二、三个参数是宽高。第四个参数是位图的像素类型,如果指定的像素类型不支持a通道,那么颜色数组中的a值会被忽略。

返回值是bitmap,返回新建的位图。

static BitmapcreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

根据原位图创建新位图,可以由矩形指定。如果原位图不可变,且新位图大小尺寸和原位图相同,则直接返回原位图。新位图都是可变的,除非两种情况(1)直接返回的原位图,且原位图不可变(2)它的Bitmap.Config是HARDWARE类型。

前几个参数前面都有介绍,不再赘述。

第六个参数 Matrix m,提供了矩阵来指定像素区域,此参数是可选项,非必须可以为null。

第七个参数 当需要筛选原图时为true,只有当matrix不仅仅包含平移时才生效(不知道理解的对不对,看了网上别人的总觉得怪怪的,原文true if the source should be filtered. Only applies if the matrix contains more than just translation.)欢迎指正

static Bitmap

createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha, ColorSpace colorSpace)

返回一个可变的指定宽高的位图。

第一个参数 根据display决定了初始密度。

第五个参数 hasAlpha可以指定位图是否透明,如果为false位图将变为黑色而不是透明。

第六个参数 colorSpace 看不懂,只知道指定了位图的colorSpace。

static BitmapcreateBitmap(Bitmap src)

很简单不说了。

static Bitmap

createBitmap(Picture source)

根据picture创建bitmap。

static Bitmap

createBitmap(DisplayMetrics display, int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)

返回一个不可变的指定宽高的bitmap。每个像素都根据颜色数组设置。

第三个参数 offset 在颜色数组中跳过的偏移量。

第四个参数 stride 行之间数组的颜色数。

 

static Bitmap

createBitmap(DisplayMetrics display, int[] colors, int width, int height, Bitmap.Config config)

效果同上。

static Bitmap

createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)

返回一个可变的指定宽高的位图。

static Bitmap

createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)

返回一个可变的指定宽高的位图,且每个像素都根据颜色数组设置。

 

static BitmapcreateBitmap(Picture source, int width, int height, Bitmap.Config config)

根据给定绘图命令的原图片创建新位图。

static BitmapcreateBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha, ColorSpace colorSpace)

返回一个可变的指定宽高的位图。

static BitmapcreateBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config, boolean hasAlpha)

返回一个可变的指定宽高的位图。

static BitmapcreateBitmap(int width, int height, Bitmap.Config config)

返回一个可变的指定宽高的位图。

static BitmapcreateBitmap(int width, int height, Bitmap.Config config, boolean hasAlpha)

返回一个可变的指定宽高的位图。

static Bitmap

createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

根据一个原位图进行放大缩小,获得一个新的位图。

int

describeContents()

继承parcelable的方法。

 

voideraseColor(int c)

用指定颜色填充位图的元素。

第一个参数 c代表了所指定的颜色。

voideraseColor(long color)

效果同上。

第一个参数 color代表来自Color class.

Bitmap

extractAlpha()

返回一个新位图,保存原位图的a值

Returns a new bitmap that captures the alpha values of the original.

Bitmap

extractAlpha(Paint paint, int[] offsetXY)

效果同上。

第一个参数 paint用于修改a值,传null或者默认。

第二个参数 offsetXY用于定位返回位图所需要的偏移量,索引0存x,索引1存y,用于和原位图对齐。

int

getAllocationByteCount()

返回已被分配用来存储位图的大小

int

getByteCount()

返回能被用来存储此位图的最小字节数。

ColorgetColor(int x, int y)

返回指定地方的颜色。

ColorSpace

getColorSpace()

返回此位图的colorspace

Bitmap.Config

getConfig()

如果位图的内在配置是之前的几种公开格式,则返回,否则就返回null。

intgetDensity()

返回此位图的密度。

intgetGenerationId()

返回此位图的生成id。

intgetHeight()

返回位图的高度。

byte[]

getNinePatchChunk()

返回一些私有的数据,是被系统的ui系统所使用,应用程序无法使用。

int

getPixel(int x, int y)

返回指定位置的color。

voidgetPixels(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.

intgetRowBytes()

Return the number of bytes between rows in the bitmap's pixels.

intgetScaledHeight(int targetDensity)

Convenience method that returns the height of this bitmap divided by the density scale factor.

intgetScaledHeight(Canvas canvas)

Convenience for calling getScaledHeight(int) with the target density of the given Canvas.

intgetScaledHeight(DisplayMetrics metrics)

Convenience for calling getScaledHeight(int) with the target density of the given DisplayMetrics.

intgetScaledWidth(int targetDensity)

Convenience method that returns the width of this bitmap divided by the density scale factor.

intgetScaledWidth(DisplayMetrics metrics)

Convenience for calling getScaledWidth(int) with the target density of the given DisplayMetrics.

intgetScaledWidth(Canvas canvas)

Convenience for calling getScaledWidth(int) with the target density of the given Canvas.

intgetWidth()

Returns the bitmap's width

booleanhasAlpha()

Returns true if the bitmap's config supports per-pixel alpha, and if the pixels may contain non-opaque alpha values.

booleanhasMipMap()

Indicates whether the renderer responsible for drawing this bitmap should attempt to use mipmaps when this bitmap is drawn scaled down.

booleanisMutable()

Returns true if the bitmap is marked as mutable (i.e. can be drawn into)

booleanisPremultiplied()

 

Indicates whether pixels stored in this bitmaps are stored pre-multiplied.

booleanisRecycled()

Returns true if this bitmap has been recycled.

voidprepareToDraw()

Builds caches associated with the bitmap that are used for drawing it.

voidreconfigure(int width, int height, Bitmap.Config config)

 

Modifies the bitmap to have a specified width, height, and Config, without affecting the underlying allocation backing the bitmap.

voidrecycle()

Free the native object associated with this bitmap, and clear the reference to the pixel data.

booleansameAs(Bitmap other)

Given another bitmap, return true if it has the same dimensions, config, and pixel data as this bitmap.

voidsetColorSpace(ColorSpace colorSpace)

 

Modifies the bitmap to have the specified ColorSpace, without affecting the underlying allocation backing the bitmap.

voidsetConfig(Bitmap.Config config)

 

Convenience method for calling reconfigure(int, int, android.graphics.Bitmap.Config) with the current height and width.

voidsetDensity(int density)

 

Specifies the density for this bitmap.

voidsetHasAlpha(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).

voidsetHasMipMap(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.

voidsetHeight(int height)

 

Convenience method for calling reconfigure(int, int, android.graphics.Bitmap.Config) with the current width and config.

voidsetPixel(int x, int y, int color)

 

Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate.

voidsetPixels(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.

voidsetPremultiplied(boolean premultiplied)

Sets whether the bitmap should treat its data as pre-multiplied.

voidsetWidth(int width)

 

Convenience method for calling reconfigure(int, int, android.graphics.Bitmap.Config) with the current height and config.

static BitmapwrapHardwareBuffer(HardwareBuffer hardwareBuffer, ColorSpace colorSpace)

Create a hardware bitmap backed by a HardwareBuffer.

voidwriteToParcel(Parcel p, int flags)

Write the bitmap and its pixels to the parcel.

三、使用 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值