Setxfermode
Set the mode at which two pictures intersect
We know that under normal circumstances, drawing on an existing image will add a new layer of shape on top of it. If the new paint is completely opaque, it will completely obscure the paint below;
And setxfermode can solve this problem.
In general, the usage is like this.
New Canvas (BITMAP1);p Aint.setxfermode (new Porterduffxfermode (mode.src_in)); Canvas.drawbitmap ( Mask, 0f, 0f, paint);
It is the problem that is displayed when the image mask is drawn on the image bitmap1.
Canvas's original picture can be understood as the background is DST
The picture on the new picture can be understood as the foreground is SRC
The values of mode are as follows
Demo of a sliding layer
PackageCom.weidingqiang.testviewb;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Path;ImportAndroid.graphics.PorterDuff;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;/*** Created by Weidingqiang on 15/11/20.*/ Public classXfermodeviewextendsView {PrivateBitmap Mbgbitmap,mfgbitmap; PrivatePaint Mpaint; PrivateCanvas Mcanvas; PrivatePath MPath; PublicXfermodeview (Context context) {Super(context); } PublicXfermodeview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } Private voidinit () {Mpaint=NewPaint (); Mpaint.setalpha (0); Mpaint.setxfermode (NewPorterduffxfermode (PorterDuff.Mode.DST_IN)); Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokejoin (Paint.Join.ROUND); Mpaint.setstrokewidth (50); Mpaint.setstrokecap (Paint.Cap.ROUND); MPath=NewPath (); Mbgbitmap=Bitmapfactory.decoderesource (Getresources (), R.MIPMAP.IMAGE_BG); Mfgbitmap=Bitmap.createbitmap (Mbgbitmap.getwidth (), Mbgbitmap.getheight (), Bitmap.Config.ARGB_8888); Mcanvas=NewCanvas (MFGBITMAP); Mcanvas.drawcolor (Color.gray); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:mPath.reset (); Mpath.moveto (Event.getx (), event.gety ()); Break; CaseMotionEvent.ACTION_MOVE:mPath.lineTo (Event.getx (), event.gety ()); Break; } mcanvas.drawpath (Mpath,mpaint); Invalidate (); return true; } @Overrideprotected voidOnDraw (canvas canvas) {canvas.drawbitmap (Mbgbitmap,0,0,NULL); Canvas.drawbitmap (Mfgbitmap,0,0,NULL); }}
Android Graphics Setxfermode processing Two graphs cross the situation