Android 利用setpixels设置单色位图

1.创建纯白色位图

Bitmap bm1 = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888); int[] pix = new int[picw * pich]; for (int y = 0; y < pich; y++) for (int x = 0; x < picw; x++) { int index = y * picw + x; int r = ((pix[index] >> 16) & 0xff)|0xff; int g = ((pix[index] >> 8) & 0xff)|0xff; int b =( pix[index] & 0xff)|0xff; pix[index] = 0xff000000 | (r << 16) | (g << 8) | b; // pix[index] = 0xff000000; } bm1.setPixels(pix, 0, picw, 0, 0, picw, pich); BitmapDrawable bmp11=new BitmapDrawable(bm1); iv.setImageBitmap(bm1);

 

2.创建纯黑色位图

Bitmap bm1 = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888); int[] pix = new int[picw * pich]; for (int y = 0; y < pich; y++) for (int x = 0; x < picw; x++) { int index = y * picw + x; //int r = ((pix[index] >> 16) & 0xff)|0xff; //int g = ((pix[index] >> 8) & 0xff)|0xff; //int b =( pix[index] & 0xff)|0xff; // pix[index] = 0xff000000 | (r << 16) | (g << 8) | b; pix[index] = 0xff000000; } bm1.setPixels(pix, 0, picw, 0, 0, picw, pich); BitmapDrawable bmp11=new BitmapDrawable(bm1); iv.setImageBitmap(bm1);

你可能感兴趣的:(android)