Android Basics Getting Started tutorial--8.3.11 Paint API--colorfilter (color filter) (3-3)

Source: Internet
Author: User
Tags getcolor

Android Basics Getting Started tutorial--8.3.11 Paint API--colorfilter (color filter) (3-3)

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

Well, originally said not to write today, or write it, after all, rare idle ha ~, this section to bring you are
Colorfilter's third sub-class:porterduffcolorfilter, see Porterduff everyone will not
Strange, if you have seen the front
Android Basics Getting Started tutorial--8.3.5 Paint API--xfermode and Porterduff details (ii)
In fact, the effect is the same, but here is the color, and the direct setting is good, let's write a simple
example, we take 6 different colors and test 18 modes!
Official API Document: Porterduffcolorfilter
We can see that the key is also the way he constructs it:

The front is the color, followed by the pattern ~, come, write an example:

1. Test code example:

Run :

Code Implementation :

Here we use a GridView to install them, let's first write down the layout of each item:view_item.xml:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =;     <imageview  android:id
      = "@+id/img_show"  android:layout_width  =         "wrap_content"  android:layout_height  = "wrap_content"  android:src  = "@mipmap/ic_launcher" />     <textview  android:id  = "@+id/tv_color"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:gravity  =< Span class= "Hljs-value" > "center_vertical"  android:textsize  = "12SP"  android:text  = "color"  android:textcolor  = "#FFFFFFFF" />< /span>    <TextViewandroid:id= "@+id/tv_mode"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:textcolor=" #FFD9ECFF "  Android:text="mode"/>                                        </linearlayout>

Then we write a Pojo business class:Data.java:

/** * Created by Jay on 2015/10/29 0029. * * Public  class Data {    Private intColorPrivatePorterduff.mode Mode; Public Data() {    } Public Data(intColor, Porterduff.mode Mode) { This. color = color; This. mode = mode; } Public int GetColor() {returnColor } PublicPorterduff.modeGetMode() {returnMode } Public void SetColor(intColor) { This. color = color; } Public void SetMode(Porterduff.mode Mode) { This. mode = mode; }}

As for the adapter class, we used the reusable custom baseadapter class that we wrote earlier, but
Multiple methods:

        /**         * 设置ColorFilter         * */        publicsetColorFilter(int id,int color,PorterDuff.Mode mode){            View view = getView(id);            ifinstanceof ImageView) {                ((ImageView) view).setColorFilter(color,mode);            }            returnthis;        }

And then our main layout file:activity_main.xml:

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =;     <GridViewandroid:id="@+id/gd_show"android:background="#FF333333 "android:numcolumns=" 6 "android:layout_width=" Match_parent "  Android:layout_height="wrap_content"/>                                        </linearlayout>

Finally, our Mainactivity.java class, populating the data, setting the adapter, is very simple:

 Public  class mainactivity extends appcompatactivity {    PrivateGridView gd_show;Privatearraylist<data> items =NULL;PrivateMyadapter<data> Myadapter =NULL;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Gd_show = (GridView) Findviewbyid (r.id.gd_show);//Fill data, Traverse mode:Items =NewArraylist<data> (); for(Porterduff.mode mode:PorterDuff.Mode.class.getEnumConstants ()) {Items.Add (NewData (0x77e50961, mode)); Items.Add (NewData (0xffe50961, mode)); Items.Add (NewData (0x77ffffff, mode)); Items.Add (NewData (0xFFFFFFFF, mode)); Items.Add (NewData (0x77000000, mode)); Items.Add (NewData (0xff000000, mode)); } Myadapter =NewMyadapter<data> (items, R.layout.view_item) {@Override             Public void BindView(Viewholder holder, Data obj)                {Holder.setcolorfilter (R.id.img_show, Obj.getcolor (), Obj.getmode ()); Holder.settext (R.id.tv_color, String.Format ("%08x", Obj.getcolor ()));            Holder.settext (R.id.tv_mode, Obj.getmode (). toString ());        }        };    Gd_show.setadapter (Myadapter); }}

The above motion diagram may be too fast, and sometimes the reader to look at, here separate plot, because no easy to find the use of the full screen tool,
So here we can only segment the section ...

2. The sample code for this section is downloaded:

Porterduffcolorfilterdemo2.zip

This section summarizes:

This section is very brief, the API document is a usage, here also put 18 kinds of cases are listed, I believe
will be to everyone learning image mix to bring help ~ Thank you, today to take a day off, will school and feel the next students
Feeling, went to a trip to the library, saw a big wave of beauty, and then the mood is nice, decided or temporarily first
In this company to do a good job as an intern, change the environment may not change anything, start by changing yourself
PS: Example excerpt from Github:colorfiltertest

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

Android Basics Getting Started tutorial--8.3.11 Paint API--colorfilter (color filter) (3-3)

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.