Android Graphics Setxfermode processing Two graphs cross the situation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.