Porterduffxfermode correct use Method (detailed)

Source: Internet
Author: User

In the correct posture: yellow is the first to draw the lower layer of the blue for the post-painting is the upper SRC is the upper DST is the lower in is the intersection out is not want to hand over

Example: Porterduff.mode. The src_in parameter, which represents the draw intersection of two layers. Show Upper

Core Principles ( Border):

Figure one (correct posture): Two bitmap have been mobile phone width height as an example of the boundary: the width of the height of the mobile phone is wide, so that even a graphic opaque and opaque color intersect in the boundary with the superposition mode, so the effect is consistent with the left hand

figure II (usual misunderstanding): a bitmap drawcircle two so the boundary is the two rectangles I drew. Note: The boundary is the area boundary of the first layer is used in the overlay mode to get outside the boundary without superposition mode, so it becomes this way.
When the Srcin

Example: All the comments here are very important, so we need to look at the heart!!! (I think the ~)
code on GitHub : demos --animation, Surfaceview, drawing research ,xfermode-- Select mode
https://github.com/luhaoaimama1/ZoneStudio/blob/master/Android_Zone_Test/src/com/example/mylib_test/activity/ Animal/viewa/xfermodeview.java

 @Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);    Ondrawfinal (canvas); }Private void ondrawfinal(Canvas canvas) {if(mode!=NULL) {Paint.reset ();//Both//BitmapXfermodeExample1 (canvas);GraphXfermodeExample2 (canvas); }    }//This correspondence situation Figure 2    Private void GraphXfermodeExample2(Canvas canvas) {//Why so write Savelayer because Super.draw will be the bottom layer want to and the bottom without interference does not overlap need save or Direct draw bitmap        //But painting bitmap painting that bitmap need a new canvas to overlay so savelayer the fastest!!!!         //Both of these are OK//canvas.savelayer (0, 0, getwidth (), GetHeight (), paint,canvas.all_save_flag);Canvas.savelayeralpha (0,0, GetWidth (), GetHeight (),255, Canvas.all_save_flag);        Paint.setcolor (Color.yellow); Canvas.drawcircle (GetWidth ()/2, GetHeight ()/2, $, paint); Paint.setxfermode (NewPorterduffxfermode (mode));        Paint.setcolor (Color.Blue); Canvas.drawcircle (GetWidth ()/2+ $, GetHeight ()/2+ $, $, paint);//Canvas.restoretocount (1);//Can not be justified by the multi-layer should be used!!! But I don't know why it doesn't make!!!}//This correspondence situation Figure 1    Private void BitmapXfermodeExample1(Canvas canvas) {Canvas canvas2=NewCanvas ();//Painted yellow perfect screen that kind of bitmapBitmap BT = Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_4444);        Canvas2.setbitmap (BT);        Paint.setcolor (Color.yellow); Canvas2.drawcircle (GetWidth ()/2, GetHeight ()/2, $, paint);//Draw blue perfect screen that kind of bitmapBitmap BT2 = Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_4444);        Canvas2.setbitmap (BT2);        Paint.setcolor (Color.Blue); Canvas2.drawcircle (GetWidth ()/2+ $, GetHeight ()/2+ $, $, paint);//--------available with 1 bitmap overlay The simplest------------------------------------------------------------------------------------------------//Canvas2.setbitmap (BT);//Paint.setxfermode (new Porterduffxfermode (mode));//Canvas2.drawbitmap (BT2, 0, 0, paint);//// if canvas.draw, this sentence is necessary because the system Super.draw will draw a layer if not empty overlay mode will cause confusion//Paint.setxfermode (null);///// Finally, the final synthetic bitmap is shown with a canvas//Canvas.drawbitmap (BT, 0, 0, paint);//--------available 1 End------------------------------------------------------------------------------------------------//--------available 2 bitmap overlay The last discovery mode and SetBitmap are comparable and overlay modes can be used on draw and not once//Bitmap BT3 = Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_4444);//Canvas2=new Canvas (BT3);//Canvas2.drawbitmap (BT, 0, 0, paint);//Paint.setxfermode (new Porterduffxfermode (mode));//Canvas2.drawbitmap (BT2, 0, 0, paint);//Paint.setxfermode (null);//Canvas.drawbitmap (BT3, 0, 0, paint);//--------available 2 End------------------------------------------------------------------------------------------------//--------available 3 second layer overlay///Paint.setxfermode (NULL);//This line to note super.draw also session if using overlay mode will be confusing of course the default mode is null if not written can not write this line//canvas.savelayer (0, 0, getwidth (), getheight (), Paint,//Canvas.all_save_flag);//Canvas.drawbitmap (BT, 0, 0, paint);//Paint.setxfermode (new Porterduffxfermode (mode));//Canvas.drawbitmap (BT2, 0, 0, paint);//Canvas.restoretocount (1);//Can not be justified by the multi-layer should be used!!! But I don't know why it doesn't make!!!//--------available 3 End------------------------------------------------------------------------------------------------//--------4 layers layer Overlay available///Paint.setxfermode (NULL);//This line to note super.draw also session if using overlay mode will be confusing of course the default mode is null if not written can not write this lineCanvas.savelayer (0,0, GetWidth (), getheight (), paint, canvas.all_save_flag); Canvas.drawbitmap (BT,0,0, paint); Paint.setxfermode (NewPorterduffxfermode (mode));//Note that the Savelayer paint here is in overlay mode!!!!!!!!!!!!!!!!! Canvas.savelayer (0,0, GetWidth (), getheight (), paint, canvas.all_save_flag); Paint.setxfermode (NULL);//Note it needs to be here. Stack mode to empty or overlay with layer's empty mapCanvas.drawbitmap (BT2,0,0, paint);//Canvas.restoretocount (1);//Can not be justified by the multi-layer should be used!!! But I don't know why it doesn't make!!!//--------can be ended with 4-------------------------------------------------------------------}
Setxfermode's attention.

1 paint.setxfermode (NULL);
... 2 and 1 reciprocal effects
2 Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));
... The 2 between this one is interacting with 1.
3 Paint.setxfermode (NULL);
... 4 and 3 reciprocal effects
4 Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));

Understanding of the stack stack in the layer

This stack: Restore,restoretocount But when the layer is in the stack, subsequent drawxxx operations occur on this layer, and when the layer is rolled back, it will "draw" the image drawn to the top or canvas
Finally, it's better to have the original background color displayed.
Paint.setxfermode (null);//So there is no overlay mode
Canvas.restoretocount (1);//Because the layer stack is drawn to the upper level, this is also the corresponding overlay mode

That is, the canvas is the lowest 1 stack order as shown

Questions:

Canvas.restoretocount (1);//Can not be justified by the multi-layer should be used!!! But I don't know why it doesn't make!!!
If anyone understands, please tell

Porterduffxfermode correct use Method (detailed)

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.