Android Basics Getting Started tutorial--8.3.4 Paint API--xfermode and Porterduff details (i)

Source: Internet
Author: User

Android Basics Getting Started tutorial--8.3.4 Paint API--xfermode and Porterduff details (i)

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

Do not know the title of these two things you are familiar with, if you realize the fillet or round the picture, believe that the two nouns
No mode, 1:30 guy didn't remember? It's okay, do you ever see the picture below?

PS: Online is said in: \samples\android-xx\legacy\apidemos\src\com\example\android\apis\graphics
Can find this picture--, but not, do not know is because my sample is android-22, only here to find a
Xfermodes.java java file! Here directly posted on the internet found ~
Well, say it back, this picture believe most of the friends have seen it, have not seen it is OK, this section we bring you a little bit of study
This thing ~, look back to our previous Android Basics tutorial--8.3.1 three drawing Tools class
Setxfermode (Xfermode Xfermode): Sets the way in which graphics overlap, such as merging, taking intersection or union,
Often used to create eraser effect!
We came to the official document: Xfermode, we found he had three sons:

In this section we will study his first two sons ~

Older son: Avoidxfermode

Well, as with the two subclasses of the previous Maskfilter, hardware acceleration is not supported, so if it is a version of API 14 or higher,
Hardware acceleration needs to be turned off to be effective! How to shut yourself up to a section ha ~
Let's take a look at the construction method he gave us! Official API Document: Avoidxfermode

There are three parameters, followed by:
opcolor: A hexadecimal color value with transparency, such as 0X00C4C4;
tolerance: Tolerance value, if you have learned PS may have used magic wand tool, is to set the range of color values, such as
Tolerance is 0, you choose the small point of pure black, when the tolerance tune to 40, the range has been enlarged to a large block of black! If
Still not very clear, wait for us to write the code to know!
mode: Avoidxfermode model, there are two kinds:TARGET and AVOID

Mode 1:avoidxfermode.mode.target

This mode will determine if there are any colors on the canvas that are not the same as the color values we set, and if so, the areas
Stained with a layer of brush-defined colors, other places do not stain! Below we write code demo under, by the way let everyone feel the next
This tolerance value!

Using code examples :

Run :

Well, first up and down the original, footage from Gank.io:

Next we take the color of a place on the wall with a color picker and write a simple view!
PS: Need to add shutdown hardware acceleration in androidmanifest.xml appliction node:
Android:hardwareaccelerated= "false"

/** * Created by Jay on 2015/10/22 0022. * * Public  class AvoidXfermodeView1 extends View {    PrivatePaint Mpaint;PrivateBitmap Mbitmap;PrivateAvoidxfermode Avoidxfermode; Public AvoidXfermodeView1(Context context) {Super(context);    Init (); } Public AvoidXfermodeView1(context context, AttributeSet attrs) {Super(context, attrs);    Init (); } Public AvoidXfermodeView1(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Init (); }Private void Init() {Mpaint =NewPaint (Paint.anti_alias_flag);//anti-aliasingAvoidxfermode =NewAvoidxfermode (0xffccd1d4,0, AvoidXfermode.Mode.TARGET);    Mbitmap = Bitmapfactory.decoderesource (Getresources (), R.mipmap.iv_meizi); }@Override    protected void OnDraw(Canvas canvas) {Canvas.drawbitmap (Mbitmap, -, -, Mpaint); Mpaint.setargb (255,222, the, in);        Mpaint.setxfermode (Avoidxfermode); Canvas.drawrect ( -, -,690,1010, Mpaint); }}

post-run effects :

See the pile of aunts on the wall red No, the effect of the leverage, here our tolerance value does not play a role, we changed,
Sister's white dress becomes aunt Red!
We change the contents of the above constructed Avoidxfermode to:

avoidXfermode = new AvoidXfermode(0XFFD9E5F325, AvoidXfermode.Mode.TARGET);

Then, her sister's white clothes will become Aunt Red ..., full of guilt ...

Mode 2:avoidxfermode.mode.avoid

In contrast to the above target mode, the color is the same as above, the color is changed
The tolerance value also brings the opposite result, and the tolerance value is 0 o'clock, only if the pixel color value in the picture is completely different from the set color value
, and when the tolerance reaches the maximum value of 255, a little bit of color will be dyed!
We simply modify the above example can be, the same is modified under the construction of Avoidxfermode content!
Let's change to the following sentence:

avoidXfermode = new AvoidXfermode(0XFFD9E5F3,230, AvoidXfermode.Mode.AVOID);

Run :

Two sons: Pixelxorxfermode

This is another image blending mode, which is simpler than the older son, and is constructed as follows:
Official API Document: Pixelxorxfermode

Parameter resolution:
Just one 16-band transparent worth of color values, as for the effect of this value, there is an algorithm:
Pixelxorxfermode internal is based on the "Opcolor ^ src ^ dst" This XOR algorithm operation,
Get an opaque (alpha = 255) color value, set to the image! All right, this is on the Internet.
Specifically I do not know, write an example to try the effect Bai ~

code example :

Run :

Implementation Code :

/** * Created by Jay on 2015/10/22 0022. * * Public  class PixelXorXfermodeView1 extends View{    PrivatePaint Mpaint;PrivateBitmap Mbitmap;PrivatePixelxorxfermode Pixelxorxfermode; Public PixelXorXfermodeView1(Context context) {Super(context);    Init (); } Public PixelXorXfermodeView1(context context, AttributeSet attrs) {Super(context, attrs);    Init (); } Public PixelXorXfermodeView1(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Init (); }Private void Init() {Mpaint =NewPaint (Paint.anti_alias_flag);//anti-aliasingPixelxorxfermode =NewPixelxorxfermode (0xffd9e5f3);    Mbitmap = Bitmapfactory.decoderesource (Getresources (), R.mipmap.iv_meizi); }@Override    protected void OnDraw(Canvas canvas) {Canvas.drawbitmap (Mbitmap, -, -, Mpaint); Mpaint.setargb (255,222, the, in);        Mpaint.setxfermode (Pixelxorxfermode); Canvas.drawrect ( -, -,690,1010, Mpaint); }}
The sample code for this section is downloaded:

Xfermodedemo.zip

This section summarizes:

Well, full of guilt, very beautiful sister, the results for me to write a demo this way, don't blame me,

Well, yes, forget to say that Xfermode's older son and two sons have passed away (expired), in the version after API 16,
Expired, also said this section and no egg use ...
It is not possible to say that Apache was castrated after the 4.4 version, but someone was using httpclient, or
HTTP request framework written by this library ha ~ Of course, this kind of people are rarely very small! But there's no harm in learning more.
The next section of the three sons Porterduffxfermode is not outdated, but also very important, ah, rest assured, will not ruin the photos!
Well, that's all, thank you.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--8.3.4 Paint API--xfermode and Porterduff details (i)

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.