private class SampleView extends View{
private Bitmap[] mBitmaps;
private Bitmap[] mJPEGs;
private Bitmap[] mPNGs;
private int[] mColors;
private Paint mPaint;
private static final int WIDTH=50;
private static final int HEIGHT=50;
private static final int STRIDE=64;//must be >=WIDTH
public SampleView(Context context) {
super(context);
setFocusable(true);
mColors=initColors();
mBitmaps=new Bitmap[6];
mPaint=new Paint();
mPaint.setDither(true);
//these three are initialized with colors[]
mBitmaps[0]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_8888);
mBitmaps[1]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.RGB_565);
mBitmaps[2]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_4444);
//these three will have their colors[] set later.
mBitmaps[3]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
mBitmaps[4]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.RGB_565);
mBitmaps[5]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_4444);
for (int i = 3; i <=5; i++) {
mBitmaps[i].setPixels(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT);
}
//now decode/encode using JPEG and PNG
mJPEGs=new Bitmap[mBitmaps.length];
mPNGs=new Bitmap[mBitmaps.length];
for (int i = 0; i < mBitmaps.length; i++) {
mJPEGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.JPEG,80);
mPNGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.PNG,0);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
for (int i = 0; i < mBitmaps.length; i++) {
canvas.drawBitmap(mBitmaps[i], 0, 0, null);
canvas.drawBitmap(mJPEGs[i], 80,0, null);
canvas.drawBitmap(mPNGs[i], 160, 0, null);
canvas.translate(0, mBitmaps[i].getHeight());
}
//draw the color array directly,w/o(without) creating a bitmap object
canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT, true, null);
canvas.translate(0, HEIGHT);
canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT, true, mPaint);
}
private Bitmap codec(Bitmap bitmap, CompressFormat format, int quality) {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(format, quality, baos);
byte[] data=baos.toByteArray();
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
private int[] initColors() {
int[] colors=new int[STRIDE*HEIGHT];
for (int y = 0; y < HEIGHT; y++) {//use of x,y is legible then the use of i,j
for (int x = 0; x < WIDTH; x++) {
int r = x * 255 / (WIDTH - 1);
int g = y * 255 / (HEIGHT - 1);
int b = 255 - Math.min(r, g);
int a = Math.max(r, g);
colors[y*STRIDE+x]=(a<<24)|(r<<16)|(g<<8)|(b);//the shift operation generates the color ARGB
}
}
return colors;
}
}
}
Bitmap中setPiexls的解释:
public void setPixels (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. Each element in the array is a packed int prepresenting a Color
Parameters
pixels | The colors to write to the bitmap |
---|---|
offset | The index of the first color to read from pixels[] |
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). |
x | The x coordinate of the first pixel to write to in the bitmap. |
y | The y coordinate of the first pixel to write to in the bitmap. |
width | The number of colors to copy from pixels[] per row |
height | The number of rows to write to the bitmap |
Throws
IllegalStateException | if the bitmap is not mutable |
---|---|
IllegalArgumentException | if x, y, width, height are outside of the bitmap's bounds. |
ArrayIndexOutOfBoundsException | if the pixels array is too small to receive the specified number of pixels. |