android Paint setXfermode()方法讲解

android Paint setXfermode()方法讲解
摘要由CSDN通过智能技术生成

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

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类中

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值