- 14
- 0
- 约4.35千字
- 约 5页
- 2016-11-27 发布于河南
- 举报
AndroidBitmap
Bitmap:
类说明:该类为final 类,实现了Parcelable接口。此类仅有一个构造方法为private所以不new方法创建对象。
一、枚举:
CompressFormat:
列举了Bitmap的压缩格式。分别是JPEG、PNG两种格式的压缩。
Config:指定了Bitmap的色彩深度(色彩位数)。其枚举值分别为:
ARGB_4444:16位。即A4R4G4B4,每个元素个站4位。
ALPHA_8:8位。
ARGB_8888:32位。
RGB_565:16位。
二、创建位图:
得到资源位图有两种方式得到:
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baby);
height, Bitmap.config);
根据指定的宽、高和色彩格式创建一张可变的bitmap。bitmap的像素将被设置成黑色透明。
如果设置了画笔将显示的是画笔的颜色。
2、createBitmap(Bitmap src);
根据src创建一张不可变的位图。
createBitmap(Bitmap soures, int x, int y, int width, int height);
用soutes的一部分创建一张不可变的位图,x, y 分别为soures上的起始坐标。Width表示每行像素的数量,heigth为要创建的行数。
注意:如果x, y, width, heigth不在soures范围内则抛出IllegelArgomentException异常。
该方法对方法4进行了封装。
createBitmap(Bitmap soures, int x, int y, int width, int height, Matrix m, boolean filter);
该方法是在截取了soures后,又对截取的位图通过m进行了一下矩阵变换。然后生成一不可变的张位图。filter如果为true则表示对变换后的位图进行过滤。其中m是可选参数。如果为null则和方法3相同。
其内部是先大致如下:
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);
RectF deviceR = new RectF();
m.mapRect(deviceR, dstR);
bitmap = createBitmap(width, height, Config.ARGB_8888);
canvas.translate(-deviceR.left, -deviceR.top);
canvas.concat(m);
paint = new Paint();
paint.setFilterBitmap(filter);
canvas.setBitmap(bitmap);
canvas.drawBitmap(source, srcR, dstR, paint);
return bitmap;
5、createBitmap(int[] colors, int width, int height, Bitmap.Config config);
根据指定的颜色创建一张不可变的位图。其密度是调用getDensity()得到的。
如果config不支持alpha则colors中的alpha将被忽略掉。
注意:color = width * height; width 0; height 0; 否则将抛出数组
越界。
createBitmap(int[] colors, int offset, int stride, int width,
int height, Bitmap.Config config);
根据指定的颜色创建一张不可变的位图。offset、stride是针对colors进行的操作。分别表示从数组中的那个元素开始创建、行间隔是多少。
注意:color = width * height; width 0; height 0;否则将抛出数组越
界。stride的绝对值不能小于width,否则将抛出参数异常。
createScaledBitmap(Bitmap bitmap, int dstWidth, int dstHeigth,
bo
原创力文档

文档评论(0)