Android濾鏡--ColorMatrixColorFilter--ColorMatrix

之前用到ColorMatrixColorFilter,其中除了傳入一個五階矩陣外慕的,還可以使用ColorMatrix,調(diào)用其成員函數(shù)

    /**
     * Set this colormatrix to scale by the specified values.
     */
    public void setScale(float rScale, float gScale, float bScale,
                         float aScale) {
        final float[] a = mArray;

        for (int i = 19; i > 0; --i) {
            a[i] = 0;
        }
        a[0] = rScale;
        a[6] = gScale;
        a[12] = bScale;
        a[18] = aScale;
    }
該方法為設(shè)置顏色縮放,可以實現(xiàn)顏色增強效果
    @Override
    protected void onDraw(Canvas canvas) {
        //畫原圖
        Paint paint = new Paint();
        RectF rectF = new RectF(100, 100, 100 + bitmap.getWidth() / 2, 100 + bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap, null, rectF, paint);

        //畫設(shè)置ColorFilter效果后的圖
        rectF = new RectF(600, 100, 600 + bitmap.getWidth() / 2, 100 + bitmap.getHeight() / 2);
        ColorMatrix colorMatrix = new ColorMatrix();
        //和顏色增強效果相同
        colorMatrix.setScale(1.2f, 1.2f, 1.2f, 1);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, null, rectF, paint);
    }
縮放運算---乘法 -- 顏色增強.jpg

    /**
     * Set the matrix to affect the saturation of colors.
     *
     * @param sat A value of 0 maps the color to gray-scale. 1 is identity.
     */
    public void setSaturation(float sat) {
        reset();
        float[] m = mArray;

        final float invSat = 1 - sat;
        final float R = 0.213f * invSat;
        final float G = 0.715f * invSat;
        final float B = 0.072f * invSat;

        m[0] = R + sat; m[1] = G;       m[2] = B;
        m[5] = R;       m[6] = G + sat; m[7] = B;
        m[10] = R;      m[11] = G;      m[12] = B + sat;
    }
設(shè)置飽和度
setSaturation.gif

    /**
     * Set the rotation on a color axis by the specified values.
     * <p>
     * <code>axis=0</code> correspond to a rotation around the RED color
     * <code>axis=1</code> correspond to a rotation around the GREEN color
     * <code>axis=2</code> correspond to a rotation around the BLUE color
     * </p>
     */
    public void setRotate(int axis, float degrees) {
        reset();
        double radians = degrees * Math.PI / 180d;
        float cosine = (float) Math.cos(radians);
        float sine = (float) Math.sin(radians);
        switch (axis) {
        // Rotation around the red color
        case 0:
            mArray[6] = mArray[12] = cosine;
            mArray[7] = sine;
            mArray[11] = -sine;
            break;
        // Rotation around the green color
        case 1:
            mArray[0] = mArray[12] = cosine;
            mArray[2] = -sine;
            mArray[10] = sine;
            break;
        // Rotation around the blue color
        case 2:
            mArray[0] = mArray[6] = cosine;
            mArray[1] = sine;
            mArray[5] = -sine;
            break;
        default:
            throw new RuntimeException();
        }
    }
參數(shù)aixs為 0 紅色軸,1,綠色哩治,2,藍色芭析; 參數(shù)degrees為旋轉(zhuǎn)的角度
setRotate.gif

    /**
     * Set this colormatrix to the concatenation of the two specified
     * colormatrices, such that the resulting colormatrix has the same effect
     * as applying matB and then applying matA.
     * <p>
     * It is legal for either matA or matB to be the same colormatrix as this.
     * </p>
     */
    public void setConcat(ColorMatrix matA, ColorMatrix matB) {
        float[] tmp;
        if (matA == this || matB == this) {
            tmp = new float[20];
        } else {
            tmp = mArray;
        }

        final float[] a = matA.mArray;
        final float[] b = matB.mArray;
        int index = 0;
        for (int j = 0; j < 20; j += 5) {
            for (int i = 0; i < 4; i++) {
                tmp[index++] = a[j + 0] * b[i + 0] +  a[j + 1] * b[i + 5] +
                               a[j + 2] * b[i + 10] + a[j + 3] * b[i + 15];
            }
            tmp[index++] = a[j + 0] * b[4] +  a[j + 1] * b[9] +
                           a[j + 2] * b[14] + a[j + 3] * b[19] +
                           a[j + 4];
        }

        if (tmp != mArray) {
            System.arraycopy(tmp, 0, mArray, 0, 20);
        }
    }
組合效果锚扎,將兩個ColorMatrix 的效果組合
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吞瞪,一起剝皮案震驚了整個濱河市馁启,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芍秆,老刑警劉巖惯疙,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異妖啥,居然都是意外死亡霉颠,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門荆虱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒿偎,“玉大人朽们,你說我怎么就攤上這事∷呶唬” “怎么了骑脱?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長苍糠。 經(jīng)常有香客問我叁丧,道長,這世上最難降的妖魔是什么岳瞭? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任拥娄,我火速辦了婚禮,結(jié)果婚禮上瞳筏,老公的妹妹穿的比我還像新娘稚瘾。我一直安慰自己,他們只是感情好姚炕,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布孟抗。 她就那樣靜靜地躺著,像睡著了一般钻心。 火紅的嫁衣襯著肌膚如雪凄硼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天捷沸,我揣著相機與錄音摊沉,去河邊找鬼。 笑死痒给,一個胖子當著我的面吹牛说墨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苍柏,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼尼斧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了试吁?” 一聲冷哼從身側(cè)響起棺棵,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎熄捍,沒想到半個月后烛恤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡余耽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年缚柏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碟贾。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡币喧,死狀恐怖轨域,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情杀餐,我是刑警寧澤疙挺,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站怜浅,受9級特大地震影響铐然,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恶座,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一搀暑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跨琳,春花似錦自点、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至溅潜,卻和暖如春术唬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背滚澜。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工粗仓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人设捐。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓借浊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親萝招。 傳聞我的和親對象是個殘疾皇子蚂斤,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容