Tuesday, September 11, 2012

Example of using ColorMatrixColorFilter

More examples of drawing on canvas of custom View listed HERE.

Example of using setColorFilter(ColorFilter) of Paint to apply color filter on bitmap.

Example of using ColorMatrixColorFilter


Create out custom View, MyView.java. Override onDraw(Canvas canvas) method to apply ColorFilter to Paint.

package com.example.androiddraw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
 
 Paint BackPaint = new Paint();
 Context MyContext;

 public MyView(Context context) {
  super(context);
  init(context);
 }

 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context);
 }

 public MyView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init(context);
 }
 
 private void init(Context ctx){
  MyContext = ctx;
  BackPaint.setStyle(Paint.Style.FILL);
  BackPaint.setColor(Color.BLACK);
  
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int w = MeasureSpec.getSize(widthMeasureSpec);
  int h = MeasureSpec.getSize(heightMeasureSpec);
  setMeasuredDimension(w, h);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  
  /*
   * 5x4 matrix for transforming the color+alpha components of a Bitmap. 
   * The matrix is stored in a single array, and its treated as follows: 
   * [  a, b, c, d, e, 
   *   f, g, h, i, j, 
   *   k, l, m, n, o, 
   *   p, q, r, s, t ] 
   * 
   * When applied to a color [r, g, b, a], the resulting color is computed 
   * as (after clamping) 
   * R' = a*R + b*G + c*B + d*A + e; 
   * G' = f*R + g*G + h*B + i*A + j; 
   * B' = k*R + l*G + m*B + n*A + o; 
   * A' = p*R + q*G + r*B + s*A + t;
   */
  
  // Identity Matrix
  /*
  float[] colorMatrix = { 
    1, 0, 0, 0, 0, //red
    0, 1, 0, 0, 0, //green
    0, 0, 1, 0, 0, //blue
    0, 0, 0, 1, 0 //alpha  
  };*/
  
  float[] colorMatrix = { 
    1, 0, 0, 0, 0, //red
    0, 0, 0, 0, 0, //green
    0, 0, 0, 0, 0, //blue
    0, 0, 0, 1, 0 //alpha  
  };
  
  Paint MyPaint = new Paint();
  ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
     MyPaint.setColorFilter(colorFilter);
     
     Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
     canvas.drawBitmap(myBitmap, 100, 100, MyPaint);
 };
 
}


Modify the main activity, MainActivity.java, to set MyView as ContentView.
package com.example.androiddraw;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        MyView myView = new MyView(this);
        setContentView(myView);
    }
    
}


download filesDownload the files.

Related:
- Generate negative image using ColorMatrix
- LightingColorFilter example
- PorterDuffColorFilter example
- Example to apply ColorFilter on ImageView

No comments: