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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创