Paint之setColorFilter(二)

来源:互联网 发布:怎么卖软件赚钱 编辑:程序博客网 时间:2024/06/17 08:03
public ColorFilter setColorFilter(ColorFilter filter)  //参数是传入ColorFilter的对象ColorFilter有以下三种:    private ColorMatrixColorFilter colorMatrixColorFilter;//    色彩矩阵颜色过滤器//    ColorMatrixColorFilter(ColorMatrix matrix)//    ColorMatrixColorFilter(float[] array)    private LightingColorFilter lightingColorFilter;//    光照颜色过滤器,可以简单的完成色彩过滤和色彩增强功能//    public LightingColorFilter(int mul, int add)    private PorterDuffColorFilter porterDuffColorFilter;//    图形混合滤镜,其名称是Tomas Proter和Tom Duff两个人名的缩写//    public PorterDuffColorFilter(int srcColor, PorterDuff.Mode mode)//    1、PorterDuffColorFilter只能实现与一个特定颜色值的合成。//    2、通过Mode.ADD(饱和度相加),Mode.DARKEN(变暗),Mode.LIGHTEN(变亮),//      Mode.MULTIPLY(正片叠底),Mode.OVERLAY(叠加),Mode.SCREEN(滤色)//      可以实现与指定颜色的复合。//    3、通过Mode.SRC、Mode.SRC_IN、Mode.SRC_ATOP能够实现setTint()的功能,可以改变纯色图标的颜色。

效果:

mPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, mode));
canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形

这里写图片描述

mPaint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, mode));
canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形

这里写图片描述

mPaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, mode));
canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形

这里写图片描述

mPaint.setColorFilter(new PorterDuffColorFilter(Color.YELLOW, mode));
canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形

这里写图片描述

示例:

MyActivityI

public class MyActivityI extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.test);        //黄色圆形:dst    蓝色矩形:src        ((PorterDuffView) this.findViewById(R.id.view11)).setMode(PorterDuff.Mode.CLEAR);        ((PorterDuffView) this.findViewById(R.id.view12)).setMode(PorterDuff.Mode.ADD);        ((PorterDuffView) this.findViewById(R.id.view21)).setMode(PorterDuff.Mode.SRC);        ((PorterDuffView) this.findViewById(R.id.view22)).setMode(PorterDuff.Mode.DST);        ((PorterDuffView) this.findViewById(R.id.view31)).setMode(PorterDuff.Mode.SRC_OVER);        ((PorterDuffView) this.findViewById(R.id.view32)).setMode(PorterDuff.Mode.DST_OVER);        ((PorterDuffView) this.findViewById(R.id.view41)).setMode(PorterDuff.Mode.SRC_IN);        ((PorterDuffView) this.findViewById(R.id.view42)).setMode(PorterDuff.Mode.DST_IN);        ((PorterDuffView) this.findViewById(R.id.view51)).setMode(PorterDuff.Mode.SRC_OUT);        ((PorterDuffView) this.findViewById(R.id.view52)).setMode(PorterDuff.Mode.DST_OUT);        ((PorterDuffView) this.findViewById(R.id.view61)).setMode(PorterDuff.Mode.SRC_ATOP);        ((PorterDuffView) this.findViewById(R.id.view62)).setMode(PorterDuff.Mode.DST_ATOP);        ((PorterDuffView) this.findViewById(R.id.view71)).setMode(PorterDuff.Mode.XOR);        ((PorterDuffView) this.findViewById(R.id.view72)).setMode(PorterDuff.Mode.OVERLAY);        ((PorterDuffView) this.findViewById(R.id.view81)).setMode(PorterDuff.Mode.DARKEN);        ((PorterDuffView) this.findViewById(R.id.view82)).setMode(PorterDuff.Mode.LIGHTEN);        ((PorterDuffView) this.findViewById(R.id.view91)).setMode(PorterDuff.Mode.MULTIPLY);        ((PorterDuffView) this.findViewById(R.id.view92)).setMode(PorterDuff.Mode.SCREEN);    }}

定义view

/** * 1、关闭硬件加速 * 2、使用图层(离屏绘制) */@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class PorterDuffView extends View {    private int width = 100;    private int height = 100;    private Bitmap dstBmp;    private Bitmap srcBmp;    private Paint mPaint;    public PorterDuffView(Context context, AttributeSet attrs) {        super(context, attrs);        srcBmp = makeSrc(width, height);        mPaint = new Paint();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        mPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, mode));//红色过滤        canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);//源:蓝色矩形    }    static Bitmap makeSrc(int w, int h) {        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);        Canvas c = new Canvas(bm);        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);        p.setColor(0xFF66AAFF);        c.drawRect(0, 0, w, h, p);        return bm;    }    private PorterDuff.Mode mode = PorterDuff.Mode.CLEAR;    public void setMode(PorterDuff.Mode mode) {        this.mode = mode;        invalidate();    }}

布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#e5e5e5">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:orientation="vertical">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:text="CLEAR"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="ADD"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="SRC"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DST"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <com.android.imooc.PorterDuffView                android:id="@+id/view11"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view12"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view21"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view22"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />        </LinearLayout>        <!--000000000000000000000000000000000000000000000000000000000000000-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:text="SRC_OVER"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DST_OVER"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="SRC_IN"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DST_IN"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <com.android.imooc.PorterDuffView                android:id="@+id/view31"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view32"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view41"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view42"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />        </LinearLayout>        <!--000000000000000000000000000000000000000000000000000-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:text="SRC_OUT"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DST_OUT"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="SRC_ATOP"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DST_ATOP"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <com.android.imooc.PorterDuffView                android:id="@+id/view51"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view52"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view61"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view62"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />        </LinearLayout>        <!--00000000000000000000000000000000000000000000000-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:text="XOR"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="OVERLAY"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="DARKEN"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="LIGHTEN"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <com.android.imooc.PorterDuffView                android:id="@+id/view71"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view72"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view81"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view82"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />        </LinearLayout>        <!--00000000000000000000000000000000000000000000000000-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:text="MULTIPLY"                android:textSize="16sp" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="SCREEN"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <com.android.imooc.PorterDuffView                android:id="@+id/view91"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />            <com.android.imooc.PorterDuffView                android:id="@+id/view92"                android:layout_width="0dp"                android:layout_height="60dp"                android:layout_weight="1" />        </LinearLayout>    </LinearLayout></ScrollView>
阅读全文
0 0
原创粉丝点击