Use of color matrix ColorMatrix

Source: Internet
Author: User

The color matrix is a 4x 5 matrix, for example:

Multiply it with the color component RGBA to change the value of RGBA.

As for the matrix operation, we will not talk much about it. The following example shows how to use 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; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); view = getView (); setContentView (view);}/*** get the page layout ** @ return */private View getView () {// root layout RelativeLayout root = new RelativeLayout (this); LinearLayout. layoutParams rootlp = new LinearLayout. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT); root. setLayoutParams (rootlp); // The root layout of all text boxes is 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 ++) {// The root layout of each line of text box is 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); // the text box for each row (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 () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {myView. setValue (getData (); myView. invalidate () ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void afterTextChanged (Editable s) {}}); layout. addView (et, lp3) ;}// picture layout 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;}/*** get the data in the text box ** @ 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];}}@Overrideprotected 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);}}

Add the following attributes to the activity to isolate the UI from the input method.

android:windowSoftInputMode="adjustResize"



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.