Android中ColorMatrix的学习

来源:互联网 发布:mac大型单机游戏下载 编辑:程序博客网 时间:2024/06/05 08:32

在上篇Android中Matrix的学习的学习中,我知道Matrix是一个3*3的矩阵,并且知道了它的一些基本算法规则,那么ColorMatrix也是类似的一个矩阵不过它是一个4*5的矩阵,如图
这里写图片描述
这里有篇文章非常详细的介绍了ColorMatrix,大家可以看下Android学习笔记22:图像颜色处理(ColorMatrix),就明白了ColorMatrix在图像颜色变化中所占的作用了,就像我们想改变图像的位置,大小,角度的属性通过Matrix,我们要是改变图像的颜色就可以通过设置ColorMatrix里的矩阵值来改变。一般图像的颜色都是由RGBA来决定的,R:红色(red),G:绿色(green),B:蓝色(blue),A:透明通道(alpha).

ColorMatrix方法的使用

ColorMatrix colorMatrix = new ColorMatrix();//设置其矩阵值colorMatrix.set(new float[] {            1,0,0,0,brightness,            0,1,0,0,brightness,            0,0,1,0,brightness,            0,0,0,1,0,    });Paint paint = new Paint();//将ColorMatrix放入颜色过滤器paint .setColorFilter(new ColorMatrixColorFilter(mColorMatrix));//绘制位图canvas.drawBitmap(mBitmap, 0, 0, mPaint);

ColorMatrix举例

首先上效果图
这里写图片描述
这个实现是在Android中Matrix的学习的代码基础上修改的,所以这里只列出对应修改的地方
activity_main.xml主视图的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Spinner        android:id="@+id/id_spinner"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <com.example.matrixtestdemo.MatrixView        android:id="@+id/id_imageview"        android:layout_width="wrap_content"        android:layout_height="600dp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/id_a"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_b"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_c"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_d"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_e"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/id_f"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_g"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_h"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_i"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_j"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/id_k"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_l"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_m"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_n"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_o"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/id_p"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_q"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_r"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_s"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />        <EditText            android:id="@+id/id_t"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" />    </LinearLayout>    <Button        android:id="@+id/id_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="变换" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="饱和度:" />        <SeekBar            android:id="@+id/id_seekbar_saturation"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:max="200"            android:progress="100" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="亮度:" />        <SeekBar            android:id="@+id/id_seekbar_brightness"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:max="255"            android:progress="127" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="对比度:" />        <SeekBar            android:id="@+id/id_seekbar_contrast"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:max="127"            android:progress="64" />    </LinearLayout></LinearLayout>

MatrixView.java中所增加的代码

    private Paint mPaint;    private ColorMatrix mColorMatrix;    private void init() {        mMatrix = new Matrix();        mPaint  = new Paint();        mColorMatrix = new ColorMatrix();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        mPaint.setColorFilter(new ColorMatrixColorFilter(mColorMatrix));        float[] array = mColorMatrix.getArray();        for (int i=0; i < array.length; i+=5) {            Log.d("TAG", "onDraw:"+array[i]+","+array[i+1]+","+array[i+2]+","+array[i+3]+","+array[i+4]);        }        if (mIsMirror) {            canvas.drawBitmap(mBitmap, 0, 0, null);        }        canvas.drawBitmap(mBitmap, mMatrix, mPaint);        Log.d("TAG", "onDraw:"+mMatrix.toString());    }    /**     * 设置颜色矩阵,修改bitmap的位图颜色     * @param colorMatrix     */    public void setColorMatrix(ColorMatrix colorMatrix) {        mColorMatrix = colorMatrix;        mMatrix.reset();        mMatrix.postTranslate(mSrcWidth+20, 0);        postInvalidate();    }

MainActivity.java就全部贴出来了

