最近用到Bitmap的各种专场,需要了解Pixels(get set);记录一下相关知识
setPixels:
/**
* <p>Replace pixels in the bitmap with the colors in the array. Each element
* in the array is a packed int prepresenting a non-premultiplied ARGB
* {@link Color}.</p>
*
* @param pixels The colors to write to the bitmap
* @param offset The index of the first color to read from pixels[]
* @param stride The number of colors in pixels[] to skip between rows.
* Normally this value will be the same as the width of
* the bitmap, but it can be larger (or negative).
* @param x The x coordinate of the first pixel to write to in
* the bitmap.
* @param y The y coordinate of the first pixel to write to in
* the bitmap.
* @param width The number of colors to copy from pixels[] per row
* @param height The number of rows to write to the bitmap
*
* @throws IllegalStateException if the bitmap is not mutable
* @throws IllegalArgumentException if x, y, width, height are outside of
* the bitmap's bounds.
* @throws ArrayIndexOutOfBoundsException if the pixels array is too small
* to receive the specified number of pixels.
*/
public void setPixels(int[] pixels, int offset, int stride,
int x, int y, int width, int height) {
checkRecycled("Can't call setPixels() on a recycled bitmap");
if (!isMutable()) {
throw new IllegalStateException();
}
if (width == 0 || height == 0) {
return; // nothing to do
}
checkPixelsAccess(x, y, width, height, offset, stride, pixels);
nativeSetPixels(mNativeBitmap, pixels, offset, stride,
x, y, width, height, mIsPremultiplied);
}
bitmap = BitmapFactory.decodeFile("mnt/sdcard/001.png");
bw = bitmap.getWidth();
bh = bitmap.getHeight();
lineNum = bh / lineH;
pixels = new int[bw * bh];
bitmapTem = Bitmap.createBitmap(bw, bh, Config.ARGB_8888);
bitmap.getPixels(pixels, 0, bw, 0, 0, bw, bh);
1、 IllegalStateException if the bitmap is not mutable 这个异常需要在 bitmap数组副本(bitmapTem) 上setPixels
2、x,y 是bitmapTem数组的拷贝下标位置
3、width 、height 是每次拷贝pixels[] 元素个数
4、x+width < bitmapTem.width();y+height <bitmapTem.height();
5、offset 是pixels[]读取的开始位置
下面是个图片专场例子
package com.example.bitmaptest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends Activity {
Bitmap bitmap, bitmapTem;
int pixels[];
int bw, bh;
int speed = 10;
int lineNum;
int lineH = 50;
PixelsTest pixelsTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pixelsTest = new PixelsTest(this);
setContentView(pixelsTest);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
speed += 10;
if (speed < bw) {
for (int i = 0; i < lineNum; i++) {
if (i % 2 == 0) {
bitmapTem.setPixels(pixels, i * lineH * bw, bw, 0, i * lineH,
speed, lineH);
}
pixelsTest.invalidate();
}
}
}
return super.onTouchEvent(event);
}
class PixelsTest extends View {
public PixelsTest(Context context) {
super(context);
bitmap = BitmapFactory.decodeFile("mnt/sdcard/001.png");
bw = bitmap.getWidth();
bh = bitmap.getHeight();
lineNum = bh / lineH;
pixels = new int[bw * bh];
bitmapTem = Bitmap.createBitmap(bw, bh, Config.ARGB_8888);
bitmap.getPixels(pixels, 0, bw, 0, 0, bw, bh);
}
@Override
protected void onDraw(Canvas c) {
c.drawColor(Color.TRANSPARENT);
c.drawBitmap(bitmapTem, 0, 0, null);
}
}
}
点击屏幕图片专场