getPixels

本文详细介绍了Bitmap类中的getPixels和drawBitmap方法的使用方式及参数含义。getPixels用于获取Bitmap中的像素数据,而drawBitmap则用于将像素数组绘制为Bitmap。文章通过示例展示了如何利用这两个方法进行图像处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
Since: API Level 1

Returns in pixels[] a copy of the data in the bitmap. Each value is a packed int representing a Color. The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed results, just pass width for the stride value.

Parameters
pixels The array to receive the bitmap's colors
offset The first index to write into pixels[]
stride The number of entries in pixels[] to skip between rows (must be >= bitmap's width). Can be negative.
x The x coordinate of the first pixel to read from the bitmap
y The y coordinate of the first pixel to read from the bitmap
width The number of pixels to read from each row
height

The number of rows to read

 

 
IllegalArgumentException    if x, y, width, height exceed the bounds of the bitmap, or if abs(stride) <  width ArrayIndexOutOfBoundsException   if the pixels array is too small to receive the specified number of pixels IllegalArgumentException if x, y, width, height exceed the bounds of the bitmap, or if abs(stride) < width. ArrayIndexOutOfBoundsException

 

 

 
例:
      mBarPixels = new int[mWidth * mHeight];
     mBarBitmap.getPixels(mBarPixels, 0, mWidth, i, 0, mWidth, mHeight);
//从(i,0)点 开始截取像素,放入 mBarPixels 数组中。
      canvas.drawBitmap(mBarPixels, 0, mWidth, 0, 0, mWidth, mHeight, true, null);
//使用像素
public void drawBitmap (int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)
colors Array of colors representing the pixels of the bitmap
offset Offset into the array of colors for the first pixel
stride The number of colors in the array between rows (must be >= width or <= -width).
x The X coordinate for where to draw the bitmap
y The Y coordinate for where to draw the bitmap
width The width of the bitmap
height The height of the bitmap
hasAlpha True if the alpha channel of the colors contains valid values. If false, the alpha byte is ignored (assumed to be 0xFF for every pixel).
paint May be null. The paint used to draw the bitmap
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值