天天看點

android ColorMatrix 顔色矩陣的使用 效果圖首先

 效果圖
android ColorMatrix 顔色矩陣的使用 效果圖首先

android ColorMatrix 顔色矩陣的使用 效果圖首先

效果是不是很炫酷呢

那我們來看看我們要怎麼實作它們吧

首先

我們來看一下我們的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        android:gravity="center"
        android:orientation="horizontal" >
        
            <ImageView
        android:id="@+id/mImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/letme" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >
              <SeekBar
        android:id="@+id/sb_Hue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
 />
            
            
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >
              
              <SeekBar
        android:id="@+id/sb_Sat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <SeekBar
                android:id="@+id/sb_Lum"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>
           

話不多說上源碼

private Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
        /**
         * Android系統不允許直接修改原圖,
         * 必須通過原圖建立一個同樣大小的bitmap,
         * 并将原圖繪制到該Bitmap中,
         * 以一個副本的形式來修改圖像
         */
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Paint p = new Paint();

        //色調矩陣
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);//紅
        hueMatrix.setRotate(1, hue);//綠
        hueMatrix.setRotate(2, hue);//藍

        //飽和度矩陣
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        //亮度矩陣
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);


        //圖檔矩陣
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        p.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        c.drawBitmap(bm, 0, 0,p);

        return bmp;

    }   
           

看懂了上面的接下來的操作就簡單了(看代碼吧)

public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {

        switch (seekBar.getId()) {
        case R.id.sb_Hue:
            hue = (progress - MID_VALIE) * 1.0F / MID_VALIE * 180;
            break;
        case R.id.sb_Lum:
            saturation = progress * 1.0F / MID_VALIE;
            break;
        case R.id.sb_Sat:
            lum = progress * 1.0F / MID_VALIE;
            break;
        }

        img.setImageBitmap(handleImageEffect(bm, hue, saturation, lum));
    }
           

好了到這裡就大功告成了,還有些初始化的代碼我就不一一羅列了,寫出核心代碼足矣。

有興趣的可以點選這裡檢視