import android.app.Activity;import android.graphics.BitmapFactory;import android.graphics.ColorMatrix;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.Spinner;public class MainActivity extends Activity implements OnSeekBarChangeListener {    private String[] mModel = new String[] {            "平移",            "放大",            "缩小",            "旋转",            "由中心点放大",            "由中心点缩小",            "由中心点旋转",            "错切",            "水平镜像",            "垂直镜像",    };    private float colorArray[] = new float[] {            1, 0, 0, 0, 0,            0, 1, 0, 0, 0,            0, 0, 1, 0, 0,            0, 0, 0, 1, 0,        };    private int mEditTextId[] = new int [] {            R.id.id_a,R.id.id_b,R.id.id_c,R.id.id_d,R.id.id_e,            R.id.id_f,R.id.id_g,R.id.id_h,R.id.id_i,R.id.id_j,            R.id.id_k,R.id.id_l,R.id.id_m,R.id.id_n,R.id.id_o,            R.id.id_p,R.id.id_q,R.id.id_r,R.id.id_s,R.id.id_t,    };    private EditText mEditText[];    private MatrixView mMatrixView;    private Button mBtn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    private void init() {        Spinner spinner = (Spinner) findViewById(R.id.id_spinner);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mModel);        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        spinner.setAdapter(adapter);        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> parent, View view,                    int position, long id) {                mMatrixView.setMirror(false);                switch (position) {                case 0:                    mMatrixView.thanslate(100, 100);                    break;                case 1:                    mMatrixView.scale(2, 2);                    break;                case 2:                    mMatrixView.scale(0.5f, 0.5f);                    break;                  case 3:                    mMatrixView.rotate(45);                    break;                  case 4:                    mMatrixView.scaleByCenter(2, 2);                    break;                  case 5:                    mMatrixView.scaleByCenter(0.5f, 0.5f);                    break;                  case 6:                    mMatrixView.rotateByCenter(45f);                    break;                  case 7:                    mMatrixView.skew(0.5f, 0.5f);                    break;                  case 8:                    mMatrixView.setMirror(true);                    mMatrixView.horizontalMirror();                    break;                case 9:                    mMatrixView.setMirror(true);                    mMatrixView.verticalMirror();                    break;                default:                    break;                }            }            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });         mMatrixView = (MatrixView) findViewById(R.id.id_imageview);        mMatrixView.setSrcBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.a));        int length = mEditTextId.length;        mEditText = new EditText[length];        for (int i = 0; i < length; i++) {            mEditText[i] = (EditText) findViewById(mEditTextId[i]);            mEditText[i].setText(colorArray[i]+"");        }        mBtn = (Button) findViewById(R.id.id_btn);        mBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                int length = mEditTextId.length;                for (int i = 0; i < length; i++) {                    String num = mEditText[i].getText().toString();                    if (TextUtils.isEmpty(num)) {                        num = "0";                    }                    colorArray[i] = Float.valueOf(num);                }                ColorMatrix colorMatrix = new ColorMatrix();                colorMatrix.set(colorArray);                mMatrixView.setColorMatrix(colorMatrix);            }        });        SeekBar saturationBar = (SeekBar) findViewById(R.id.id_seekbar_saturation);        SeekBar brightness = (SeekBar) findViewById(R.id.id_seekbar_brightness);        SeekBar contrast = (SeekBar) findViewById(R.id.id_seekbar_contrast);        saturationBar.setOnSeekBarChangeListener(this);        brightness.setOnSeekBarChangeListener(this);        contrast.setOnSeekBarChangeListener(this);    }    @Override    public void onProgressChanged(SeekBar seekBar, int progress,            boolean fromUser) {        int id = seekBar.getId();        ColorMatrix colorMatrix = null;        mMatrixView.setMirror(true);        switch (id) {        case R.id.id_seekbar_saturation:            colorMatrix = setSaturation(progress/100.0f);            break;        case R.id.id_seekbar_brightness:            colorMatrix = setBrightness(progress-127);            break;        case R.id.id_seekbar_contrast:            colorMatrix = setContrast((progress+64)/127.0f);            break;        default:            break;        }        if (colorMatrix != null) {            float[] array = colorMatrix.getArray();            int length = array.length;            for (int i = 0; i < length; i++) {                mEditText[i].setText(array[i]+"");            }        }    }    @Override    public void onStartTrackingTouch(SeekBar seekBar) {    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {    }    /**     * 设置饱和度     * @param sat 范围0~2     * @return     */    public ColorMatrix setSaturation(float sat) {        Log.d("TAG", "setSaturation:"+sat);        ColorMatrix colorMatrix = new ColorMatrix();        colorMatrix.setSaturation(sat);        mMatrixView.setColorMatrix(colorMatrix);        return colorMatrix;    }    /**     * 设置亮度     * @param brightness 范围-127~126     * @return     */    public ColorMatrix setBrightness(float brightness) {        Log.d("TAG", "setBrightness:"+brightness);        ColorMatrix colorMatrix = new ColorMatrix();        colorMatrix.set(new float[] {            1,0,0,0,brightness,            0,1,0,0,brightness,            0,0,1,0,brightness,            0,0,0,1,0,        });        mMatrixView.setColorMatrix(colorMatrix);        return colorMatrix;    }    /**     * 设置对比度     * @param contrast 范围0.5~1.5     * @return     */    public ColorMatrix setContrast(float contrast) {        Log.d("TAG", "setContrast:"+contrast);        ColorMatrix colorMatrix = new ColorMatrix();        colorMatrix.set(new float[] {            contrast,0,0,0,0,            0,contrast,0,0,0,            0,0,contrast,0,0,            0,0,0,1,0,        });        mMatrixView.setColorMatrix(colorMatrix);        return colorMatrix;    }}

总结

对于ColorMatrix这个类 类似Matrix,我们要修改图像的颜色时,只要修改其对应的矩阵值就好了,可以实现多种效果,像黑白照片,高亮度,高对比度的照片等等。在使用上没有Matrix那么复杂,方法较少,上手快,不过是要了解点图像颜色变化的一些概念,做起来比较得心应手。
这里有一个github上的开源项目关于图像处理的做的比较全,可以参考https://github.com/leibnik/about_draw

参考文章

Android学习笔记22:图像颜色处理(ColorMatrix)
Android改变图像的饱和度、亮度和对比度

0 0
原创粉丝点击