Simple use of Xfermode in Android

Source: Internet
Author: User

Simple use of Xfermode in Android

First of all, when writing this blog, you need to note that I was inspired by the blog:
SetXfermode (Xfermode xfermode) of Paint)
Secondly, when I write this blog post, I cannot avoid complaints. Most of the other articles on the Internet about Xfermode are about the meaning of the properties in the official google documentation, which are similar and probably translated.
What I want to say is that it cannot be a little original?

So, I decided to write this article:

One is to express my heart's tangle; the other is to express so many articles in the world that I do not have my own articles; the third is to copy them, and I also want to add my own insights;
4. record the information so that you do not forget it later;

In addition, I prepared to write this article by referring to the source code, but I didn't download it after the download. I didn't find apidemo in samples downloaded by android5.1. I don't know what's going on, if you know what is going on with shoes, you can talk to me and download the android 5.0 sdk quickly. However, the sdk after is basically unresponsive and cannot be downloaded.

Let's go to the topic below.:
Paste Xfermode in Graphics in google's official ApiDemo as follows:

After two days, I finally got it.

The most annoying thing is that you look at other people's blogs and want to make the results of google's official attributes, but the effect is not. I am so angry that I have to get it out. Is more entangled than others.
As a result, with this blog post.

It may be that my brain is not working. Let's look at other blog posts:
They are all written after XxxView extends ImageView, but the key points are not described. Next, I will introduce the simplest and most direct practices. To make it easier for everyone to understand, simple and effective is the king !!

First, I declare, NoMalicious attacks and bumping

, Just to everyoneComparisonAs a matter of fact, we only need to simply use it, instead of posting all the written code. It's just a few clicks, and it also needs to let everyone know how to use it.

 
A lot of articles about Baidu are omitted here. The Code does not know what the main idea is ........,Not Irrigation,Not an attack,Just for Reference

The following is the actual text.
For the xfermode in the official document, we only see two images with similar layers.
In factLayerSame;
One isOriginal LayerAnd the other isMask Layer;
I set the canvas of the entire View to green to make it easier for you to View the effect.

Canvas. drawColor (Color. GREEN); // set the canvas to GREEN.

Come on first, heyInstructor Cang.

After reading the correct running results, let's take a look at the effects of incorrect running:
It's my teacher, and it's also everyone's teacher.

Why?
Finally, refer to the link at the beginning of the article to show everyone the settings:

// Save the painting 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)

Don't underestimate this method. We must set to save the painting operationNew LayerOtherwise, the instructor's profile picture may be abnormal.
Of course, you have to restore the canvas.

// Restore the canvas
Canvas. restoreToCount (SC );

Canvas, as its name implies, is a canvas. All the painting operations occur on this canvas. Of course, when we want to use multiple image overlays, we need to use "layers", such as Baidu map or AMAP, they add new things, such as lines and points of interest, to the map using Layer layers.
Of course, Canvas provides Layer support. These layers are managed according to the stack structure, and are displayed in the order of first-in-first-out;
When a Layer is added to the stack, subsequent drawXXXX operations occur on the Layer. When the Layer moves back to the stack, it will "Draw" the image drawn at the current Layer to the upper Layer or Canvas. When copying the Layer to the Canvas, you can specify the transparency of the Layer ), this is specified during Layer creation:
Public int saveLayerAlpha (RectF bounds, int alpha, int saveFlags)
We will not describe saveLayerAplha in this article. You can use it on your own.

So if there are so many words, some people will definitely say the code when reading the blog. Wait a moment and the guest will be there immediately:
Of course, many articles on Xfermode have written the description of relevant attributes. Of course, when I was doing this, I gave the official image results. Yes. If I say so, everyone will be confused again. Hahahaha, this is also my style.

All my operations are written in the onDraw () function body. Performance is not discussed in this article. You don't need to operate new objects in onDraw to improve the performance.
Of course, the custom view cannot be avoided. Please measure it first. To make it simple and straightforward:

@ 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, the Code in onDraw is all here:

@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Canvas. drawColor (Color. GREEN); // set the canvas to GREEN.
Canvas. translate (20, 20); // move the canvas
// Original Image
Bitmap src = BitmapFactory. decodeResource (getResources (), R. mipmap. cangcang );
// Mask of the image
Bitmap mask = Bitmap. createBitmap (300,300, src. getConfig ());
Canvas cc = new Canvas (mask );
Cc. drawCircle (150,150,150, mPaint );
/*
* Off-screen Cache
* Set the width and height of the Layer. Otherwise, some parts are no longer in the Layer. Your operations will not work for 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 hybrid mode (only draw the target image where the source image and the target image InterSect)
MPaint. setXfermode (new porterduxfermode (PorterDuff. Mode. DST_IN ));
// Draw a src Source Diagram
Canvas. drawBitmap (mask, 0, 0, mPaint );
// Restore the hybrid mode
MPaint. setXfermode (null );
// Restore the canvas
Canvas. restoreToCount (SC );
}

You can try other xfermode attributes by yourself. Premise:LayerYou must control the width and height of the image.

Related Article

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.