首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在安卓系统中使用colorMatrix让黑屏变暗

在安卓系统中使用colorMatrix让黑屏变暗
EN

Stack Overflow用户
提问于 2011-04-07 00:08:51
回答 2查看 2.8K关注 0票数 0

我目前有下面的代码来获得一个Bitmap对象,去掉颜色,然后把它变成红色,这是可行的,但我需要图像中较暗的元素来看起来更暗,目前就像有人在图像上放了一层红色的胶片,这几乎就是我想要的,但需要黑色更暗:

代码语言:javascript
复制
Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
        float[] colorTransform = {
                0, 1f, 0, 0, 0, 
                0, 0, 0f, 0, 0,
                0, 0, 0, 0f, 0, 
                0, 0, 0, 1f, 0};
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); //Remove Colour 
        colorMatrix.set(colorTransform); //Apply the Red

        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);   

        Display display = getWindowManager().getDefaultDisplay(); 

        Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));            

        image.setImageBitmap(resultBitmap);

        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawBitmap(resultBitmap, 0, 0, paint);
EN

回答 2

Stack Overflow用户

发布于 2011-05-24 11:38:57

代码语言:javascript
复制
c = 2;//this will boost your contrast by 2x thus deepening the black (and lightning the white). I'm not sure why at 0 you have anything but black... maybe I don't understand the matrix as well as I think I do... I thought 1 (in place of my c) gets you the original colors.

Anyway... give that a whirl. 


float[] colorTransform = {
                c, 1f, 0, 0, 0, 
                0, c, 0f, 0, 0,
                0, 0, c, 0f, 0, 
                0, 0, 0, 1f, 0};
票数 0
EN

Stack Overflow用户

发布于 2013-02-25 16:21:22

颜色矩阵中前三行的最后一列改变图像的亮度。它在-255...255之间变化。-255将为您提供黑色图像,255将使其为白色。这种方法可以改变你的对比度。比明亮的物体会变得更深,更暗,更暗。然后你可以将你的亮度设置为重复的位置。对比度在-1...1之间更改。

代码语言:javascript
复制
private static void setContrast(ColorMatrix cm, float contrast) {
                float scale = contrast + 1.f;
                   float translate = (-.5f * scale + .5f) * 255.f;
                cm.set(new float[] {
                       scale, 0, 0, 0, translate,
                       0, scale, 0, 0, translate,
                       0, 0, scale, 0, translate,
                       0, 0, 0, 1, 0 });
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5569588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档