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
- Paint之setColorFilter(二)
- Paint之setColorFilter(一)
- 自定义控件三部曲之绘图篇(九)——Paint之setColorFilter
- 自定义控件三部曲之绘图篇(九)——Paint之setColorFilter
- 自定义控件之绘图篇:Paint之setColorFilter
- Android 背景颜色处理[使用 Drawable.setColorFilter(),或者paint.setColorFilter()]
- 自定义View之RGB颜色变化Paint画笔颜色过滤器setColorFilter,LightingColorFilter光照过滤器
- 详解Paint的setColorFilter(ColorFilter filter)
- 自定义view(二):onDraw 之 Paint
- Android—图形用户界面之绘图Paint(二)
- Android学习之Paint图形图像处理(二)
- 关于ImageView的setColorFilter()
- 自定义控件三部曲之绘图篇(十一)——Paint之setXfermode(二)
- 自定义控件三部曲之绘图篇(十一)——Paint之setXfermode(二)
- 自定义控件点滴2(setColorFilter)
- ImageView setColorFilter 滤镜效果(描边)
- 利用Paint的 setColorFilter 颜色过滤 来实现按钮被按下的效果
- 【Paint】Android 自定义View之Paint篇(更新中...)
- Vue2集成AdminLte 后台模板
- Java中常用的for循环
- 1005. Spell It Right (20)
- 单向加密——JAVA消息摘要算法使用
- scipy在图像中的读取和保存图片功能
- Paint之setColorFilter(二)
- 项目启动错误--搭建图片服务器
- 单例模式
- 移动订单ERP接口
- leetcode 44. Wildcard Matching
- 并查集模板
- urllib与requests的对比
- 201609-3炉石传说
- 小发包小程序系统开发分析