android Paint setXfermode()方法讲解

阅读: 评论:0

android Paint setXfermode()方法讲解

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,

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

 
application;
t.Context;
aphics.AvoidXfermode;
aphics.Bitmap;
aphics.BitmapFactory;
aphics.Canvas;
aphics.Color;
aphics.Matrix;
aphics.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) {Draw(canvas);
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
        mPaint.setXfermode(new AvoidXfermode(Color.WHITE,100, AvoidXfermode.Mode.TARGET));
        canvas.drawRect(0,Width(),Height(),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) {Draw(canvas);
    mPaint.setColor(Color.RED);
    canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
    mPaint.setXfermode(new AvoidXfermode(Color.WHITE,255, AvoidXfermode.Mode.TARGET));
    canvas.drawRect(0,Width(),Height(),mPaint);
}
效果:


现在传0试试:

@Override
protected void onDraw(Canvas canvas) {Draw(canvas);
    mPaint.setColor(Color.RED);
    canvas.drawBitmap(mBitmap,new Matrix(),mPaint);
    mPaint.setXfermode(new AvoidXfermode(Color.WHITE,0, AvoidXfermode.Mode.TARGET));
    canvas.drawRect(0,Width(),Height(),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) {de = mode;
    native_instance = nativeCreateXfermode(mode.nativeInt);
}

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

本文发布于:2024-02-03 23:58:48,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170698047251900.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:方法   android   Paint   setXfermode
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23