Android Background tint Color rendering

Source: Internet
Author: User
Tags transparent color

This article is mainly about Android color rendering, first look at Porterduff, is very important to the drawing.
The origin of Porterduff:
Believe that most people see this proterduff word is very strange, it is swollen what a meaning, and then use Youdao Ah, Jinshan AH began to translate, but the translation software given the results are definitely still proterduff or "not found."
This is God horse situation? Because Proterduff is a combination of two names: Tomas Proter and Tom Duff. They were the first big-siggraph people to put together the concept of graphic blending. Interested children's boots can check it out and learn more about it.
Using Proterbuff.mode we can complete any 2D image measurement operation, such as the eraser effect in the graffiti application, draw a variety of custom progress, and so very powerful effect, see below the effect:

From the above we can see that Porterduff.mode is an enumeration class with a total of 16 enumeration values:
1.porterduff.mode.clear
The drawing is not submitted to the canvas.
2.porterduff.mode.src
Show Top Draw picture
3.porterduff.mode.dst
Show Lower drawing pictures
4.porterduff.mode.src_over
Normal drawing shows that the upper and lower layers are drawn with overlapping covers.
5.porterduff.mode.dst_over
The upper and lower layers are displayed. The lower level is shown on the home.
6.porterduff.mode.src_in
Draw the intersection of two layers. Displays the upper layer.
7.porterduff.mode.dst_in
Draw the intersection of two layers. Displays the lower layer.
8.porterduff.mode.src_out
Draw the non-intersecting portion of the upper layer.
9.porterduff.mode.dst_out
Remove the layer to draw the non-intersecting part.
10.porterduff.mode.src_atop
Remove the non-intersecting part of the layer from the upper intersection section
11.porterduff.mode.dst_atop
Take the upper non-intersecting part and the lower part of the intersection
12.porterduff.mode.xor
XOR: Remove the two Layer intersection section
13.porterduff.mode.darken
Take two layers of all areas, the intersection part of the color deepened
14.porterduff.mode.lighten
Take two layers all, light the intersection part color
15.porterduff.mode.multiply
Take two layer intersection part overlay color
16.porterduff.mode.screen
Take two layers of all areas, the intersection part becomes transparent color

There are two other
17.porterduff.mode.add
18.porterduff.mode.overlay

So what is tint?
We can set it through the attributes Android:backgroundtint and Android:backgroundtintmode in XML, Android: Backgroundtintmode the value of this attribute is just above those porterduff.mode in the value, the effect of the above diagram is also shown. Android:backgroundtint's words are the values of color.

So what's the difference between Android:background and android:backgroundtint?
If Android:background is set, the background color of the control is modified directly.
If the android:backgroundtint is set, then the set color and the original background will be superimposed on the process, as to how to overlay, is the above mode.
See an example:

<Button    android:id="@+id/button_1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@drawable/button_states" android:text="text" />

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:state_pressed="false" android:drawable="@drawable/button_normal" /></selector>

This is what the button normally looks like:

Set Tint for button:

Button b = (Button)findViewById(R.id.button_1);int tint = Color.parseColor("cyan");b .getBackground().setColorFilter(tint, Mode.DARKEN);
    • 1


See, the effect is superimposed, not directly covered. Use tint to preserve the original shadow ripple and other effects.

Another method is the Setbackgroundtintlist (colorstatelist) and Setbackgroundtintmode (Porterduff.mode) methods.
If the control has no background, setting Backgroundtint is not valid.

button b = (Button) Findviewbyid (r.id.button_1); B.settext (Modes[i-1]); int[] colors = new int[]{0xfff8513f, 0xffe43d2b}; int[][] states = new int[2][];states[0] = new int[]{android. R.attr.state_pressed};states[1] = new int[]{android. R.attr.state_enabled};b.setbackgroundtintlist (new colorstatelist (states, colors)); B.setbackgroundtintmode (Mode[i-1]);          
    • 1

The effect is similar to the above, but the color transformation of the state transformation is more.

However, if the control state transitions, the color does not change, you can use the

.getBackground().setColorFilter(tint, Mode.DARKEN);

Because this API is available in version 21, the following methods can be called for low-version calls:

ViewCompat.setBackgroundTintList(b, new ColorStateList(states, colors));ViewCompat.setBackgroundTintMode(b, Mode.DARKEN);

But now the normal button, to call Setbackgroundtint and Setbackgroundtintmode invalid, no effect. Solve this problem, You can then call Setsupportbackgroundtintlist and the Setsupportbackgroundtintmode method by using Android.support.v7.widget.AppCompatButton.

Android Background tint Color rendering

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.