Basic tutorial for Android -- 8.3.11 Paint API -- ColorFilter (color filter) (3-3)

Source: Internet
Author: User
Tags getcolor

Basic tutorial for Android -- 8.3.11 Paint API -- ColorFilter (color filter) (3-3)

This section introduces:

Well, I would have told you not to write it today. After all, it's hard to write it ~, What this section brings to you is:
The third subclass of ColorFilter:Porterducolorfilter, SeePorterDuffNo.
If you have read the previous
Basic tutorial for Android -- 8.3.5 painting API -- Xfermode and PorterDuff (2)
In fact, the effects are the same, but the color is used here and it is easy to set directly. Let's write a simple
In this example, we use 6 different colors to test the 18 modes!
Official API documentation: porterducolorfilter
We can see that the key is its constructor:

The front is the color and the back is the mode ~, Come and come, write example:

1. test sample code:

Run:

Code Implementation:

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

 
      
       
        
     
    
   
  

Then we compile a POJO service class:Data. java:

/** * Created by Jay on 2015/10/29 0029. */public class Data {    private int color;    private PorterDuff.Mode mode;    public Data() {    }    public Data(int color, PorterDuff.Mode mode) {        this.color = color;        this.mode = mode;    }    public int getColor() {        return color;    }    public PorterDuff.Mode getMode() {        return mode;    }    public void setColor(int color) {        this.color = color;    }    public void setMode(PorterDuff.Mode mode) {        this.mode = mode;    }}

As for the Adapter class, we will use the reusable User-Defined BaseAdapter class previously written. This will not be pasted here, but we need to add
Multiple methods:

/*** Set ColorFilter **/public ViewHolder setColorFilter (int id, int color, PorterDuff. mode mode) {View view = getView (id); if (view instanceof ImageView) {(ImageView) view ). setColorFilter (color, mode);} return this ;}

Next is our main layout file:Activity_main.xml:


      
   
  

Finally, ourMainActivity. javaClass, fill data, set Adapter, very simple:

Public class MainActivity extends AppCompatActivity {private GridView gd_show; private ArrayListItems = null; private MyAdapterMyAdapter = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gd_show = (GridView) findViewById (R. id. gd_show); // fill in data and traverse Mode: items = new ArrayList(); For (PorterDuff. mode mode: PorterDuff. mode. class. getEnumConstants () {items. add (new Data (0x77E50961, mode); items. add (new Data (0xFFE50961, mode); items. add (new Data (0x77FFFFFF, mode); items. add (new Data (0 xFFFFFFFF, mode); items. add (new Data (0x77000000, mode); items. add (new Data (0xFF000000, mode);} myAdapter = new MyAdapter(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 motion picture above may be too fast. Sometimes the reader will check it and separate the screenshot here, because the useful screenshot full screen tool is not found,
So here we can only segment...

 

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.