android Paint setXfermode()方法讲解

2023-12-21 01:20

本文主要是介绍android Paint setXfermode()方法讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

看过很多开源代码效果,发现里面的代码很多地方都用到这函数,于是想花些时间研究下这个,我现在写博客一般不可能一下子就写完,因为我也要查资料,写代码去验证其效果,而且还要找下好的效果,看能不能用这篇博客知识能实现出来的,整理出来,切入正题!

paint的setXfermode()是图形混合模式,多叫混合模式了,肯定是二张以上的图形才可以,看下Paint类中定义这个方法:

public Xfermode setXfermode(Xfermode xfermode) {long xfermodeNative = 0;
    if (xfermode != null)xfermodeNative = xfermode.native_instance;
    native_setXfermode(mNativePaint, xfermodeNative);
    mXfermode = xfermode;
    return xfermode;
}
发现函数中接受Xfermode形参,点击Xfermode类看看啥东西,
public class Xfermode {protected void finalize() throws Throwable {try {finalizer(native_instance);
        } finally {super.finalize();
        }}private static native void finalizer(long native_instance);

    long native_instance;
}
什么玩意,就这几行代码,而且还有一个被native修饰的方法,这就超越了上层的知识结构,表示不懂,所以不看了,ctrl+T,发现有三个子类,


现在就分别讲一下这三个类怎么使用,以及其能实现什么效果

AvoidXfermode

 这是一个关于颜色模式的类,这是我自己取得,网上很多叫--指定了一个颜色和容差,强制Paint避免在它上面绘图(或者只在它上面绘图),不管它叫什么,

看下AvoidXfermode的类

@Deprecated
public class AvoidXfermode extends Xfermode {// these need to match the enum in AvoidXfermode.h on the native side
    public enum Mode {AVOID   (0),    //!< draw everywhere except on the opColor
        TARGET  (1);    //!< draw only on top of the opColor
        
        Mode(int nativeInt) {this.nativeInt = nativeInt;
        }final int nativeInt;
    }/** This xfermode draws, or doesn't draw, based on the destination's
     * distance from an op-color.
     *
     * There are two modes, and each mode interprets a tolerance value.
     *
     * Avoid: In this mode, drawing is allowed only on destination pixels that
     * are different from the op-color.
     * Tolerance near 0: avoid any colors even remotely similar to the op-color
     * Tolerance near 255: avoid only colors nearly identical to the op-color
     
     * Target: In this mode, drawing only occurs on destination pixels that
     * are similar to the op-color
     * Tolerance near 0: draw only on colors that are nearly identical to the op-color
     * Tolerance near 255: draw on any colors even remotely similar to the op-color
     */
    public AvoidXfermode(int opColor, int tolerance, Mode mode) {if (tolerance < 0 || tolerance > 255) {throw new IllegalArgumentException("tolerance must be 0..255");
        }native_instance = nativeCreate(opColor, tolerance, mode.nativeInt);
    }private static native long nativeCreate(int opColor, int tolerance,
                                            int nativeMode);
}
发现它都没有自己的方法,就是一个构造函数,
public AvoidXfermode(int opColor, int tolerance, Mode mode) {if (tolerance < 0 || tolerance > 255) {throw new IllegalArgumentException("tolerance must be 0..255");
    }native_instance = nativeCreate(opColor, tolerance, mode.nativeInt);
}
参数说明:

int opColor:是一个16进制的颜色值

int tolerance:看的出来这个取值范围为[0,255]

Mode mode值在这个类下面定义了一个枚举就二种值一个是AVOID,一种是TARGET,

现在写一个例子更好里面上面三个参数,

package com.example.myapplication;
import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
 * Created by admin on 2016/7/2.
 */
public class MyView extends View {private Paint mPaint;
    private Bitmap mBitmap;
    public MyView(Context context) {this(context,null);
    }public MyView(Context context, AttributeSet attrs) {this(context, attrs,0);
    }public MyView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
        init();
    }private void init() {mPaint = new Paint();
        mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.bb);
    }@Override
    protected void onDraw(Canvas canvas) {super.onDraw(canvas);
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
        mPaint.setXfermode(new AvoidXfermode(Color.WHITE,100, AvoidXfermode.Mode.TARGET));
        canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(),mPaint);
    }
}
效果图:


尼玛发现这不是下面一个画布把上一个画布挡住了么,还什么混合模式,这个跟硬件加速有关,大家手机中设置界面有关一个叫GPU的东西吧,是不是和CPU很像啊,GPU的出现就是为了绘制图形图像的,在view中禁止GPU硬件加速的方法是:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);这个在构造函数中设置就行
现在再看下效果:


把硬件加速禁止了效果就出来了,看下onDraw()中的代码,canvas是2个画布,


现在我把AvoidXfermode构造函数中的第二个参数变一下,看下其效果,之前传的是100,现在传200,效果:

之前说了这个值最大是255,现在就把它设置为255

@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);
    mPaint.setColor(Color.RED);
    canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
    mPaint.setXfermode(new AvoidXfermode(Color.WHITE,255, AvoidXfermode.Mode.TARGET));
    canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(),mPaint);
}
效果:


现在传0试试:

@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);
    mPaint.setColor(Color.RED);
    canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
    mPaint.setXfermode(new AvoidXfermode(Color.WHITE,0, AvoidXfermode.Mode.TARGET));
    canvas.drawRect(0,0,mBitmap.getWidth(),mBitmap.getHeight(),mPaint);
}
效果:


发现0是透明的效果,现在看第三个形参Mode,这个是传递AvoidXfermode.Mode.TARGET

AvoidXfermode.Mode.TARGET:会判断画布(也就是canvas,而canvas颜色是通过Paint设置的)上的颜色是否会有跟opColor(AvoidXfermode构造函数第一个形参)有一样的颜色,比如我opColor是白色,那么在TARGET模式下就会去判断我们的画布上是否有存在白色的地方,如果有,则把该区域“染”上一层我们画笔定义的颜色,否则不“染”色

AvoidXfermode.Mode.AVOID:跟上面刚好相反,

PorterDuffXfermode

构造函数:

public PorterDuffXfermode(PorterDuff.Mode mode) {this.mode = mode;
    native_instance = nativeCreateXfermode(mode.nativeInt);
}

发现构造函数就一个值,这些值是定义在PorterDuff类中

这篇关于android Paint setXfermode()方法讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/518191

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定