颜色矩阵是一个4x 5的矩阵,如下图:
让它和颜色分量RGBA相乘去改变RGBA的值
至于矩阵的运算就不多说了。下面给个示例演示ColorMatrix的使用:
MainActivity:
package com.home.colormatrix; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.text.method.DigitsKeyListener; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class MainActivity extends Activity { private View view; private float[] values = new float[20]; private MyView myView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = getView(); setContentView(view); } /** * 得到页面布局 * * @return */ private View getView() { // 根布局 RelativeLayout root = new RelativeLayout(this); LinearLayout.LayoutParams rootlp = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); root.setLayoutParams(rootlp); // 所有文本框根布局 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); LinearLayout editLayout = new LinearLayout(this); editLayout.setId(30); editLayout.setOrientation(LinearLayout.VERTICAL); root.addView(editLayout, lp); int index = 1; for (int i = 0; i < 4; i++) { // 每一行文本框根布局 LinearLayout layout = new LinearLayout(this); LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.setOrientation(LinearLayout.HORIZONTAL); editLayout.addView(layout, lp2); // 每一行的文本框 for (int j = 0; j < 5; j++) { EditText et = new EditText(this); LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); et.setId(index++); et.setKeyListener(new DigitsKeyListener(false, true)); et.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { myView.setValue(getData()); myView.invalidate(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); layout.addView(et, lp3); } } // 图片布局 myView = new MyView(this); RelativeLayout.LayoutParams viewlp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); viewlp.addRule(RelativeLayout.ABOVE, 30); root.addView(myView, viewlp); return root; } /** * 获取文本框里面的数据 * * @return */ private float[] getData() { for (int i = 0; i < 20; i++) { EditText et = (EditText) view.findViewById(i + 1); String data = et.getText().toString(); if (data == null || "".equals(data)) { values[i] = 0; } else { values[i] = Float.valueOf(data); } } return values; } }
MyView:
package com.home.colormatrix; import com.home.testcolormatrix.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView extends View { private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); private Bitmap bitmap; private float[] array = new float[20]; public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } public MyView(Context context) { super(context); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.test); invalidate(); } public void setValue(float[] array) { for (int i = 0; i < array.length; i++) { this.array[i] = array[i]; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColorFilter(null); canvas.drawBitmap(bitmap, 0, 0, paint); ColorMatrix cm = new ColorMatrix(); cm.set(array); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, 0, 0, paint); } }
给activity加上下面属性可以让UI和输入法隔离。
android:windowSoftInputMode="adjustResize"