android.graphics.ColorFilter

   ColorFilter和它的三个子类LightingColorFilter,PorterDuffColorFilter,ColorMatrixColorFilter在sdk上并没有过多的介绍,它的功能应该就是按照一定的规则改变图片的颜色,三个子类各有各的不同的改法规则,其中ColorMatrixColorFilter的改变法则就是ColorMatrix的改变规则,它是ColorMatrix的应用,LightingColorFilter这个类只有一个初始化函数,

public LightingColorFilter (int mul, int add)

它的改变法则是图片的RGB值分别*mul+add,然后对255求余,最后得到新的RGB值,整个过程中alpha不参与改变。PorterDuffColorFilter这个类虽然也只有一个函数,但是它的改变法则就有点多了,

public PorterDuffColorFilter (int srcColor, PorterDuff.Mode mode)

改变法则跟选定的srcColor和mode都有关,具体后续再探讨,上代码

package com.hahajlu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.Rect;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuff;
import android.graphics.ColorMatrixColorFilter;
public class ColorfilterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
}
class SampleView extends View
{
   Drawable mdraw; 
   Drawable mdraw2;
   Drawable mdraw3;
   public SampleView(Context context)
   {
    super(context);
    mdraw=context.getResources().getDrawable(R.drawable.btn_check_off);
    mdraw2=context.getResources().getDrawable(R.drawable.btn_check_on);
    mdraw3=context.getResources().getDrawable(R.drawable.btn_circle_normal);
    mdraw.setBounds(new Rect(0,0,38,48));
    mdraw2.setBounds(40, 0, 78, 48);
    mdraw3.setBounds(80, 0, 120, 48);
   
   }
public void drawSample(Canvas canvas,ColorFilter colorfilter)
{
    mdraw.setColorFilter(colorfilter);
    mdraw2.setColorFilter(colorfilter);
    mdraw3.setColorFilter(colorfilter); 
    mdraw.draw(canvas);
 mdraw2.draw(canvas);
 mdraw3.draw(canvas); 
}
  
@Override
protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 canvas.drawColor(Color.WHITE);
 mdraw.draw(canvas);
 mdraw2.draw(canvas);
 mdraw3.draw(canvas);
 canvas.translate(0, 50);
    ColorFilter colorfilter=new ColorFilter();
    drawSample(canvas,colorfilter);
 canvas.translate(0, 50);
 LightingColorFilter lcf=new LightingColorFilter(100,100);
 drawSample(canvas,lcf);
 canvas.translate(0, 50); 
 PorterDuffColorFilter pdcf=new PorterDuffColorFilter(0xffff0000,PorterDuff.Mode.SRC_ATOP);
 drawSample(canvas,pdcf);
 canvas.translate(0, 50);
 ColorMatrixColorFilter cmcf=new ColorMatrixColorFilter(
   new float[]{
   1,0,0,0,100,
   0,1,0,0,100,
   0,0,1,0,100,
   0,0,0,1,0
   }
   );
 drawSample(canvas,cmcf);
 super.onDraw(canvas);
}
  
}

运行后可以发现,ColorFilter colorfilter=new ColorFilter();这样的东东调用后是没有任何效果的,除了mdraw3.setColorFilter(colorfilter); 这样设置之外还可以在paint里边设置,paint.setColorFilter(colorfilter);

 

你可能感兴趣的:(filter,colors)