关于Android Bitmap Blending – Color Channels

来源:互联网 发布:淘宝卖家寄快递的软件 编辑:程序博客网 时间:2024/04/28 16:24

Android Bitmap Blending – Color Channels

最近美工要求做一种特殊的图片处理,也就是blending。显示效果就如下图。

原理就是将原有的带黑色背景的那块区域抹去,留下最亮的那部分。当然中间很亮的那部分也可以在代码里面来改变原有的颜色。



private Bitmap getARGBImage(){BitmapFactory.Options opt = new BitmapFactory.Options();opt.inPreferredConfig = Config.ARGB_8888;Bitmap red = BitmapFactory.decodeResource(getResources(),R.drawable.red, opt);Bitmap green = BitmapFactory.decodeResource(getResources(),R.drawable.green, opt);Bitmap blue = BitmapFactory.decodeResource(getResources(),R.drawable.blue, opt);Bitmap alphaGray = BitmapFactory.decodeResource(getResources(),R.drawable.alpha, opt);int width = red.getWidth();int height = red.getHeight();Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);result.eraseColor(Color.BLACK);Paint redP = new Paint();redP.setShader(new BitmapShader(red, TileMode.CLAMP, TileMode.CLAMP));redP.setColorFilter(new PorterDuffColorFilter(Color.RED, Mode.MULTIPLY));
//此处改变MULTIPLY的值为SCREEN还能看到不一样的效果。
redP.setXfermode(new PorterDuffXfermode(Mode.SCREEN));Paint greenP = new Paint();greenP.setShader(new BitmapShader(green, TileMode.CLAMP,TileMode.CLAMP));greenP.setColorFilter(new PorterDuffColorFilter(Color.GREEN,Mode.MULTIPLY));greenP.setXfermode(new PorterDuffXfermode(Mode.SCREEN));Paint blueP = new Paint();blueP.setShader(new BitmapShader(blue, TileMode.CLAMP, TileMode.CLAMP));blueP.setColorFilter(new PorterDuffColorFilter(Color.BLUE,Mode.MULTIPLY));blueP.setXfermode(new PorterDuffXfermode(Mode.SCREEN));Canvas c = new Canvas(result);c.drawRect(0, 0, width, height, redP);c.drawRect(0, 0, width, height, greenP);c.drawRect(0, 0, width, height, blueP);Bitmap alpha = Bitmap.createBitmap(width, height, Config.ARGB_8888);int[] alphaPix = new int[width * height];alphaGray.getPixels(alphaPix, 0, width, 0, 0, width, height);int count = width * height;for (int i = 0; i < count; ++i){alphaPix[i] = alphaPix[i] << 8;}alpha.setPixels(alphaPix, 0, width, 0, 0, width, height);Paint alphaP = new Paint();alphaP.setAntiAlias(true);alphaP.setXfermode(new PorterDuffXfermode(Mode.DST_IN));c.drawBitmap(alpha, 0, 0, alphaP);red.recycle();green.recycle();blue.recycle();alphaGray.recycle();alpha.recycle();return result;}

本文主要是参考http://kevindion.com/2011/01/android-bitmap-blending-color-channels/

可能具体效果在原文说明的更清楚。


0 0
原创粉丝点击