Android中图像的效果的处理无非是Matrix和ColorMatrix,以及PortialXformed,其中Matrix使用在旋转,镜像方面,而ColorMatrix用于跳转色相,最后一种用于合成
Bitmap bitmap = Bitmap.createBitmap(srcBmp.getWith(),srcBmp.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Bitmap(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //设置色相 ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotation(0,20);//0表示 红色,参数二 表示色度 hueMatrix.setRotation(1,20);//1表示 绿色,参数二 表示色度 hueMatrix.setRotation(2,20);//2表示 蓝色,参数二 表示色度 //饱和度 ColorMatrix saturationMatrix = new ColorMatrix(); saturationMatrix .setSaturation(40f); //亮度 ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(50,50,50,1);//参数:红,绿,蓝,透明度 //融合以上三种 ColorMatrix imgMatrix = new ColorMatrix(); imgMatrix.postConcat(hueMatrix); imgMatrix.postConcat(saturationMatrix ); imgMatrix.postConcat(lumMatrix); //设置到画笔 paint.setColorFilter(new ColorMatrixColorFilter(imgMatrix)); canvas.drawBitmap(bitmap ,0,0,paint);
{1,0,0,0,0} //红 {R} {0,1,0,0,0} //绿 乘 {G} {0,0,1,0,0} //蓝 {B} {0,0,0,1,0} //alpha {A}
其实是矩阵的乘法
ColorMatrix imgMatrix = new ColorMatrix(); imgMatrix.set(int[4][5] cm);
int[] pixelSize = new int[bmp.getWidth()*bmp.getHeight()]; bmp.getPixels(pixelSize,0, bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight()); //对于刮刮卡计算刮开涂层的面积可以使用 int alphaSize = 0; int totalSize = bmp.getWidth()*bmp.getHeight(); for(int i=0;i<bmp.getHeight();i++) { for(int j<0;j<bmp.getWidth();j++) { int h = i*width+j; if(pixelSize[h]==0 ) //色素值为0,表示透明 { alphaSize++; } } } //计算刮开区域比例 int rota = (float)alphaSize/(float)totalSize *100; if(rota>60) { }
(实际开发中某些Bitmap不能修改像素点,不能调用setPixels,请参考5解决)
//定义目标像素点 int[] pixelSize = new int[bmp.getWidth()*bmp.getHeight()]; bmp.getPixels(pixelSize,0, bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight()); int[] dstPixelSize = new int[bmp.getWidth()*bmp.getHeight()]; for(int i=0;i<bmp.getWidth()*bmp.getHeight();i++) { int color = pixelSize[i]; int r= Color.red(color); int g= Color.green(color); int b= Color.blue(color); int alpha = Color.alpha(color); r = 255-r; g = 255-g; b = 255-b; int r = Math.min(255,Math.max(0,r));//保证范围在0-255 int g = Math.min(255,Math.max(0,g)); int b = Math.min(255,Math.max(0,b)); dstPixelSize[i] = Color.argb(alpha,r,g,b); } bmp.setPixels(dstPixelSize,0, bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight());
实际开发中,有些Bitmap不能修改像素点,因此需要转化
mBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(mBitmap); canvas.drawBitmap(bmp, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG)); //转为可修改 int[] pixelSize = new int[mBitmap.getWidth()*mBitmap.getHeight()]; int[] dstPixelSize = new int[mBitmap.getWidth()*mBitmap.getHeight()]; mBitmap.getPixels(pixelSize,0, mBitmap.getWidth(),0,0,mBitmap.getWidth(),mBitmap.getHeight()); for(int i=0;i<mBitmap.getWidth()*mBitmap.getHeight();i++) { int color = pixelSize[i]; int r= Color.red(color); int g= Color.green(color); int b= Color.blue(color); int alpha = Color.alpha(color); if(b>=225) { alpha = 255; dstPixelSize[i] = Color.TRANSPARENT; }else{ r = 255-r; g = 255-g; b = 255-b; r = Math.min(255,Math.max(0,r));//保证范围在0-255 g = Math.min(255,Math.max(0,g)); b = Math.min(255,Math.max(0,b)); dstPixelSize[i] = Color.argb(alpha,r,g,b); } } mBitmap.setPixels(dstPixelSize,0, mBitmap.getWidth(),0,0,mBitmap.getWidth(),mBitmap.getHeight()); setImageBitmap(mBitmap); bmp.recycle();