新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS位圖操作開發(fā)指導(dǎo)
場景介紹

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供杞縣企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、做網(wǎng)站、html5、小程序制作等業(yè)務(wù)。10年已為杞縣眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進行中。
位圖操作就是指對 PixelMap 圖像進行相關(guān)的操作,比如創(chuàng)建、查詢信息、讀寫像素數(shù)據(jù)等。
接口說明
| 接口名 | 描述 |
|---|---|
| create(InitializationOptions opts) | 根據(jù)圖像大小、像素格式、alpha 類型等初始化選項創(chuàng)建 PixelMap。 |
| create(int[] colors, InitializationOptions opts) | 根據(jù)圖像大小、像素格式、 alpha 類型等初始化選項,以像素顏色數(shù)組為數(shù)據(jù)源創(chuàng)建PixelMap。 |
| create(int[] colors, int offset, int stride, InitializationOptions opts) | 根據(jù)圖像大小、像素格式、alpha 類型等初始化選項,以像素顏色數(shù)組、起始偏移量、行像素大小描述的數(shù)據(jù)源創(chuàng)建 PixelMap。 |
| create(PixelMap source, InitializationOptions opts) | 根據(jù)圖像大小、像素格式、alpha 類型等初始化選項,以源 PixelMap 為數(shù)據(jù)源創(chuàng)建 PixelMap。 |
| create(PixelMap source, Rect srcRegion, InitializationOptions opts) | 根據(jù)圖像大小、像素格式、alpha 類型等初始化選項,以源 PixelMap、源裁剪區(qū)域描述的數(shù)據(jù)源創(chuàng)建 PixelMap。 |
| getBytesNumberPerRow() | 獲取每行像素數(shù)據(jù)占用的字節(jié)數(shù)。 |
| getPixelBytesCapacity() | 獲取存儲Pixelmap像素數(shù)據(jù)的內(nèi)存容量。 |
| isEditable() | 判斷 PixelMap 是否允許修改。 |
| isSameImage(PixelMap other) | 判斷兩個圖像是否相同,包括 ImageInfo 屬性信息和像素數(shù)據(jù)。 |
| readPixel(Position pos) | 讀取指定位置像素的顏色值,返回的顏色格式為 PixelFormat.ARGB_8888。 |
| readPixels(int[] pixels, int offset, int stride, Rect region) | 讀取指定區(qū)域像素的顏色值,輸出到以起始偏移量、行像素大小描述的像素數(shù)組,返回的顏色格式為 PixelFormat.ARGB_8888。 |
| readPixels(Buffer dst) | 讀取像素的顏色值到緩沖區(qū),返回的數(shù)據(jù)是 PixelMap 中像素數(shù)據(jù)的原樣拷貝,即返回的顏色數(shù)據(jù)格式與 PixelMap 中像素格式一致。 |
| resetConfig(Size size, PixelFormat pixelFormat) | 重置 PixelMap 的大小和像素格式配置,但不會改變原有的像素數(shù)據(jù)也不會重新分配像素數(shù)據(jù)的內(nèi)存,重置后圖像數(shù)據(jù)的字節(jié)數(shù)不能超過 PixelMap 的內(nèi)存容量。 |
| setAlphaType(AlphaType alphaType) | 設(shè)置PixelMap的Alpha類型。 |
| writePixel(Position pos, int color) | 向指定位置像素寫入顏色值,寫入顏色格式為PixelFormat.ARGB_8888。 |
| writePixels(int[] pixels, int offset, int stride, Rect region) | 將像素顏色數(shù)組、起始偏移量、行像素的個數(shù)描述的源像素數(shù)據(jù)寫入 PixelMap 的指定區(qū)域,寫入顏色格式為 PixelFormat.ARGB_8888。 |
| writePixels(Buffer src) | 將緩沖區(qū)描述的源像素數(shù)據(jù)寫入 PixelMap,寫入的數(shù)據(jù)將原樣覆蓋 PixelMap 中的像素數(shù)據(jù),即寫入數(shù)據(jù)的顏色格式應(yīng)與 PixelMap 的配置兼容。 |
| writePixels(int color) | 將所有像素都填充為指定的顏色值,寫入顏色格式為 PixelFormat.ARGB_8888。 |
| getPixelBytesNumber() | 獲取全部像素數(shù)據(jù)包含的字節(jié)數(shù)。 |
| setBaseDensity(int baseDensity) | 設(shè)置 PixelMap 的基礎(chǔ)像素密度值。 |
| getBaseDensity() | 獲取 PixelMap 的基礎(chǔ)像素密度值。 |
| setUseMipmap(boolean useMipmap) | 設(shè)置 PixelMap 渲染是否使用mipmap。 |
| useMipmap() | 獲取 PixelMap 渲染是否使用mipmap。 |
| getNinePatchChunk() | 獲取圖像的 NinePatchChunk 數(shù)據(jù)。 |
| getFitDensitySize(int targetDensity) | 獲取適應(yīng)目標像素密度的圖像縮放的尺寸。 |
| getImageInfo() | 獲取圖像基本信息。 |
| release() | 釋放對象關(guān)聯(lián)的本地資源。 |
開發(fā)步驟
- 創(chuàng)建位圖對象 PixelMap。
// 指定初始化選項創(chuàng)建
PixelMap pixelMap2 = PixelMap.create(initializationOptions);
// 從像素顏色數(shù)組創(chuàng)建
int[] defaultColors = new int[] {5, 5, 5, 5, 6, 6, 3, 3, 3, 0};
PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
initializationOptions.size = new Size(3, 2);
initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
PixelMap pixelMap1 = PixelMap.create(defaultColors, initializationOptions);
// 以另外一個PixelMap作為數(shù)據(jù)源創(chuàng)建
PixelMap pixelMap3 = PixelMap.create(pixelMap2, initializationOptions);
- 從位圖對象中獲取信息。
long capacity = pixelMap.getPixelBytesCapacity();
long bytesNumber = pixelMap.getPixelBytesNumber();
int rowBytes = pixelMap.getBytesNumberPerRow();
byte[] ninePatchData = pixelMap.getNinePatchChunk();
- 讀寫位圖像素數(shù)據(jù)。
// 讀取指定位置像素
int color = pixelMap.readPixel(new Position(1, 1));
// 讀取指定區(qū)域像素
int[] pixelArray = new int[50];
Rect region = new Rect(0, 0, 10, 5);
pixelMap.readPixels(pixelArray, 0, 10, region);
// 讀取像素到Buffer
IntBuffer pixelBuf = IntBuffer.allocate(50);
pixelMap.readPixels(pixelBuf);
// 在指定位置寫入像素
pixelMap.writePixel(new Position(1, 1), 0xFF112233);
// 在指定區(qū)域?qū)懭胂袼? pixelMap.writePixels(pixelArray, 0, 10, region);
// 寫入Buffer中的像素
pixelMap.writePixels(intBuf); 分享標題:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS位圖操作開發(fā)指導(dǎo)
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/cohgddp.html


咨詢
建站咨詢
