Xfermode Simple usage in Android

Source: Internet
Author: User

First, when writing this blog, I need to explain that I am referring to the inspiration from that blog post:
detailed setxfermode of paint (Xfermode xfermode)
Secondly, when writing this blog post, you can not avoid complaining AH. Most of the other online information about Xfermode is the meaning of attributes in Google's official documents, which are all very similar.
What I'm trying to say is that you can't be a little original?

So, I decided to write this article:

One is to express the tangled in my heart, the second is to express so much of the world article a big copy is not his own article; three is to copy it, but also to join their own sentiment;
Four is record, lest forget later;

In addition, I was prepared to refer to the source code to write this article, but I did not download after the download, android5.1 download samples inside did not find Apidemo, do not know how, if there are children shoes know what is going on, and I can say, and download Download android5.1 SDK speed is fast ah, but after 5.0 of the SDK basically unresponsive, download not.

let's get to the bottom:
It's a cliché to stick to Google's official Apidemo graphics in Xfermode:

Tangled up for 2 days and finally got it out

The most irritating is that you look at other people's blog and then want to do Google's official properties of the effect, but Leng did not make the effect, I this temper, do not want to get out. is to be more entangled than others.
So, with this blog post.

It's possible that my brain is not working, give the guys a look at other blog posts:
They were all Xxxview extends ImageView then wrote, but the key points were not introduced. I will give you the following to introduce the simplest direct approach, in order to facilitate the people to see understand, simple and effective is the kingly!!

First of all, I declare. is notMalicious attacks and irrigation

, just for the comparison , in fact, we just need to simple how to use instead of the full set of code are posted out. Point to stop, but also let everyone know how to use.

[Blog One] (http://blog.csdn.net/lmj623565791/article/details/42094215)
[Blog II] (http://407827531.iteye.com/blog/1470519)
[Blog III] (http://www.111cn.net/sj/android/74912.htm)
Baidu here omitted a lot of articles, all the code does not know what they want to say the main idea is what ..., not irrigation , not attack , but for everyone to learn from

The following is really the text of AH: the attention of everyone.
For the Xfermode of official documents we just saw two images of a similar layer
In fact, the same as in Photoshop;
One is the original layer and the other is the mask layer ;
I set the entire view of the canvas to green, in order to facilitate people to see the effect

Canvas.drawcolor (Color.green);//Painted Green

Let's go first, hey, Mr. Cang 's head.

After reading the correct run out of the effect, then give us a look at the effect of the error run out:
Is my old teacher, but also everyone's old teacher

What's this for?
Finally, referring to the article at the beginning of the link to see the guys must be set:

Save the drawing operation to a new layer (off-screen cache)
Canvas.savelayer (0,0,300,300, Null,canvas.all_save_flag);
Method:
public int Savelayer (float left, float top, float right, float bottom, paint paint, int saveflags)

Do not underestimate this method, we must set to save the drawing operation to a new layer , or it will appear the teacher portrait of the abnormal situation
And, of course, finally, restore the canvas

Restore Canvas
Canvas.restoretocount (SC);

Canvas is the name of canvas. All the drawing operations take place on this canvas, of course, when we want to use multiple image overlay and so on, we will use "layer", such as Baidu Map or high-gold map, they add new things on the map, such as lines, points of interest are used to layer layers.
Of course, Canvas provides us with layer support; These layers are managed according to the stack structure, and the last-in-first-out order is displayed;
When the layer is in the stack, subsequent drawxxxx operations occur on the layer. When layer is retired, the image drawn on this layer is "drawn" to the top or canvas, and when you copy a layer to a canvas, you specify the layer's transparency (layer), which is specified when you create a layer:
public int Savelayeralpha (RECTF bounds, int alpha, int saveflags)
We don't explain Savelayeraplha in this article. Everyone can use it on their own

So much so, some people read the blog must say code, a little while, Sir immediately come:
Of course, Xfermode online many articles have written about the meaning of the relevant attributes, of course, I do this when the official give the picture to do the effect. Meaning, can be understood. If I say it, the guys are confused again. Haha, this is my style.

Then all my operations are written in the OnDraw () function body. As far as performance is concerned, this article is not about it. You can improve performance without having to manipulate the new object inside the OnDraw.
Of course, the custom view can not be avoided, measured first. For the sake of simplicity directly this:

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
int width=measurespec.getsize (WIDTHMEASURESPEC);
int height=measurespec.getsize (HEIGHTMEASURESPEC);
Setmeasureddimension (Width,height);
}

Finally, put the OnDraw inside the code, all in this:

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Canvas.drawcolor (Color.green);//Set Canvas Green
Canvas.translate (20,20);//Move Canvas
Original picture
Bitmap src = bitmapfactory.decoderesource (getresources (), R.mipmap.cangcang);
Masks for pictures
Bitmap Mask=bitmap.createbitmap (+, Src.getconfig ());
Canvas cc=new canvas (mask);
Cc.drawcircle (150,150,150,mpaint);
/*
* Off-screen cache
* Layer width and Gaoyao set well, otherwise there will be some parts no longer in the layer, your operation is not working on these parts
*/
int sc = Canvas.savelayer (0,0,300,300, NULL, Canvas.all_save_flag);
Draw the DIS target diagram first
Canvas.drawbitmap (SRC, 0, 0, mpaint);
Set the blending mode (the target image is drawn only where the source image and the target image intersect)
Mpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN));
Draw src Source Map again
Canvas.drawbitmap (mask, 0, 0, mpaint);
Restore blending Mode
Mpaint.setxfermode (NULL);
Restore Canvas
Canvas.restoretocount (SC);
}

Xfermode Simple usage in Android

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.